-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (124 loc) · 5.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var Hapi = require('hapi')
, twilio = require('twilio')
, restClient = new twilio.RestClient(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN)
, taskRouter = new twilio.TaskRouterClient(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
var commands = ['ACCEPT', 'DECLINE', 'CLOSE'];
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: process.env.PORT
});
var organizers = {};
// cache the organizer/worker info
taskRouter.workspaces(process.env.TWILIO_WORKSPACE_SID).workers.get(function(err, res){
res.workers.forEach(function(worker) {
var worker = JSON.parse(worker.attributes);
var key = worker['contact-uri'];
organizers[key] = {};
organizers[key]['worker'] = worker;
});
console.log(organizers);
});
var getActiveOrganizer = function(number) {
var activeOrganizer;
Object.keys(organizers).forEach(function(key) {
if (organizers[key].task && organizers[key].task.from === number) {
activeOrganizer = key;
}
});
console.log("Worker for ", number, " is ", activeOrganizer);
return activeOrganizer;
}
var processCommand = function(command, from) {
var stash = organizers[from];
if (command === 'ACCEPT') {
stash['accepted'] = true;
taskRouter.workspaces(process.env.TWILIO_WORKSPACE_SID).tasks(stash.taskSid).reservations(stash.reservationSid).post({ReservationStatus: 'accepted'}, function(err, res) { console.log(err, res); });
}
else if (command === 'DECLINE') {
taskRouter.workspaces(process.env.TWILIO_WORKSPACE_SID).tasks(stash.taskSid).reservations(stash.reservationSid).post({ReservationStatus: 'rejected'}, function(err, res) { console.log(err, res); });
organizers[from] = {};
}
else if (command === 'CLOSE') {
taskRouter.workspaces(process.env.TWILIO_WORKSPACE_SID).workers(stash.workerSid).post({ActivitySid: process.env.TWILIO_ACTIVITY_IDLE_SID}, function(err, res) { console.log(err, res); })
organizers[from] = {};
}
};
var events = function(request, reply) {
console.log("TaskRouter Event: ", request.payload);
};
var assignment = function(request, reply) {
console.log(request.payload);
// get task info
var task = JSON.parse(request.payload.TaskAttributes);
var worker = JSON.parse(request.payload.WorkerAttributes);
// store some state!
var key = worker['contact-uri'];
organizers[key]['task'] = task;
organizers[key]['worker'] = worker;
organizers[key]['taskSid'] = request.payload.TaskSid;
organizers[key]['reservationSid'] = request.payload.ReservationSid;
organizers[key]['workerSid'] = request.payload.WorkerSid;
var message = task.from+ ":\n\n"+task['body']+"\n\nReply with 'accept' or 'reject'";
// send text to organizer
restClient.messages.create({from: task['to'] , to: worker['contact-uri'], body: message});
reply(JSON.stringify({})).type('application/json');
};
// Process incoming text messages
var sms = function(request, reply) {
var twiml = new twilio.TwimlResponse();
// check to see if the text is from an organizer
if (Object.keys(organizers).indexOf(request.payload.From) >= 0) {
var stash = organizers[request.payload.From];
var command = request.payload.Body.toUpperCase().trim();
// check to see if the message is a command
if (commands.indexOf(command) >= 0) {
processCommand(command, request.payload.From);
}
// if currently reserved, forward message to attendee
else if (stash.accepted) {
restClient.messages.create({from: request.payload.To, to: stash.task.from, body: stash.worker.name + ": " + request.payload.Body});
}
// send message to fellow organizers
else {
Object.keys(organizers).forEach(function(num) {
if (num != request.payload.From) {
restClient.messages.create({from: request.payload.To, to: num, body: organizers[request.payload.From].worker.name + ": " + request.payload.Body});
}
});
}
}
else {
var activeOrganizer = getActiveOrganizer(request.payload.From);
// if this is brand-new, create task
if (activeOrganizer === undefined) {
var incomingText = {
to: request.payload.To,
from: request.payload.From,
body: request.payload.Body
};
// post Task to TaskRouter
console.log("Posting Task to TaskRouter");
taskRouter.workspaces(process.env.TWILIO_WORKSPACE_SID).tasks.create({workflowSid: process.env.TWILIO_WORKFLOW_SID, attributes: JSON.stringify(incomingText)}, function(err, res) {console.log(err, res);});
// send response to end user
twiml.message('We have received your request and will be in touch shortly.')
}
// else, send to active/assigned worker
else {
restClient.messages.create({from: request.payload.To, to: activeOrganizer, body: request.payload.From + ": " + request.payload.Body});
}
}
reply(twiml.toString()).type('text/xml');
};
server.route({
method: 'POST', path:'/events', handler: events
});
server.route({
method: 'POST', path:'/assignment', handler: assignment
});
server.route({
method: 'POST', path:'/sms', handler: sms
});
// Start the server
server.start();
console.log("Server started on port ", process.env.PORT);