/
conversation.ts
754 lines (628 loc) · 24.6 KB
/
conversation.ts
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import { EventEmitter } from 'events';
import * as HttpStatus from 'http-status-codes';
import updateIn from 'simple-update-in';
import BotEmulator from '../botEmulator';
import Activity from '../types/activity/activity';
import ContactRelationUpdateActivity from '../types/activity/contactRelationUpdate';
import ConversationUpdateActivity from '../types/activity/conversationUpdate';
import EventActivity from '../types/activity/event';
import GenericActivity from '../types/activity/generic';
import InvokeActivity from '../types/activity/invoke';
import MessageActivity from '../types/activity/message';
import Attachment from '../types/attachment';
import ErrorCodes from '../types/errorCodes';
import LogLevel from '../types/log/level';
import { appSettingsItem, externalLinkItem, textItem } from '../types/log/util';
import PaymentAddress from '../types/payment/address';
import CheckoutConversationSession from '../types/payment/checkoutConversationSession';
import PaymentOperations from '../types/payment/operations';
import PaymentRequest from '../types/payment/request';
import PaymentRequestComplete from '../types/payment/requestComplete';
import PaymentRequestUpdate from '../types/payment/requestUpdate';
import ResourceResponse from '../types/response/resource';
import TranscriptRecord from '../types/transcriptRecord';
import User from '../types/user';
import { TokenCache } from '../userToken/tokenCache';
import createAPIException from '../utils/createResponse/apiException';
import createResourceResponse from '../utils/createResponse/resource';
import isLocalhostUrl from '../utils/isLocalhostUrl';
import OAuthClientEncoder from '../utils/oauthClientEncoder';
import PaymentEncoder from '../utils/paymentEncoder';
import uniqueId from '../utils/uniqueId';
import BotEndpoint from './botEndpoint';
// moment currently does not export callable function
const moment = require('moment');
const maxDataUrlLength = Math.pow(2, 22);
interface ActivityBucket {
activity: Activity;
watermark: number;
}
/**
* Stores and propagates conversation messages.
*/
export default class Conversation extends EventEmitter {
public botEmulator: BotEmulator;
public botEndpoint: BotEndpoint;
public conversationId: string;
public user: User;
// flag indicating if the user has been shown the
// "please don't use default Bot State API" warning message
// when they try to write bot state data
public stateApiDeprecationWarningShown: boolean = false;
public codeVerifier: string = undefined;
// private speechToken: SpeechTokenInfo;
public members: User[] = [];
public nextWatermark = 0;
// the list of activities in this conversation
private activities: ActivityBucket[] = [];
private transcript: TranscriptRecord[] = [];
private get conversationIsTranscript() {
return this.conversationId.includes('transcript');
}
constructor(botEmulator: BotEmulator, botEndpoint: BotEndpoint, conversationId: string, user: User) {
super();
Object.assign(this, { botEmulator, botEndpoint, conversationId, user });
// We should consider hardcoding bot id because we don't really use it
this.members.push({ id: botEndpoint && botEndpoint.botId || 'bot-1', name: 'Bot' });
this.members.push({ id: user.id, name: user.name });
}
/**
* Sends the activity to the conversation's bot.
*/
async postActivityToBot(activity: Activity, recordInConversation: boolean) {
if (!this.botEndpoint) {
return this.botEmulator.facilities.logger.logMessage(this.conversationId,
textItem(LogLevel.Error,
`This conversation does not have an endpoint, cannot post "${ activity && activity.type }" activity.`));
}
// Do not make a shallow copy here before modifying
activity = this.postage(this.botEndpoint.botId, activity);
activity.from = activity.from || this.user;
activity.locale = this.botEmulator.facilities.locale;
if (!activity.recipient.name) {
activity.recipient.name = 'Bot';
}
// Fill in role field, if missing
if (!activity.recipient.role) {
activity.recipient.role = 'bot';
}
activity.serviceUrl = this.botEmulator.getServiceUrl(this.botEndpoint.botUrl);
if (!this.conversationIsTranscript && !isLocalhostUrl(this.botEndpoint.botUrl) &&
isLocalhostUrl(this.botEmulator.getServiceUrl(this.botEndpoint.botUrl))) {
this.botEmulator.facilities.logger.logMessage(this.conversationId,
textItem(LogLevel.Error,
'Error: The bot is remote, but the service URL is localhost.' +
' Without tunneling software you will not receive replies.'));
this.botEmulator.facilities.logger.logMessage(this.conversationId,
externalLinkItem('Connecting to bots hosted remotely', 'https://aka.ms/cnjvpo'));
this.botEmulator.facilities.logger.logMessage(this.conversationId,
appSettingsItem('Configure ngrok'));
}
const options = {
body: JSON.stringify(activity),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
};
if (recordInConversation) {
this.addActivityToQueue({ ...activity });
}
this.transcript = [...this.transcript, { type: 'activity add', activity }];
this.emit('transcriptupdate');
let status = 200;
let resp: any = { json: async () => ({}) };
// If a message to the bot was triggered from a transcript, don't actually send it.
// This can happen when clicking a button in an adaptive card, for instance.
if (!this.conversationIsTranscript) {
resp = await this.botEndpoint.fetchWithAuth(this.botEndpoint.botUrl, options);
status = resp.status;
}
return {
activityId: activity.id,
response: resp,
statusCode: status
};
}
async sendConversationUpdate(membersAdded: User[], membersRemoved: User[]) {
const activity: ConversationUpdateActivity = {
type: 'conversationUpdate',
membersAdded,
membersRemoved
};
try {
await this.postActivityToBot(activity, false);
} catch (err) {
this.botEmulator.facilities.logger.logException(this.conversationId, err);
}
}
/**
* Queues activity for delivery to user.
*/
public postActivityToUser(activity: Activity, isHistoric: boolean = false): ResourceResponse {
activity = this.processActivity(activity);
activity = this.postage(this.user.id, activity, isHistoric);
if (!activity.from.name) {
activity.from.name = 'Bot';
}
if (!activity.locale) {
activity.locale = this.botEmulator.facilities.locale;
}
// Fill in role field, if missing
if (!activity.recipient.role) {
activity.recipient.role = 'user';
}
this.addActivityToQueue(activity);
this.transcript = [...this.transcript, { type: 'activity add', activity }];
this.emit('transcriptupdate');
if (activity.type === 'endOfConversation') {
this.emit('end');
}
return createResourceResponse(activity.id);
}
// TODO: Payment modification is only useful for emulator, but not local mode
// This function turns all payment cardAction into openUrl to payment://
public processActivity(activity: Activity): Activity {
const visitors = [new PaymentEncoder(), new OAuthClientEncoder(activity)];
activity = { ...activity };
visitors.forEach(v => v.traverseActivity(activity));
return activity;
}
// This function turns local contentUrls into dataUrls://
public async processActivityForDataUrls(activity: Activity): Promise<Activity> {
const visitor = new DataUrlEncoder(this.botEmulator);
activity = { ...activity };
await visitor.traverseActivity(activity);
return activity;
}
// updateActivity with replacement
public updateActivity(updatedActivity: Activity): ResourceResponse {
const { id } = updatedActivity;
const index = this.activities.findIndex(entry => entry.activity.id === id);
if (index === -1) {
// The activity may already flushed to the client, thus, not found anymore
// TODO: Should we add a new activity with same ID, so the client will get the update?
throw createAPIException(HttpStatus.NOT_FOUND, ErrorCodes.BadArgument, 'not a known activity id');
}
this.activities = updateIn(this.activities, [index, 'activity'], activity => ({ ...activity, ...updatedActivity }));
updatedActivity = this.activities[index].activity;
this.emit('activitychange', { activity: updatedActivity });
this.transcript = [...this.transcript, { type: 'activity update', activity: updatedActivity }];
this.emit('transcriptupdate');
return createResourceResponse(id);
}
public deleteActivity(id: string) {
// if we found the activity to reply to
const activityIndex = this.activities.findIndex(entry => entry.activity.id === id);
if (activityIndex === -1) {
// The activity may already flushed to the client
// TODO: Should we add a new empty activity with same ID to let the client know about the recall?
throw createAPIException(HttpStatus.NOT_FOUND, ErrorCodes.BadArgument, 'The activity id was not found');
}
const { activity } = this.activities[activityIndex];
this.activities = updateIn(this.activities, [activityIndex]);
this.emit('deleteactivity', { activity });
this.transcript = [...this.transcript, { type: 'activity delete', activity: { id } }];
this.emit('transcriptupdate');
}
public async addMember(id: string, name: string): Promise<User> {
name = name || `user-${ uniqueId() }`;
id = id || uniqueId();
const user = { name, id };
this.members = [...this.members, user];
this.emit('join', { user });
// TODO: A conversation without endpoint is weird, think about it
// Don't send "conversationUpdate" if we don't have an endpoint
if (this.botEndpoint) {
await this.sendConversationUpdate([user], undefined);
}
this.transcript = [...this.transcript, {
type: 'member join', activity: {
type: 'conversationUpdate',
membersAdded: [user]
} as ConversationUpdateActivity
}];
this.emit('transcriptupdate');
return user;
}
public async removeMember(id: string) {
const index = this.members.findIndex(val => val.id === id);
if (index !== -1) {
const user = this.members[index];
this.members = updateIn(this.members, [index]);
this.emit('left', { user });
}
await this.sendConversationUpdate(undefined, [{ id, name: undefined }]);
this.transcript = [...this.transcript, {
type: 'member left', activity: {
type: 'conversationUpdate',
membersRemoved: [{ id }]
} as ConversationUpdateActivity
}];
this.emit('transcriptupdate');
}
public async sendContactAdded() {
const activity: ContactRelationUpdateActivity = {
type: 'contactRelationUpdate',
action: 'add'
};
try {
await this.postActivityToBot(activity, false);
} catch (err) {
this.botEmulator.facilities.logger.logException(this.conversationId, err);
}
this.transcript = [...this.transcript, { type: 'contact update', activity }];
this.emit('transcriptupdate');
}
public async sendContactRemoved() {
const activity: ContactRelationUpdateActivity = {
type: 'contactRelationUpdate',
action: 'remove'
};
try {
await this.postActivityToBot(activity, false);
} catch (err) {
this.botEmulator.facilities.logger.logException(this.conversationId, err);
}
this.transcript = [...this.transcript, { type: 'contact remove', activity }];
this.emit('transcriptupdate');
}
public async sendTyping() {
// TODO: Which side is sending the typing activity?
const activity: Activity = {
type: 'typing'
};
try {
await this.postActivityToBot(activity, false);
} catch (err) {
this.botEmulator.facilities.logger.logException(this.conversationId, err);
}
this.transcript = [...this.transcript, { type: 'typing', activity }];
this.emit('transcriptupdate');
}
public async sendPing() {
// TODO: Which side is sending the ping activity?
const activity: Activity = {
type: 'ping'
};
try {
await this.postActivityToBot(activity, false);
} catch (err) {
this.botEmulator.facilities.logger.logException(this.conversationId, err);
}
this.transcript = [...this.transcript, { type: 'ping', activity }];
this.emit('transcriptupdate');
}
public async sendDeleteUserData() {
const activity: Activity = {
type: 'deleteUserData'
};
try {
await this.postActivityToBot(activity, false);
} catch (err) {
this.botEmulator.facilities.logger.logException(this.conversationId, err);
}
this.transcript = [...this.transcript, { type: 'user data delete', activity }];
this.emit('transcriptupdate');
}
public async sendUpdateShippingAddressOperation(
checkoutSession: CheckoutConversationSession,
request: PaymentRequest,
shippingAddress: PaymentAddress,
shippingOptionId: string
) {
return await this.sendUpdateShippingOperation(
checkoutSession,
PaymentOperations.UpdateShippingAddressOperationName,
request,
shippingAddress,
shippingOptionId,
);
}
public async sendUpdateShippingOptionOperation(
checkoutSession: CheckoutConversationSession,
request: PaymentRequest,
shippingAddress: PaymentAddress,
shippingOptionId: string
) {
return await this.sendUpdateShippingOperation(
checkoutSession,
PaymentOperations.UpdateShippingOptionOperationName,
request,
shippingAddress,
shippingOptionId
);
}
public async sendPaymentCompleteOperation(
checkoutSession: CheckoutConversationSession,
request: PaymentRequest,
shippingAddress: PaymentAddress,
shippingOptionId: string,
payerEmail: string,
payerPhone: string
) {
if (!this.botEndpoint) {
return this.botEmulator.facilities.logger.logMessage(this.conversationId,
textItem(LogLevel.Error,
'Error: This conversation does not have an endpoint, cannot send payment complete activity.'));
}
const paymentTokenHeader = {
amount: request.details.total.amount,
expiry: '1/1/2020',
format: 2,
merchantId: request.methodData[0].data.merchantId,
paymentRequestId: request.id,
timestamp: '4/27/2017'
};
const paymentTokenHeaderStr = JSON.stringify(paymentTokenHeader);
const pthBytes = new Buffer(paymentTokenHeaderStr).toString('base64');
const paymentTokenSource = 'tok_18yWDMKVgMv7trmwyE21VqO';
const ptsBytes = new Buffer(paymentTokenSource).toString('base64');
const ptsigBytes = new Buffer('Emulator').toString('base64');
const updateValue: PaymentRequestComplete = {
id: request.id,
paymentRequest: request,
paymentResponse: {
details: {
paymentToken: pthBytes + '.' + ptsBytes + '.' + ptsigBytes
},
methodName: request.methodData[0].supportedMethods[0],
payerEmail: payerEmail,
payerPhone: payerPhone,
shippingAddress: shippingAddress,
shippingOption: shippingOptionId
}
};
const activity: InvokeActivity = {
type: 'invoke',
name: PaymentOperations.PaymentCompleteOperationName,
from: { id: checkoutSession.checkoutFromId },
conversation: { id: checkoutSession.checkoutConversationId },
relatesTo: {
activityId: checkoutSession.paymentActivityId,
bot: { id: this.botEndpoint.botId },
channelId: 'emulator',
conversation: { id: this.conversationId },
serviceUrl: this.botEmulator.getServiceUrl(this.botEndpoint.botUrl),
user: this.botEmulator.facilities.users.usersById(this.botEmulator.facilities.users.currentUserId)
},
value: updateValue
};
const { response } = await this.postActivityToBot(activity, false);
return response;
}
public async sendTokenResponse(
connectionName: string,
token: string,
doNotCache?: boolean) {
let userId = this.botEmulator.facilities.users.currentUserId;
let botId = this.botEndpoint.botId;
if (!doNotCache) {
TokenCache.addTokenToCache(botId, userId, connectionName, token);
}
const activity: EventActivity = {
type: 'event',
name: 'tokens/response',
value: {
connectionName: connectionName,
token: token
}
};
return await this.postActivityToBot(activity, false);
}
/**
* Returns activities since the watermark.
*/
public getActivitiesSince(watermark: number): { activities: Activity[], watermark: number } {
this.activities = watermark ? this.activities.filter(entry => entry.watermark >= watermark) : this.activities;
return {
activities: this.activities.map(entry => entry.activity),
watermark: this.nextWatermark
};
}
// TODO: This need to be redesigned
public feedActivities(activities: Activity[]) {
/*
* We need to fixup the activities to look like they're part of the current conversation.
* This a limitation of the way the emulator was originally designed, and not a problem
* with the transcript data. In the fullness of time, the emulator (and webchat control)
* could be better adapted to loading transcripts.
* */
const { id: currUserId } = this.user;
let origUserId = null;
let origBotId = null;
// Get original botId and userId
// Fixup conversationId
activities.forEach( (activity) => {
if (activity.conversation) {
activity.conversation.id = this.conversationId;
}
const { type } = activity;
if (
activity.recipient && (
type === 'event'
|| type === 'message'
|| type === 'messageReaction'
|| type === 'typing'
)
) {
if (!origBotId && activity.recipient.role === 'bot') {
origBotId = activity.recipient.id;
}
if (!origUserId && activity.recipient.role === 'user') {
origUserId = activity.recipient.id;
}
}
});
// Fixup recipient and from ids
if (this.botEndpoint && origUserId && origBotId) {
activities.forEach(activity => {
if (activity.recipient.id === origBotId) {
activity.recipient.id = this.botEndpoint.botId;
}
if (activity.from.id === origBotId) {
activity.from.id = this.botEndpoint.botId;
}
if (activity.recipient.id === origUserId) {
activity.recipient.id = currUserId;
}
if (activity.from.id === origUserId) {
activity.from.id = currUserId;
}
});
}
// Add activities to the queue
activities.forEach(activity => {
if (activity.recipient && activity.recipient.role === 'user') {
activity = this.processActivity(activity);
}
this.addActivityToQueue(activity);
});
}
public async getTranscript(): Promise<Activity[]> {
// Currently, we only export transcript of activities
// TODO: Think about "member join/left", "typing", "activity update/delete", etc.
const activities = this.transcript.filter(record => record.type === 'activity add').map(record => record.activity);
for (let i = 0; i < activities.length; i++) {
await this.processActivityForDataUrls(activities[i]);
}
return activities;
}
private async sendUpdateShippingOperation(
checkoutSession: CheckoutConversationSession,
operation: string,
request: PaymentRequest,
shippingAddress: PaymentAddress,
shippingOptionId: string
) {
if (!this.botEndpoint) {
return this.botEmulator.facilities.logger.logMessage(this.conversationId,
textItem(LogLevel.Error,
'Error: This conversation does not have an endpoint, cannot send update shipping activity.'));
}
const updateValue: PaymentRequestUpdate = {
id: request.id,
shippingAddress: shippingAddress,
shippingOption: shippingOptionId,
details: request.details
};
const activity: InvokeActivity = {
type: 'invoke',
name: operation,
from: { id: checkoutSession.checkoutFromId },
conversation: { id: checkoutSession.checkoutConversationId },
relatesTo: {
activityId: checkoutSession.paymentActivityId,
bot: { id: this.botEndpoint.botId },
channelId: 'emulator',
conversation: { id: this.conversationId },
serviceUrl: this.botEmulator.getServiceUrl(this.botEndpoint.botUrl),
user: this.botEmulator.facilities.users.usersById(this.botEmulator.facilities.users.currentUserId)
},
value: updateValue
};
const { response } = await this.postActivityToBot(activity, false);
// TODO: Should we record this in transcript? It looks like normal InvokeActivity
return response;
}
private postage(recipientId: string, activity: Activity, isHistoric: boolean = false): Activity {
const date = moment();
const timestamp = isHistoric ? activity.timestamp : date.toISOString();
const recipient = isHistoric ? activity.recipient : { id: recipientId };
return {
...activity,
channelId: 'emulator',
conversation: activity.conversation || { id: this.conversationId },
id: activity.id || uniqueId(),
localTimestamp: date.format(),
recipient: recipient,
timestamp: timestamp
};
}
private addActivityToQueue(activity: Activity) {
if (!(activity.channelData || {}).postback) {
this.activities = [...this.activities, { activity, watermark: this.nextWatermark++ }];
this.emit('addactivity', { activity });
}
const genericActivity = activity as GenericActivity;
if (genericActivity && activity.recipient) {
this.botEmulator.facilities.logger.logActivity(this.conversationId, genericActivity, activity.recipient.role);
}
}
}
class DataUrlEncoder {
constructor(
public bot: BotEmulator
) {
// the constructor only exists so we can pass in the bot
}
public async traverseActivity(activity: Activity) {
let messageActivity = activity as MessageActivity;
if (messageActivity) {
await this.traverseMessageActivity(messageActivity);
}
}
public async traverseMessageActivity(messageActivity: MessageActivity) {
if (messageActivity && messageActivity.attachments) {
for (let i = 0; i < messageActivity.attachments.length; i++) {
await this.traverseAttachment(messageActivity.attachments[i]);
}
}
}
public async traverseAttachment(attachment: Attachment) {
if (attachment && attachment.contentUrl) {
await this.visitContentUrl(attachment);
}
}
protected async visitContentUrl(attachment: Attachment) {
if (this.shouldBeDataUrl(attachment.contentUrl)) {
attachment.contentUrl = await this.makeDataUrl(attachment.contentUrl);
}
}
protected async makeDataUrl(url: string): Promise<string> {
let resultUrl = url;
const imported = await this.bot.options.fetch(url, {});
if (parseInt(imported.headers.get('content-length'), 10) < maxDataUrlLength) {
const buffer = await imported.buffer();
const encoded = Buffer.from(buffer).toString('base64');
const typeString = imported.headers.get('content-type');
resultUrl = 'data:' + typeString + ';base64,' + encoded;
}
return resultUrl;
}
protected shouldBeDataUrl(url: string): boolean {
return (url && (isLocalhostUrl(url) || url.indexOf('ngrok') !== -1));
}
}