/
teamsBot.ts
170 lines (156 loc) · 4.73 KB
/
teamsBot.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
import { default as axios } from "axios";
import * as querystring from "querystring";
import {
TeamsActivityHandler,
CardFactory,
TurnContext,
MessagingExtensionAction,
MessagingExtensionQuery,
MessagingExtensionResponse,
MessagingExtensionActionResponse,
AppBasedLinkQuery,
} from "botbuilder";
export class TeamsBot extends TeamsActivityHandler {
// Action.
public async handleTeamsMessagingExtensionSubmitAction(
context: TurnContext,
action: MessagingExtensionAction
): Promise<MessagingExtensionActionResponse> {
switch (action.commandId) {
case "createCard":
return createCardCommand(context, action);
case "shareMessage":
return shareMessageCommand(context, action);
default:
throw new Error("NotImplemented");
}
}
// Search.
public async handleTeamsMessagingExtensionQuery(
context: TurnContext,
query: MessagingExtensionQuery
): Promise<MessagingExtensionResponse> {
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.content.tap = {
type: "invoke",
value: { name: obj.package.name, description: obj.package.description },
};
const attachment = { ...heroCard, preview };
attachments.push(attachment);
});
return {
composeExtension: {
type: "result",
attachmentLayout: "list",
attachments: attachments,
},
};
}
public async handleTeamsMessagingExtensionSelectItem(
context: TurnContext,
obj: any
): Promise<MessagingExtensionResponse> {
return {
composeExtension: {
type: "result",
attachmentLayout: "list",
attachments: [CardFactory.heroCard(obj.name, obj.description)],
},
};
}
// Link Unfurling.
public async handleTeamsAppBasedLinkQuery(
context: TurnContext,
query: AppBasedLinkQuery
): Promise<MessagingExtensionResponse> {
const attachment = CardFactory.thumbnailCard("Image Preview Card", query.url, [query.url]);
return {
composeExtension: {
type: "result",
attachmentLayout: "list",
attachments: [attachment],
},
};
}
}
async function createCardCommand(
context: TurnContext,
action: MessagingExtensionAction
): Promise<MessagingExtensionResponse> {
// 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],
},
};
}
async function shareMessageCommand(
context: TurnContext,
action: MessagingExtensionAction
): Promise<MessagingExtensionResponse> {
// The user has chosen to share a message by choosing the 'Share Message' context menu command.
let userName = "unknown";
if (
action.messagePayload &&
action.messagePayload.from &&
action.messagePayload.from.user &&
action.messagePayload.from.user.displayName
) {
userName = action.messagePayload.from.user.displayName;
}
// This Message 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 &&
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],
},
};
}