-
Notifications
You must be signed in to change notification settings - Fork 0
/
rip_that_data_back_from_them.php
352 lines (302 loc) · 10.6 KB
/
rip_that_data_back_from_them.php
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
use Intercom\IntercomClient;
require_once(__DIR__.'/vendor/autoload.php');
require_once(__DIR__.'/database_wrapper.php');
ini_set('display_errors', 1);
// Get your config
$config = require_once(__DIR__.'/config.php');
// Get a PDO instance ready for ya
$db = new Database('mysql:host='.$config['db_connection']['host'].';port='.$config['db_connection']['port'].';dbname='.$config['db_connection']['database'].';charset=UTF8', $config['db_connection']['username'], $config['db_connection']['password'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// Get your client all nicely setup
$client = new IntercomClient($config['intercom_access_token']);
$resp = $client->users->scrollUsers([]);
$count = 1;
while (!empty($resp->scroll_param) && count($resp->users) > 0) {
echo "USERS PAGE $count: " . count($resp->users) . "\n";
$count = ++$count;
foreach($resp->users as $user) {
$user_check = $db->fetchField("SELECT COUNT(*) FROM users WHERE intercom_id = ?", [ $user->id ]);
if(!empty($user_check)) {
echo "Duplicate user {$user->email} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO users SET
intercom_id = ?,
email = ?,
phone = ?,
name = ?,
pseudonym = ?,
app_id = ?,
referrer = ?
", [
$user->id,
$user->email,
$user->phone,
$user->name,
$user->pseudonym,
$user->app_id,
$user->referrer
]);
}
$resp = $client->users->scrollUsers(["scroll_param" => $resp->scroll_param]);
}
$resp = $client->admins->getAdmins([]);
foreach($resp->admins as $admin) {
$user_check = $db->fetchField("SELECT COUNT(*) FROM admins WHERE intercom_id = ?", [ $admin->id ]);
if(!empty($user_check)) {
echo "Duplicate admin {$admin->email} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO admins SET
intercom_id = ?,
email = ?,
name = ?,
team_id = ?
", [
$admin->id,
$admin->email,
$admin->name,
join(',', $admin->team_ids)
]);
}
$resp = $client->leads->scrollLeads([]);
$count = 1;
while (!empty($resp->scroll_param) && count($resp->contacts) > 0) {
echo "LEADS PAGE $count: " . count($resp->contacts) . "\n";
$count = ++$count;
foreach($resp->contacts as $user) {
$user_check = $db->fetchField("SELECT COUNT(*) FROM leads WHERE intercom_id = ? AND intercom_user_id = ?", [ $user->id, $user->user_id ]);
if(!empty($user_check)) {
echo "Duplicate user {$user->email} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO leads SET
intercom_id = ?,
intercom_user_id = ?,
email = ?,
phone = ?,
name = ?,
pseudonym = ?
", [
$user->id,
$user->user_id,
utf8_encode($user->email),
$user->phone,
utf8_encode($user->name),
$user->pseudonym
]);
}
$resp = $client->leads->scrollLeads(["scroll_param" => $resp->scroll_param]);
}
$conv_resp = $client->conversations->getConversations([]);
$count = 1;
while(count($conv_resp->conversations) > 0) {
echo "CONVERSATIONS PAGE $count: " . count($conv_resp->conversations) . "\n";
++$count;
foreach($conv_resp->conversations as $conversation) {
// start with the conversation parent element
$conversation_check = $db->fetchField("SELECT COUNT(*) FROM conversations WHERE intercom_id = ?", [ $conversation->id ]);
if(!empty($conversation_check)) {
echo "Duplicate conversation {$conversation->id} - SKIPPING\n";
continue;
}
$customer_reply_url = $conversation->customer_first_reply->url ?? '';
$db->execute("INSERT INTO conversations SET
intercom_id = ?,
created_at = ?,
updated_at = ?,
initiated_from_url = ?,
assigned_to_type = ?,
assigned_to_id = ?
", [
$conversation->id,
date('Y-m-d H:i:s', $conversation->created_at),
($conversation->updated_at ? date('Y-m-d H:i:s', $conversation->updated_at) : null),
$customer_reply_url,
$conversation->assignee->type,
$conversation->assignee->id
]);
$conversation_id = $db->lastInsertId();
echo " CONVERSATION ID: {$conversation_id}\n";
// pull out the tags
if(count($conversation->tags->tags)) {
echo " CONVERSATION TAGS: " . count($conversation->tags->tags) . "\n";
foreach($conversation->tags->tags as $tag) {
$tag_check = $db->fetchField("SELECT COUNT(*) FROM conversation_tags WHERE conversation_id = ? AND tag_intercom_id = ?", [ $conversation_id, $tag->id ]);
if(!empty($tag_check)) {
echo " Duplicate conversation tag {$tag->id} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO conversation_tags SET
conversation_id = ?,
tag_intercom_id = ?,
name = ?,
applied_at = ?,
applied_by_type = ?,
applied_by_id = ?
", [
$conversation_id,
$tag->id,
utf8_encode($tag->name),
date('Y-m-d H:i:s', $tag->applied_at),
$tag->applied_by->type,
$tag->applied_by->id
]);
}
}
// get all assigned people to the conversation
if(count($conversation->customers)) {
echo " CONVERSATION CUSTOMERS: " . count($conversation->customers) . "\n";
foreach($conversation->customers as $customer) {
$customer_check = $db->fetchField("SELECT COUNT(*) FROM conversation_customers WHERE conversation_id = ? AND customer_id = ?", [ $conversation_id, $customer->id ]);
if(!empty($customer_check)) {
echo " Duplicate conversation customer {$customer->id} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO conversation_customers SET
conversation_id = ?,
customer_type = ?,
customer_id = ?
", [
$conversation_id,
$customer->type,
$customer->id,
]);
}
}
// rip through any attachments attached to the main part of the conversation
if(count($conversation->conversation_message->attachments)) {
echo " CONVERSATION ATTACHMENTS: " . count($conversation->conversation_message->attachments) . "\n";
foreach($conversation->conversation_message->attachments as $attachment) {
// use this as we aren't given a unique file id identifier...
$unique_filename_hash = hash('sha256', $attachment->name);
$attachment_check = $db->fetchField("SELECT COUNT(*) FROM conversation_attachments WHERE conversation_id = ? AND unique_filename_hash = ?", [ $conversation_id, $unique_filename_hash ]);
if(!empty($attachment_check)) {
echo " Duplicate conversation attachment {$attachment->name} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO conversation_attachments SET
conversation_id = ?,
unique_filename_hash = ?,
attached_by_type = ?,
attached_by_id = ?,
type = ?,
name = ?,
url = ?,
content = ?,
content_type = ?,
filesize = ?,
width = ?,
height = ?
", [
$conversation_id,
$unique_filename_hash,
$conversation->conversation_message->author->type,
$conversation->conversation_message->author->id,
$attachment->type,
utf8_encode($attachment->name),
$attachment->url,
file_get_contents($attachment->url),
$attachment->content_type,
$attachment->filesize,
$attachment->width,
$attachment->height,
]);
}
}
// now we rip through all the conversation parts.
$part_resp = $client->conversations->getConversation($conversation->id);
echo " CONVERSATION PARTS: " . count($part_resp->conversation_parts->conversation_parts) . "\n";
if(isset($part_resp->conversation_parts->conversation_parts) && count($part_resp->conversation_parts->conversation_parts)) {
foreach($part_resp->conversation_parts->conversation_parts as $part) {
$part_check = $db->fetchField("SELECT COUNT(*) FROM conversation_parts WHERE conversation_id = ? AND intercom_id = ?", [ $conversation_id, $part->id ]);
if(!empty($part_check)) {
echo " Duplicate conversation part {$part->id} - SKIPPING\n";
continue;
}
$subject = isset($part->subject) ? $part->subject : '';
$assigned_to_type = isset($part->assigned_to) && is_array($part->assigned_to) && count($part->assigned_to) ? $part->assigned_to->type : '';
$assigned_to_id = isset($part->assigned_to) && is_array($part->assigned_to) && count($part->assigned_to) ? $part->assigned_to->id : '';
$db->execute("INSERT INTO conversation_parts SET
conversation_id = ?,
intercom_id = ?,
created_at = ?,
updated_at = ?,
assigned_to_type = ?,
assigned_to_id = ?,
author_type = ?,
author_id = ?,
subject = ?,
body = ?
", [
$conversation_id,
$part->id,
date('Y-m-d H:i:s', $part->created_at),
($part->updated_at ? date('Y-m-d H:i:s', $part->updated_at) : null),
$assigned_to_type,
$assigned_to_id,
$part->author->type,
$part->author->id,
utf8_encode($subject),
utf8_encode($part->body)
]);
$conversation_part_id = $db->lastInsertId();
echo " CONVERSATION PART ID: {$conversation_part_id}\n";
// rip through any attachments attached to the main part of the conversation
if(count($part->attachments)) {
echo " CONVERSATION PART ATTACHMENTS: " . count($part->attachments) . "\n";
foreach($part->attachments as $attachment) {
// use this as we aren't given a unique file id identifier...
$unique_filename_hash = hash('sha256', $attachment->name);
$attachment_check = $db->fetchField("SELECT COUNT(*) FROM conversation_part_attachments WHERE conversation_part_id = ? AND unique_filename_hash = ?", [ $conversation_part_id, $unique_filename_hash ]);
if(!empty($attachment_check)) {
echo " Duplicate conversation attachment {$attachment->name} - SKIPPING\n";
continue;
}
$db->execute("INSERT INTO conversation_part_attachments SET
conversation_part_id = ?,
unique_filename_hash = ?,
attached_by_type = ?,
attached_by_id = ?,
type = ?,
name = ?,
url = ?,
content = ?,
content_type = ?,
filesize = ?,
width = ?,
height = ?
", [
$conversation_part_id,
$unique_filename_hash,
$conversation->conversation_message->author->type,
$conversation->conversation_message->author->id,
$attachment->type,
utf8_encode($attachment->name),
$attachment->url,
file_get_contents($attachment->url),
$attachment->content_type,
$attachment->filesize,
$attachment->width,
$attachment->height,
]);
}
}
}
}
}
if(isset($conv_resp->pages->next) && $conv_resp->pages->next) {
$conv_resp = $client->nextPage($conv_resp->pages);
} else {
$conv_resp->conversations = [];
}
}
echo "CONVERSATIONS COMPLETED\n";
echo "DATA RIPPED BACK, PROCESS COMPLETE. THANKS FOR USING IT!\n\n";
exit(0);