-
Notifications
You must be signed in to change notification settings - Fork 1
/
intslack.py
executable file
·333 lines (301 loc) · 15.2 KB
/
intslack.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
import sys
import json
import copy
import requests
import datetime
import time
import argparse
import logging
import smtplib
import traceback
import socket
import string
from email.mime.text import MIMEText
from flask import Flask, request, jsonify
from bs4 import BeautifulSoup
# Intercom unfortunately is not willing to provide a whitelist of source IP addresses from which
# we might get webhook notifications. As a result, the TCP port has to be open to the world.
# Furthermore, since the Flask web server is single-threaded, it's prone to blocking, such as
# if some attacker did a "telnet <server> <port>" and just let it sit there, that would ordinarily
# cause all subsequent incoming connections to hang waiting for us to finish processing the first
# one. By setting the TCP socket timeout to 10 seconds, this means we'll close all such connections
# if they've not sent us a byte in that amount of time, after which we'll unblock and handle the
# other legit incoming notifications.
socket.setdefaulttimeout(10)
app = Flask(__name__)
def prep_logging(name, log_filepath):
"""Set up logging to go to the console and a file simultaneously."""
newlogger = logging.getLogger(name)
newlogger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(message)s')
filehandler = logging.FileHandler(log_filepath)
consolehandler = logging.StreamHandler()
for handler in [filehandler, consolehandler]:
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
newlogger.addHandler(handler)
return newlogger
def parse_args():
"""Get command-line arguments."""
parser = argparse.ArgumentParser(description='Start an Intercom/Slack relay agent')
parser.add_argument('--port', help='TCP port to listen on for Intercom notifications',
dest='port', required=True)
parser.add_argument('--appid', help='Intercom App ID', dest='appid', required=True)
parser.add_argument('--inttoken', help='Intercom Access Token', dest='inttoken', required=True)
parser.add_argument('--slacktoken', help='Slack API token', dest='slacktoken', required=True)
parser.add_argument('--channel', help='Slack channel to send messages to (no hash)',
dest='channel', required=True)
parser.add_argument('--backupchannel', help='Backup channel with native relay pointing at it (no hash)',
dest='backupchannel', required=True)
parser.add_argument('--email', help='E-mail addrress of who to contact on failures',
dest='email', required=True)
return parser.parse_args()
def failmail(addr, message, copy_to_slack=True):
msg = MIMEText(message)
msg['Subject'] = 'intslack failure'
msg['From'] = addr
msg['To'] = addr
logger.info('Sending Failmail:\n' + msg.as_string())
s = smtplib.SMTP('localhost')
s.sendmail(addr, [addr], msg.as_string())
s.quit()
if copy_to_slack:
slack_message = {}
slack_message['text'] = "!!! Error relaying message from Intercom to Slack. <mailto:" + email + "|" + email + "> will look into it. \nCheck #" + backupchannel + " for missed message.\n" + message
slack_message['color'] = "danger"
slacksend_channel(slack_message, slackchannel)
def user_info(id):
try:
info = {}
req = session.get("https://api.intercom.io/users/" + id, headers=headers)
if req.status_code == 200:
u = req.json()
logger.info('User record from Infocom:\n' + json.dumps(u, sort_keys=True, indent=4))
else:
failmail(email, 'Unexpected status code ' + str(req.status_code) +
' when getting user info from Intercom')
return None
if u['name']:
known_as = u['name']
else:
known_as = u['email']
info['name'] = ("<https://app.intercom.io/a/apps/" + appid + "/users/" + u['id'] + "|" + known_as + ">")
companies = ''
for company in u['companies']['companies']:
# There's some Company records in Intercom that have an id but no name.
if 'name' in company:
companies = companies + company['name'] + ' '
if companies == '':
info['company'] = u['email']
else:
info['company'] = ("<https://app.intercom.io/a/apps/" + appid + "/companies/" +
company['id'] + "|" + companies[:-1] + ">")
return info
except:
failmail(email, 'Failure on processing Intercom user info:\n' + traceback.format_exc())
return None
def clean_up(body):
# Once in a while we get a "null" body.
if body:
body = body.replace('<br>', '\n')
body = body.replace('</p><p>', '\n')
body = body.replace('<p>', '')
soup = BeautifulSoup(body)
return soup.get_text()
else:
return ""
def intercom_parse(notification):
try:
if notification['topic'] == 'conversation.admin.replied':
for part in notification['data']['item']['conversation_parts']['conversation_parts']:
part['body'] = clean_up(part['body'])
uinfo = user_info(notification['data']['item']['user']['id'])
if uinfo:
message = (part['author']['name'] + " replied to <" +
notification['data']['item']['links']['conversation_web'] + "|a conversation> with " +
uinfo['name'] + " (" +
uinfo['company'] + ")\n" +
part['body'])
return({"text": message, "color": "ffce49"})
else:
return None
elif notification['topic'] == 'conversation.user.replied':
for part in notification['data']['item']['conversation_parts']['conversation_parts']:
part['body'] = clean_up(part['body'])
uinfo = user_info(part['author']['id'])
if uinfo:
message = (uinfo['name'] + " (" +
uinfo['company'] + ") replied to <" +
notification['data']['item']['links']['conversation_web'] + "|a conversation> with " +
notification['data']['item']['assignee']['name'] + '\n' +
part['body'])
return({"text": message, "color": "1414ff"})
else:
return None
elif notification['topic'] in [ 'conversation.admin.opened', 'conversation.admin.closed' ]:
operation = string.split(notification['topic'], '.')[2]
for part in notification['data']['item']['conversation_parts']['conversation_parts']:
part['body'] = clean_up(part['body'])
uinfo = user_info(notification['data']['item']['user']['id'])
if uinfo:
message = (part['author']['name'] + " " + operation + " <" +
notification['data']['item']['links']['conversation_web'] + "|a conversation> with " +
uinfo['name'] + " (" +
uinfo['company'] + ")\n" +
part['body'])
return({"text": message, "color": "ffce49"})
else:
return None
elif notification['topic'] == 'conversation.admin.closed':
for part in notification['data']['item']['conversation_parts']['conversation_parts']:
part['body'] = clean_up(part['body'])
uinfo = user_info(notification['data']['item']['user']['id'])
if uinfo:
message = (part['author']['name'] + " closed <" +
notification['data']['item']['links']['conversation_web'] + "|a conversation> with " +
uinfo['name'] + " (" +
uinfo['company'] + ")\n" +
part['body'])
return({"text": message, "color": "ffce49"})
else:
return None
elif notification['topic'] == 'conversation.admin.assigned':
for part in notification['data']['item']['conversation_parts']['conversation_parts']:
part['body'] = clean_up(part['body'])
uinfo = user_info(notification['data']['item']['user']['id'])
if uinfo:
message = (part['author']['name'] + " assigned <" +
notification['data']['item']['links']['conversation_web'] + "|a conversation> with " +
uinfo['name'] + " (" +
uinfo['company'] +
") to " + part['assigned_to']['name'] + "\n" +
part['body'])
return({"text": message, "color": "ffce49"})
else:
return None
elif notification['topic'] == 'conversation.user.created':
part = notification['data']['item']['conversation_message']
part['body'] = clean_up(part['body'])
uinfo = user_info(part['author']['id'])
if uinfo:
message = (uinfo['name'] + " (" +
uinfo['company'] + ") started a new <" +
notification['data']['item']['links']['conversation_web'] + "|conversation> with " +
notification['data']['item']['assignee']['name'] + '\n' +
part['body'])
return({"text": message, "color": "1414ff"})
else:
return None
elif notification['topic'] == 'user.created':
uinfo = user_info(notification['data']['item']['id'])
if uinfo:
message = ('Intercom user ' + uinfo['name'] + " (" +
uinfo['company'] + ") was created")
return({"text": message, "color": "1414ff"})
else:
return None
elif notification['topic'] == 'conversation.admin.noted':
for part in notification['data']['item']['conversation_parts']['conversation_parts']:
part['body'] = clean_up(part['body'])
uinfo = user_info(notification['data']['item']['user']['id'])
if uinfo:
message = (part['author']['name'] + " added <" +
notification['data']['item']['links']['conversation_web'] + "|an internal note> to <" +
notification['data']['item']['links']['conversation_web'] + "|a conversation> with " +
uinfo['name'] + " (" +
uinfo['company'] + ")\n" +
part['body'])
return({"text": message, "color": "ffce49"})
else:
return None
else:
failmail(email, 'Received an unsupported Intercom notification type:\n' + notification['topic'])
return None
except:
failmail(email, 'Failure parsing Intercom notification:\n' + traceback.format_exc())
return None
def slacksend_channel(message, channel_name):
try:
args = copy.deepcopy(slackauth)
args['name'] = channel_name
logger.info('Joining channel on Slack:\n' + json.dumps(args, sort_keys=True, indent=4))
req = session.post("https://slack.com/api/channels.join", data=args)
if req.status_code == 200:
resp = req.json()
logger.info('Response from Slack channel join:\n' + json.dumps(resp, sort_keys=True, indent=4))
channel = req.json()
else:
failmail(email, 'Unexpected response code ' + str(req.status_code) + ' when joining Slack channel', copy_to_slack=False)
return False
args = copy.deepcopy(slackauth)
args['channel'] = channel['channel']['id']
args['username'] = 'Intercom'
# If the message is too big, Slack will send us a response code 414. Keep chopping the message in
# half until it goes through.
msglen = len(message['text'])
trailer = ''
msgsent = False
while not msgsent:
att = [{ "fallback": message['text'][:msglen] + trailer,
"text": message['text'][:msglen] + trailer,
"color": message['color'] }]
args['attachments'] = json.dumps(att)
logger.info('Posting message to Slack:\n' + json.dumps(args, sort_keys=True, indent=4))
req = session.post("https://slack.com/api/chat.postMessage", params=args)
if req.status_code == 200:
resp = req.json()
logger.info('Response from Slack message post:\n' + json.dumps(resp, sort_keys=True, indent=4))
return resp['ok']
elif req.status_code == 414 or req.status_code == 413:
logger.info('Response code 414 from Slack message post. Cutting in half and trying again.\n')
trailer = '\n\n[This message was too long to relay to Slack. You\'ll have to click to Intercom to see the whole thing.]\n'
msglen /= 2
time.sleep(1)
else:
failmail(email, 'Unexpected response code ' + str(req.status_code) +
' when posting message to Slack', copy_to_slack=False)
return False
# Always sleep an extra second, as the Slack API says it will cut us off if we send more frequently
# than once per second.
time.sleep(1)
except:
failmail(email, 'Failure posting message to Slack:\n' + traceback.format_exc(), copy_to_slack=False)
return False
@app.route('/intercom', methods=['POST'])
def process_notification():
try:
notification = request.get_json(force=True)
logger.info("Intercom notification received:\n" + json.dumps(notification, sort_keys=True, indent=4))
message = intercom_parse(notification)
if message:
if slacksend_channel(message, slackchannel):
logger.info('Successfully relayed a parsed Intercom message to Slack')
return("OK")
else:
logger.info('Something went wrong when trying to send to Slack')
else:
logger.info('Was not able to parse the Intercom notification into a sendable message')
except:
failmail(email, 'General failure processing Intercom notification:\n' + traceback.format_exc())
cmdline_args = parse_args()
port = int(cmdline_args.port)
appid = cmdline_args.appid
inttoken = cmdline_args.inttoken
email = cmdline_args.email
slackauth = { 'token': cmdline_args.slacktoken }
slackchannel = cmdline_args.channel
backupchannel = cmdline_args.backupchannel
logger = prep_logging('intslack', 'intslack.log')
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + inttoken
}
session = requests.Session()
if __name__ == '__main__':
startup_msg = {
"text": "Custom <https://github.com/philrz/intercom-slack-relay|intercom-slack-relay> starting up\nMaintained by <mailto:" + email + "|" + email + ">\nCheck #" + backupchannel + " for any messages that may have been missed while relay was offline",
"color": "danger"
}
slacksend_channel(startup_msg, slackchannel)
app.run(host='0.0.0.0', port=port)