-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
createBot.js
89 lines (73 loc) · 3.23 KB
/
createBot.js
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
const { ActivityHandler, TurnContext } = require('botbuilder');
const bytes = require('bytes');
const createBotAdapter = require('./createBotAdapter');
const { Aborter, BlockBlobURL, SharedKeyCredential, StorageURL } = require('@azure/storage-blob');
const { AZURE_STORAGE_ACCOUNT_KEY, AZURE_STORAGE_ACCOUNT_NAME } = process.env;
module.exports = () => {
const bot = new ActivityHandler();
// Handler for "event" activity.
bot.onEvent(async (context, next) => {
const {
activity: { name, value }
} = context;
// Intercepts all "upload" event activities.
if (name === 'upload') {
// For async operations that are outside of BotBuilder, we should use proactive messaging.
const reference = TurnContext.getConversationReference(context.activity);
// The file upload validation could take more than a second, we should send a typing indicator.
await context.sendActivity({ type: 'typing' });
const { files } = value;
const pipeline = StorageURL.newPipeline(
new SharedKeyCredential(AZURE_STORAGE_ACCOUNT_NAME, AZURE_STORAGE_ACCOUNT_KEY)
);
const properties = await Promise.all(
files.map(file => {
// You should verify the URL if it is from the blob account and container that this service owned.
const blockBlobURL = new BlockBlobURL(file, pipeline);
// After the bot receives the URL, it should validate its content and associate it with the user ID.
// If the content needs to be kept for longer than a day, we should copy it to another blob container because we set up a rule to clean the storage daily.
return blockBlobURL.getProperties(Aborter.timeout(5000));
})
);
const adapter = createBotAdapter();
// Resumes the conversation when everything is ready.
await adapter.continueConversation(reference, async context => {
await context.sendActivity({
attachmentLayout: 'carousel',
attachments: properties.map(({ contentLength, metadata: { name } }, index) => ({
contentType: 'application/vnd.microsoft.card.thumbnail',
content: {
buttons: [
{
subtitle:
'The link of this blob URL being sent back to the user is for demonstration purposes; since it does not contain SAS, the file is not downloadable by the user, and the URL should be used for further processing in the bot/app.',
title: 'Get blob URL',
type: 'openurl',
value: files[index]
}
],
images: [
{
alt: '',
url: '/document.png'
}
],
subtitle: bytes(contentLength, { unitSeparator: ' ' }),
title: name
}
})),
text: 'Here is your upload summary.\n\nWe will clean up daily and will not redistribute your uploads.',
type: 'message'
});
});
} else {
await next();
}
});
// Handler for "message" activity
bot.onMessage(async (context, next) => {
await context.sendActivity('Please upload one or more files.');
await next();
});
return bot;
};