forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botActivityHandler.js
87 lines (73 loc) · 3.09 KB
/
botActivityHandler.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const {
TurnContext,
MessageFactory,
TeamsActivityHandler,
CardFactory,
ActionTypes
} = require('botbuilder');
class BotActivityHandler extends TeamsActivityHandler {
constructor() {
super();
}
/* Building a messaging extension search command is a two step process.
(1) Define how the messaging extension will look and be invoked in the client.
This can be done from the Configuration tab, or the Manifest Editor.
Learn more: https://aka.ms/teams-me-design-search.
(2) Define how the bot service will respond to incoming search commands.
Learn more: https://aka.ms/teams-me-respond-search.
NOTE: Ensure the bot endpoint that services incoming messaging extension queries is
registered with Bot Framework.
Learn more: https://aka.ms/teams-register-bot.
*/
// Invoked when the service receives an incoming search query.
async handleTeamsMessagingExtensionQuery(context, query) {
const axios = require('axios');
const querystring = require('querystring');
const searchQuery = query.parameters[0].value;
const response = await axios.get(`http://registry.npmjs.com/-/v1/search?${ querystring.stringify({ text: searchQuery, size: 8 }) }`);
const attachments = [];
response.data.objects.forEach(obj => {
const heroCard = CardFactory.heroCard(obj.package.name);
const preview = CardFactory.heroCard(obj.package.name); // Preview cards are optional for Hero card. You need them for Adaptive Cards.
preview.content.tap = { type: 'invoke', value: { description: obj.package.description } };
const attachment = { ...heroCard, preview };
attachments.push(attachment);
});
return {
composeExtension: {
type: 'result',
attachmentLayout: 'list',
attachments: attachments
}
};
}
// Invoked when the user selects an item from the search result list returned above.
async handleTeamsMessagingExtensionSelectItem(context, obj) {
return {
composeExtension: {
type: 'result',
attachmentLayout: 'list',
attachments: [CardFactory.thumbnailCard(obj.description)]
}
};
}
/* Messaging Extension - Unfurling Link */
handleTeamsAppBasedLinkQuery(context, query) {
const attachment = CardFactory.thumbnailCard('Thumbnail Card',
query.url,
['https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png']);
const result = {
attachmentLayout: 'list',
type: 'result',
attachments: [attachment]
};
const response = {
composeExtension: result
};
return response;
}
/* Messaging Extension - Unfurling Link */
}
module.exports.BotActivityHandler = BotActivityHandler;