-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
218 lines (170 loc) · 6.2 KB
/
app.coffee
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
WebSocket = require('ws')
winston = require('winston')
express = require('express')
request = require('superagent')
Pusher = require('pusher-client')
bodyParser = require('body-parser')
emoji = require('js-emoji')
emoji.init_env()
emoji.replace_mode = 'unified'
logger = new winston.Logger
transports: [
new winston.transports.Console
prettyPrint: true
level: process.env["LOG_LEVEL"]
]
BITLY_TOKEN = process.env["BITLY_TOKEN"]
SLACK_BOT = process.env["SLACK_BOT"]
SLACK_USER = process.env["SLACK_USER"]
SLACK_BOT_TOKEN = process.env["SLACK_BOT_TOKEN"]
SLACK_USER_TOKEN = process.env["SLACK_USER_TOKEN"]
SLACK_TOKEN = process.env["SLACK_TOKEN"]
USER_SLUG = process.env["USER_SLUG"]
USER_NUMBER = process.env["USER_NUMBER"]
USER_TOKEN = process.env["USER_TOKEN"]
USER_PHOTO_TOKEN = process.env["USER_PHOTO_TOKEN"]
slack = (method, role, payload, callback) ->
token = if role == "bot"
SLACK_BOT_TOKEN
else if role == "user"
SLACK_USER_TOKEN
request
.post("https://slack.com/api/#{method}")
.type('form')
.send(token: token)
.send(payload)
.end (err, result) ->
if err
logger.log('error', 'Slack error', err)
callback?(err, null)
else
if result.body.ok
callback?(null, result.body)
else
logger.log('error', 'Slack error', result.body.error)
callback?(result.body.error, null)
findOrCreateGroup = (number, name, callback) ->
slack "groups.list", "user", {exclude_archived: true}, (err, result) ->
for group in result.groups when group.purpose.value == number
return callback(null, group.id)
slack "groups.create", "user", {name}, (err, result) ->
group_id = result.group.id
slack "groups.setPurpose", "user", {channel: group_id, purpose: number}, (err, result) ->
slack "groups.invite", "user", {channel: group_id, user: SLACK_BOT}, (err, result) ->
callback(null, group_id)
processIncomingMessage = (payload) ->
from = payload.feedable.from
text = payload.feedable.text
slug = payload.contact.external_slug
name = payload.contact.external?.google?.name || from
photo_url = "https://api.abbott.io/v1/contacts/#{slug}/photo?token=#{USER_PHOTO_TOKEN}"
request
.get('https://api-ssl.bitly.com/v3/shorten')
.query(access_token: BITLY_TOKEN, longUrl: photo_url)
.end (err, result) ->
findOrCreateGroup from, name, (err, group_id) ->
options =
channel: group_id
text: text
username: name
icon_url: result.body.data.url
slack "chat.postMessage", "bot", options
processIncomingVoicemail = (payload) ->
from = payload.feedable.from
text = payload.feedable.text
url = payload.feedable.voicemail
slug = payload.contact.external_slug
name = payload.contact.external?.google?.name || from
photo_url = "https://api.abbott.io/v1/contacts/#{slug}/photo?token=#{USER_PHOTO_TOKEN}"
request
.get('https://api-ssl.bitly.com/v3/shorten')
.query(access_token: BITLY_TOKEN, longUrl: photo_url)
.end (err, result) ->
findOrCreateGroup from, name, (err, group_id) ->
attachments = JSON.stringify(
[
fallback: text
text: text
title: "Voicemail"
title_link: url,
pretext: "New voicemail from #{name}"
])
options =
channel: group_id
username: name
icon_url: result.body.data.url
attachments: attachments
slack "chat.postMessage", "bot", options
socket = new Pusher '42d6b0407dc69bdaf0b7',
auth:
headers:
'Authorization': "Bearer #{USER_TOKEN}"
authEndpoint: "https://api.abbott.io/v1/feed/subscribe"
encrypted: true
getFeed = (id, callback) ->
request
.get("https://api.abbott.io/v1/feed/#{id}")
.set('Accept', 'application/json')
.set('Authorization', "Bearer #{USER_TOKEN}")
.end (err, result) ->
if err
logger.log('error', 'Feed Retrieval Error', err)
callback(err)
else
logger.log('debug', 'From Server', result.body)
callback(null, result.body)
channel = socket.subscribe "private-#{USER_SLUG}"
channel.bind 'messages', (data) ->
logger.log('debug', 'Received Inbound Message', data)
getFeed data.id, (err, data) ->
if !err
processIncomingMessage(data)
channel.bind 'voicemails', (data) ->
logger.log('debug', 'Received Inbound Voicemail', data)
getFeed data.id, (err, data) ->
if !err
processIncomingVoicemail(data)
slack "rtm.start", "bot", {}, (err, result) ->
ws = new WebSocket(result.url)
ws.on 'error', (error) ->
logger.log('error', 'WebSocket error', error)
ws.on 'close', (code, message) ->
logger.log('error', 'WebSocket connection closed', {code, message})
process.exit(1)
ws.on 'message', (message) ->
message = JSON.parse(message)
logger.log('debug', 'Received Outbound Message', message)
{type, subtype, channel, user, text} = message
return unless type == "message" && user == SLACK_USER && !subtype?
text = text
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/&/g, "&")
text = emoji.replace_colons(text)
slack "groups.list", "user", {exclude_archived: true}, (err, result) ->
for group in result.groups when group.id == channel
to = group.purpose.value
payload = {to, text, from: USER_NUMBER}
logger.log('debug', 'Sent Message', payload)
request
.post('https://api.abbott.io/v1/messages')
.set('Accept', 'application/json')
.set('Authorization', "Bearer #{USER_TOKEN}")
.send(payload)
.end()
return
app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.post '/calls', (req, res) ->
logger.log('debug', 'Received Call Request', req.body)
if req.body.token == SLACK_TOKEN && req.body.user_id == SLACK_USER
slack "groups.info", "user", {channel: req.body.channel_id}, (err, result) ->
if !err
request
.post('https://api.abbott.io/v1/calls')
.set('Accept', 'application/json')
.set('Authorization', "Bearer #{USER_TOKEN}")
.send({to: result.group.purpose.value})
.end()
res.status(200).end()
app.listen(3000)