forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botActivityHandler.js
122 lines (106 loc) · 4.48 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
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
// 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 action 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-action.
(2) Define how the bot service will respond to incoming action commands.
Learn more: https://aka.ms/teams-me-respond-action.
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 action command.
handleTeamsMessagingExtensionSubmitAction(context, action) {
/* Commands are defined in the manifest file. This can be done using the Configuration tab, editing
the manifest Json directly, or using the manifest editor in App Studio. This project includes two
commands to help get you started: createCard and shareMessage.
*/
switch (action.commandId) {
case 'createCard':
return createCardCommand(context, action);
case 'shareMessage':
return shareMessageCommand(context, action);
default:
throw new Error('NotImplemented');
}
}
/* 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 */
}
function createCardCommand(context, action) {
// The user has chosen to create a card by choosing the 'Create Card' context menu command.
const data = action.data;
const heroCard = CardFactory.heroCard(data.title, data.text);
heroCard.content.subtitle = data.subTitle;
const attachment = { contentType: heroCard.contentType, content: heroCard.content, preview: heroCard };
return {
composeExtension: {
type: 'result',
attachmentLayout: 'list',
attachments: [
attachment
]
}
};
}
function shareMessageCommand(context, action) {
// The user has chosen to share a message by choosing the 'Share Message' context menu command.
let userName = 'unknown';
if (action.messagePayload.from &&
action.messagePayload.from.user &&
action.messagePayload.from.user.displayName) {
userName = action.messagePayload.from.user.displayName;
}
// This Messaging Extension example allows the user to check a box to include an image with the
// shared message. This demonstrates sending custom parameters along with the message payload.
let images = [];
const includeImage = action.data.includeImage;
if (includeImage === 'true') {
images = ['https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU'];
}
const heroCard = CardFactory.heroCard(`${ userName } originally sent this message:`,
action.messagePayload.body.content,
images);
if (action.messagePayload.attachments && action.messagePayload.attachments.length > 0) {
// This sample does not add the MessagePayload Attachments. This is left as an
// exercise for the user.
heroCard.content.subtitle = `(${ action.messagePayload.attachments.length } Attachments not included)`;
}
const attachment = { contentType: heroCard.contentType, content: heroCard.content, preview: heroCard };
return {
composeExtension: {
type: 'result',
attachmentLayout: 'list',
attachments: [
attachment
]
}
};
}
module.exports.BotActivityHandler = BotActivityHandler;