-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintercom-conversations.py
289 lines (268 loc) · 11 KB
/
intercom-conversations.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
# ---
# name: intercom-conversations
# deployed: true
# config: index
# title: Intercom Conversations
# description: Returns a list of conversations from Intercom
# params:
# - name: properties
# type: array
# description: The properties to return (defaults to all properties). See "Returns" for a listing of the available properties.
# required: false
# - name: filter
# type: string
# description: Filter to apply with key/values specified as a URL query string where the keys correspond to the properties to filter.
# required: false
# returns:
# - name: id
# type: string
# description: The id of conversation
# - name: created_at
# type: string
# description: The time the conversation was created
# - name: updated_at
# type: string
# description: The last time the conversation was updated
# - name: waiting_since
# type: string
# description: The last time a contact responded to an admin
# - name: snoozed_until
# type: string
# description: The time in the future when the conversation will be marked as open
# - name: source_type
# type: string
# description: The type of conversation
# - name: source_id
# type: string
# description: The id of the conversation
# - name: source_delivered_as
# type: string
# description: The message subject
# - name: source_subject
# type: string
# description: The message subject
# - name: source_body
# type: string
# description: The message body
# - name: source_author_type
# type: string
# description: The type of author who intiated the conversation
# - name: source_author_id
# type: string
# description: The id of the author who initiated the conversation
# - name: source_author_name
# type: string
# description: The name of the author who initiated the conversation
# - name: source_author_email
# type: string
# description: The email address of the author who initiated the conversation
# - name: source_url
# type: string
# description: The url where the conversation was started
# - name: first_contact_reply_created_at
# type: string
# description: The time the contact replied
# - name: first_contact_reply_type
# type: string
# description: The channel which the first reply occured over
# - name: first_contact_reply_url
# type: string
# description: The URL where the first reply originated from
# - name: assignee_type
# type: string
# description: The type of the assignee to the conversation
# - name: assignee_id
# type: string
# description: The id of the assignee to the conversation
# - name: open
# type: boolean
# description: Whether a conversation is open or closed
# - name: state
# type: string
# description: State of the conversation
# - name: read
# type: boolean
# description: Whether a conversation as been read or not
# - name: priority
# type: string
# description: Whether a conversation is a priority
# - name: sla_applied
# type: string
# description: The SLA applied to the conversation
# - name: time_to_assignment
# type: integer
# description: Duration until last assignment before first admin reply
# - name: time_to_admin_reply
# type: integer
# description: Duration until first admin reply
# - name: time_to_first_close
# type: integer
# description: Duration until conversation was first closed
# - name: time_to_last_close
# type: integer
# description: Duration until conversation was last closed
# - name: median_time_to_reply
# type: integer
# description: Median based on all admin replies after a contact reply
# - name: first_contact_reply_at
# type: string
# description: Time of first text conversation part from a contact
# - name: first_assignment_at
# type: string
# description: Time of first assignment after first_contact_reply_at
# - name: first_admin_reply_at
# type: string
# description: Time of first admin reploy after first_contact_reply_at
# - name: first_close_at
# type: string
# description: Time of first close after first_contact_reply_at
# - name: last_assignment_at
# type: string
# description: Time of last assignment after first_contact_reply_at
# - name: last_assignment_admin_reply_at
# type: string
# description: Time of last assignment before first_admin_reply_at
# - name: last_contact_reply_at
# type: string
# description: Time of the last conversation part from a contact
# - name: last_admin_reply_at
# type: string
# description: Time of the last conversation part from an admin
# - name: last_close_at
# type: string
# description: Time of the last conversation close
# - name: last_closed_by_id
# type: string
# description: The id of the admin who closed the conversation
# - name: count_reopens
# type: integer
# description: The number of reopens after first_contact_reply_at
# - name: count_assignments
# type: integer
# description: The number of assignments after first_contact_reply_at
# - name: count_conversation_parts
# type: integer
# description: The total number of conversation parts
# examples:
# - '""'
# - '"id, source_subject, state, created_at"'
# ---
import json
import urllib
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from datetime import *
from decimal import *
from collections import OrderedDict
# main function entry point
def flexio_handler(flex):
flex.output.content_type = 'application/x-ndjson'
for data in get_data(flex.vars):
flex.output.write(data)
def get_data(params):
# get the api key from the variable input
auth_token = dict(params).get('intercom_connection',{}).get('access_token')
# see here for more info:
# https://developers.intercom.com/intercom-api-reference/reference#conversation-model
# https://developers.intercom.com/intercom-api-reference/reference#pagination
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + auth_token,
'Intercom-Version': '2.0' # api version
}
url = 'https://api.intercom.io/conversations'
page_size = 50
url_query_params = {"per_page": page_size}
url_query_str = urllib.parse.urlencode(url_query_params)
page_url = url + '?' + url_query_str
while True:
response = requests_retry_session().get(page_url, headers=headers)
response.raise_for_status()
content = response.json()
data = content.get('conversations',[])
if len(data) == 0: # sanity check in case there's an issue with cursor
break
buffer = ''
for item in data:
item = get_item_info(item)
buffer = buffer + json.dumps(item, default=to_string) + "\n"
yield buffer
page_url = content.get('pages',{}).get('next')
if page_url is None:
break
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(429, 500, 502, 503, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def to_date(ts):
if ts is None or ts == '':
return ''
return datetime.utcfromtimestamp(int(ts)/1000).strftime('%Y-%m-%dT%H:%M:%S')
def to_string(value):
if isinstance(value, (date, datetime)):
return value.isoformat()
if isinstance(value, (Decimal)):
return str(value)
return value
def get_item_info(item):
# map this function's property names to the API's property names
info = OrderedDict()
info['id'] = item.get('id')
info['created_at'] = to_date(item.get('created_at'))
info['updated_at'] = to_date(item.get('updated_at'))
info['waiting_since'] = to_date(item.get('waiting_since'))
info['snoozed_until'] = to_date(item.get('waiting_since'))
info['source_type'] = item.get('source',{}).get('type')
info['source_id'] = item.get('source',{}).get('id')
info['source_delivered_as'] = item.get('source',{}).get('delivered_as')
info['source_subject'] = item.get('source',{}).get('subject')
info['source_body'] = item.get('source',{}).get('body')
info['source_author_type'] = item.get('source',{}).get('author',{}).get('type')
info['source_author_id'] = item.get('source',{}).get('author',{}).get('id')
info['source_author_name'] = item.get('source',{}).get('author',{}).get('name')
info['source_author_email'] = item.get('source',{}).get('author',{}).get('email')
info['source_url'] = item.get('source',{}).get('url')
info['first_contact_reply_created_at'] = to_date(item.get('first_contact_reply',{}).get('created_at'))
info['first_contact_reply_type'] = item.get('first_contact_reply',{}).get('type')
info['first_contact_reply_url'] = item.get('first_contact_reply',{}).get('url')
info['assignee_type'] = item.get('assignee',{}).get('type')
info['assignee_id'] = item.get('assignee',{}).get('id')
info['open'] = item.get('open')
info['state'] = item.get('state')
info['read'] = item.get('read')
info['priority'] = item.get('priority')
info['sla_applied'] = item.get('sla_applied')
info['time_to_assignment'] = item.get('statistics',{}).get('time_to_assignment')
info['time_to_admin_reply'] = item.get('statistics',{}).get('time_to_admin_reply')
info['time_to_first_close'] = item.get('statistics',{}).get('time_to_first_close')
info['time_to_last_close'] = item.get('statistics',{}).get('time_to_last_close')
info['median_time_to_reply'] = item.get('statistics',{}).get('median_time_to_reply')
info['first_contact_reply_at'] = to_date(item.get('statistics',{}).get('first_contact_reply_at'))
info['first_assignment_at'] = to_date(item.get('statistics',{}).get('first_assignment_at'))
info['first_admin_reply_at'] = to_date(item.get('statistics',{}).get('first_admin_reply_at'))
info['first_close_at'] = to_date(item.get('statistics',{}).get('first_close_at'))
info['last_assignment_at'] = to_date(item.get('statistics',{}).get('last_assignment_at'))
info['last_assignment_admin_reply_at'] = to_date(item.get('statistics',{}).get('last_assignment_admin_reply_at'))
info['last_contact_reply_at'] = to_date(item.get('statistics',{}).get('last_contact_reply_at'))
info['last_admin_reply_at'] = to_date(item.get('statistics',{}).get('last_admin_reply_at'))
info['last_close_at'] = to_date(item.get('statistics',{}).get('last_close_at'))
info['last_closed_by_id'] = item.get('statistics',{}).get('last_closed_by_id')
info['count_reopens'] = item.get('statistics',{}).get('count_reopens')
info['count_assignments'] = item.get('statistics',{}).get('count_assignments')
info['count_conversation_parts'] = item.get('statistics',{}).get('count_conversation_parts')
return info