-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathadaptivecard-send.ts
More file actions
245 lines (214 loc) · 6.53 KB
/
adaptivecard-send.ts
File metadata and controls
245 lines (214 loc) · 6.53 KB
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
import type * as ACData from 'adaptivecards-templating';
import { z } from 'zod';
import { Logger } from '../../../cli/Logger.js';
import { globalOptionsZod } from '../../../Command.js';
import request, { CliRequestOptions } from '../../../request.js';
import { optionsUtils } from '../../../utils/optionsUtils.js';
import { zod } from '../../../utils/zod.js';
import AnonymousCommand from '../../base/AnonymousCommand.js';
import commands from '../commands.js';
export const options = z.looseObject({
...globalOptionsZod.shape,
url: z.string(),
title: z.string().optional().alias('t'),
description: z.string().optional().alias('d'),
imageUrl: z.string().optional().alias('i'),
actionUrl: z.string().optional().alias('a'),
card: z.string().optional(),
cardData: z.string().optional()
});
declare type Options = z.infer<typeof options>;
interface CommandArgs {
options: Options;
}
class AdaptiveCardSendCommand extends AnonymousCommand {
public get name(): string {
return commands.SEND;
}
public get description(): string {
return 'Sends adaptive card to the specified URL';
}
public get schema(): z.ZodType | undefined {
return options;
}
public getRefinedSchema(schema: typeof options): z.ZodObject<any> | undefined {
return schema
.refine(options => !options.cardData || options.card, {
error: 'When you specify cardData, you must also specify card.',
path: ['cardData'],
params: {
customCode: 'required'
}
})
.refine(options => {
if (options.card) {
try {
JSON.parse(options.card);
return true;
}
catch {
return false;
}
}
return true;
}, {
error: 'Specified card is not a valid JSON string.',
path: ['card']
})
.refine(options => {
if (options.cardData) {
try {
JSON.parse(options.cardData);
return true;
}
catch {
return false;
}
}
return true;
}, {
error: 'Specified cardData is not a valid JSON string.',
path: ['cardData']
});
}
public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
const unknownOptions = optionsUtils.getUnknownOptions(args.options, zod.schemaToOptions(this.schema!));
const unknownOptionNames: string[] = Object.getOwnPropertyNames(unknownOptions);
const card: any = await this.getCard(args, unknownOptionNames, unknownOptions);
const requestOptions: CliRequestOptions = {
url: args.options.url,
headers: {
'content-type': 'application/json',
'x-anonymous': true
},
data: {
type: 'message',
attachments: [{
contentType: 'application/vnd.microsoft.card.adaptive',
content: card
}]
},
responseType: 'json'
};
try {
const res = await request.post<string | number | undefined>(requestOptions);
if (res) {
// when sending card to Teams succeeds, the body contains 1 which we
// can safely ignore
if (typeof res === 'string') {
// when sending the webhook to Teams fails, the response is 200
// but the body contains a string similar to 'Webhook message delivery
// failed with error: Microsoft Teams endpoint returned HTTP error 400
// with ContextId MS-CV=Qn6afVIGzEq...' which we should treat as
// a failure
if (res.indexOf('failed') > -1) {
throw res;
}
await logger.log(res);
}
}
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}
private async getCard(args: CommandArgs, unknownOptionNames: string[], unknownOptions: any): Promise<any> {
// use custom card
if (args.options.card) {
let card: any = JSON.parse(args.options.card);
const cardData: any = this.getCardData(args, unknownOptionNames, unknownOptions);
if (cardData) {
// lazy-load adaptive cards templating SDK
const ACData = await import('adaptivecards-templating');
const template: ACData.Template = new ACData.Template(card);
// Create a data binding context, and set its $root property to the
// data object to bind the template to
const context: ACData.IEvaluationContext = {
$root: cardData
};
// expand the template - this generates the final Adaptive Card
card = template.expand(context);
}
return card;
}
// use predefined card
const card: any = {
type: "AdaptiveCard",
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
version: "1.2",
body: []
};
if (args.options.title) {
card.body.push({
type: "TextBlock",
size: "Medium",
weight: "Bolder",
text: args.options.title
});
}
if (args.options.imageUrl) {
card.body.push({
type: "Image",
url: args.options.imageUrl,
size: "Stretch"
});
}
if (args.options.description) {
card.body.push({
type: "TextBlock",
text: args.options.description,
wrap: true
});
}
if (unknownOptionNames.length > 0) {
card.body.push({
type: "FactSet",
facts: unknownOptionNames.map(o => {
return {
title: `${o}:`,
value: unknownOptions[o]
};
})
});
}
if (args.options.actionUrl) {
card.actions = [
{
type: "Action.OpenUrl",
title: "View",
url: args.options.actionUrl
}
];
}
return card;
}
private getCardData(args: CommandArgs, unknownOptionNames: string[], unknownOptions: any): any {
if (args.options.cardData) {
return JSON.parse(args.options.cardData);
}
if (unknownOptionNames.length > 0) {
return unknownOptions;
}
if (!args.options.title &&
!args.options.description &&
!args.options.imageUrl &&
!args.options.actionUrl) {
return undefined;
}
const cardData: any = {};
if (args.options.title) {
cardData.title = args.options.title;
}
if (args.options.description) {
cardData.description = args.options.description;
}
if (args.options.imageUrl) {
cardData.imageUrl = args.options.imageUrl;
}
if (args.options.actionUrl) {
cardData.actionUrl = args.options.actionUrl;
}
return cardData;
}
}
export default new AdaptiveCardSendCommand();