-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
54 lines (48 loc) · 1.5 KB
/
handler.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
'use strict';
const request = require('request')
const twilio = require('twilio')
const slack_message = text => ({
username: 'smsbot',
icon_emoji: ':phone:',
text
})
function reply (params) {
console.log('reply invoked with:', params)
const client = twilio(params.twilio.account, params.twilio.auth)
return new Promise((resolve, reject) => {
client.messages.list({to: params.twilio.number}, (err, data) => {
if (err) return Promise.reject(err)
const last = data.messages[0]
const msg = params.text.slice(params.trigger_word.length + 1)
client.messages.create({ to: last.from, from: last.to, body: msg }, (err, message) => {
if (err) return Promise.reject(err)
resolve(slack_message(`sms reply sent to ${last.from}`))
})
})
})
}
function incoming (params) {
console.log('incoming invoked with:', params)
return new Promise(function (resolve, reject) {
const body = params.Body.replace(/\@\w+/g, '<$&>')
const from = params.numbers[params.From] || 'Unknown'
request.post({
method: 'post',
body: slack_message(`Text message from ${from} (${params.From}): ${body}`),
json: true,
url: params.slack.webhook
}, () => {
const resp = new twilio.TwimlResponse()
resp.message('Thanks for letting us know.')
resolve({
headers: {
'Content-Type': 'text/xml'
},
code: 200,
body: resp.toString()
})
})
})
}
exports.reply = reply
exports.incoming = incoming