Skip to content

Commit

Permalink
removed async dependency, added messaging helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Damm committed Jul 21, 2020
1 parent 6eaecb0 commit 6b88f48
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 359 deletions.
161 changes: 82 additions & 79 deletions controllers/helpers/taskrouter-helper.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,84 @@
const twilio = require('twilio')
const context = require('../../context')

const TaskRouterCapability = twilio.jwt.taskrouter.TaskRouterCapability

const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN)

module.exports.createTask = async (attributes = {}) => {
const configuration = context.get().configuration

const payload = {
workflowSid: configuration.twilio.workflowSid,
attributes: JSON.stringify(attributes),
timeout: 3600,
taskChannel: 'voice'
}

return client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).tasks.create(payload)
}

module.exports.getOngoingTasks = (name) => {
let query = {}
query.assignmentStatus = 'pending,assigned,reserved'
query.evaluateTaskAttributes = 'name=\'' + name + '\''

return client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).tasks.list(query)
}
const twilio = require('twilio');

const TaskRouterCapability = twilio.jwt.taskrouter.TaskRouterCapability;

const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);

module.exports.createTask = function (workflowSid, attributes) {
attributes = attributes || {};

const data = {
workflowSid: workflowSid,
attributes: JSON.stringify(attributes),
timeout: 3600,
taskChannel: 'voice'
};

return client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).tasks.create(data);
};

module.exports.getOngoingTasks = function (name) {
return new Promise(function (resolve, reject) {
let query = {};
query.assignmentStatus = 'pending,assigned,reserved';
query.evaluateTaskAttributes = "name='" + name + "'";

client.taskrouter
.workspaces(process.env.TWILIO_WORKSPACE_SID)
.tasks.list(query)
.then((tasks) => {
return resolve(tasks);
})
.catch((error) => {
return reject(error);
});
});
};

const buildWorkspacePolicy = (options) => {
options = options || {}

const resources = options.resources || []
const urlComponents = ['https://taskrouter.twilio.com', 'v1', 'Workspaces', process.env.TWILIO_WORKSPACE_SID]

return new TaskRouterCapability.Policy({
url: urlComponents.concat(resources).join('/'),
method: options.method || 'GET',
allow: true
})
}

module.exports.createWorkerCapabilityToken = (sid) => {
const workerCapability = new TaskRouterCapability({
accountSid: process.env.TWILIO_ACCOUNT_SID,
authToken: process.env.TWILIO_AUTH_TOKEN,
workspaceSid: process.env.TWILIO_WORKSPACE_SID,
channelId: sid,
ttl: 3600,
})

const eventBridgePolicies = twilio.jwt.taskrouter.util.defaultEventBridgePolicies(process.env.TWILIO_ACCOUNT_SID, sid)

const workspacePolicies = [
// Workspace fetch Policy
buildWorkspacePolicy(),
// Workspace subresources fetch Policy
buildWorkspacePolicy({ resources: ['**'] }),
// Workspace resources update Policy
buildWorkspacePolicy({ resources: ['**'], method: 'POST' }),
]

eventBridgePolicies.concat(workspacePolicies).forEach(policy => {
workerCapability.addPolicy(policy)
})

return workerCapability
}

module.exports.findWorker = (friendlyName) => {
const filter = { friendlyName: friendlyName }

return client.taskrouter
.workspaces(process.env.TWILIO_WORKSPACE_SID)
.workers.list(filter)
.then(workers => {
return workers.find(worker => worker.friendlyName === friendlyName)
})

}
options = options || {};

const resources = options.resources || [];
const urlComponents = [
'https://taskrouter.twilio.com',
'v1',
'Workspaces',
process.env.TWILIO_WORKSPACE_SID
];

return new TaskRouterCapability.Policy({
url: urlComponents.concat(resources).join('/'),
method: options.method || 'GET',
allow: true
});
};

module.exports.createWorkerCapabilityToken = function (sid) {
const workerCapability = new TaskRouterCapability({
accountSid: process.env.TWILIO_ACCOUNT_SID,
authToken: process.env.TWILIO_AUTH_TOKEN,
workspaceSid: process.env.TWILIO_WORKSPACE_SID,
channelId: sid,
ttl: 3600
});

const eventBridgePolicies = twilio.jwt.taskrouter.util.defaultEventBridgePolicies(
process.env.TWILIO_ACCOUNT_SID,
sid
);

const workspacePolicies = [
// Workspace fetch Policy
buildWorkspacePolicy(),
// Workspace subresources fetch Policy
buildWorkspacePolicy({ resources: ['**'] }),
// Workspace resources update Policy
buildWorkspacePolicy({ resources: ['**'], method: 'POST' })
];

eventBridgePolicies.concat(workspacePolicies).forEach((policy) => {
workerCapability.addPolicy(policy);
});

return workerCapability;
};
53 changes: 53 additions & 0 deletions controllers/messaging-adapter-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports.getFrom = (to, configuration) => {
switch (getMessengerChannelKey(to)) {
case 'messenger':
return 'messenger:' + configuration.twilio.facebookPageId;
case 'whatsapp':
return 'whatsapp:' + configuration.twilio.whatsAppPhoneNumber;
default:
return configuration.twilio.callerId;
}
};

module.exports.createTaskAttributes = (from, channel) => {
return {
title: `${getMessengerChannelDetail(from).friendlyName} request`,
text: getMessengerChannelDetail(from).text,
channel: 'chat',
endpoint: getMessengerChannelKey(from),
team: 'support',
name: from,
channelSid: channel.sid
};
};

const getMessengerChannelKey = (from) => {
if (from.includes('messenger')) {
return 'messenger';
} else if (from.includes('whatsapp')) {
return 'whatsapp';
}

return 'sms';
};

const getMessengerChannelDetail = (from) => {
const meta = new Map();

meta.set('messenger', {
friendlyName: 'Facebook Messenger',
text: 'Customer requested support on Faceboook'
});

meta.set('whatsapp', {
friendlyName: 'WhatsApp',
text: 'Customer requested support on WhatsApp'
});

meta.set('sms', {
friendlyName: 'SMS',
text: 'Customer requested support via SMS'
});

return meta.get(getMessengerChannelKey(from));
};
Loading

0 comments on commit 6b88f48

Please sign in to comment.