forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardUtils.ts
90 lines (81 loc) · 3.89 KB
/
CardUtils.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
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import * as builder from "botbuilder";
import * as stjs from "stjs";
import * as adaptiveCard from "adaptivecards";
export function renderACAttachment(template: any, data: any): builder.AttachmentType {
// ToDo:
// 1. Optionally validate that the schema is valid (postponed as there are tool/schema issues)
// Pre-process the template so that template placeholders don't show up for null data values
// Regex: Find everything between {{}} and prepend "#? " to it
template = JSON.parse(JSON.stringify(template).replace(/{{(.+?)}}/g, "{{#? $1}}"));
// No error handling in the call to stjs functions - what you pass in may be garbage, but it always returns a value
let ac = stjs.select(data)
.transformWith(template)
.root();
return {
contentType: "application/vnd.microsoft.card.adaptive",
content: ac,
};
}
export function renderO365ConnectorAttachment(template: any, data: any): builder.AttachmentType {
// Pre-process the template so that template placeholders don't show up for null data values
// Regex: Find everything between {{}} and prepend "#? " to it
template = JSON.parse(JSON.stringify(template).replace(/{{(.+?)}}/g, "{{#? $1}}"));
// No error handling in the call to stjs functions - what you pass in may be garbage, but it always returns a value
let card = stjs.select(data)
.transformWith(template)
.root();
return {
contentType: "application/vnd.microsoft.teams.card.o365connector",
content: card,
};
}
export function renderCard(template: any, data: any): any {
// Pre-process the template so that template placeholders don't show up for null data values
// Regex: Find everything between {{}} and prepend "#? " to it
template = JSON.parse(JSON.stringify(template).replace(/{{(.+?)}}/g, "{{#? $1}}"));
// No error handling in the call to stjs functions - what you pass in may be garbage, but it always returns a value
let card = stjs.select(data)
.transformWith(template)
.root();
return card;
}
// This function doesn't work as written (the async logic and error handling aren't right)
// and should (perhaps) be refactored as a promise, but at least it captures the logic for validation
// import * as request from "request";
// import * as Ajv from "ajv";
/* function validateSchema(json: any): boolean {
request({
url: "http://adaptivecards.io/schemas/adaptive-card.json",
json: true,
}, (error, response, body) => {
if (!error && response.statusCode === 200) {
let ajv = new Ajv();
ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json"));
return(ajv.validate(body, json));
}
},
);
return true;
} */