-
Notifications
You must be signed in to change notification settings - Fork 12
/
transcriptTransformationScript.js
138 lines (128 loc) · 5.97 KB
/
transcriptTransformationScript.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
This script is used for removing attributes from a transcript exported by Bot Framework Emulator.
Since the Bot Framework Emulator is connected directly to the bot, and not through the Bot Connector,
there are differences between the messages in the exported transcript and the actual messages polled
by the Functional Tests application.
This script accepts one argument - a full path (including the file name) to a transcript file exported by
Bot Framework Emulator, and creates a new transformed transcript file named "'"transfromed.transcript"
in the script directory.
Transformation is done by applying all defined handlers on each entry of the transcript object.
**/
const fs = require('fs');
/** Here we can add all the handlers we need **/
// Handler 1
function removeSchemaAttribute(currEntry) {
if (currEntry['type'] === 'message') {
if (currEntry.hasOwnProperty("attachments") && currEntry["attachments"][0].hasOwnProperty("content") && currEntry["attachments"][0]["content"].hasOwnProperty("$schema")) {
delete currEntry["attachments"][0]["content"]["$schema"];
}
}
}
// Handler 2
function addSeparationAttribute(currEntry) {
if (currEntry['type'] === 'message') {
if (currEntry.hasOwnProperty("attachments") && currEntry["attachments"][0].hasOwnProperty("content") && currEntry["attachments"][0]["content"].hasOwnProperty("body") && currEntry["attachments"][0]["content"]["body"][0].hasOwnProperty("items")) {
for (var item of currEntry["attachments"][0]["content"]["body"][0]["items"]) {
if (item.hasOwnProperty("spacing") && item.hasOwnProperty("isSubtle")) {
item["separation"] = "strong";
}
}
}
}
}
function convertColumnsWidthToString(items) {
for (var column of items["columns"]) {
if (column.hasOwnProperty("width")) {
column["width"] = column["width"] + "";
}
}
}
// Handler 3
function convertNumbersToString(currEntry) {
if (currEntry['type'] === 'message') {
if (currEntry.hasOwnProperty("attachments")) {
for (var attachment of currEntry["attachments"]) {
if (attachment.hasOwnProperty("content") && attachment["content"].hasOwnProperty("body")) {
for (var bodyItem of attachment["content"]["body"]) {
if (bodyItem.hasOwnProperty("columns")) {
convertColumnsWidthToString(bodyItem);
} else if (bodyItem.hasOwnProperty("items")) {
for (var item of bodyItem["items"]) {
if (item.hasOwnProperty("columns")) {
convertColumnsWidthToString(item);
}
}
}
}
}
}
}
}
}
// Handler 4
function convertColumnAttributesToCamelCase(currEntry) {
if (currEntry['type'] === 'message') {
if (currEntry.hasOwnProperty("attachments")) {
const attachments = currEntry["attachments"];
attachments.forEach(attachment => {
if (attachment.hasOwnProperty("content")) {
const content = attachment["content"];
if (content.hasOwnProperty("body")) {
const contentBody = content["body"][0];
if (contentBody.hasOwnProperty("items")) {
const bodyItems = contentBody["items"];
bodyItems.forEach(bodyItem => {
if (bodyItem.hasOwnProperty("columns")) {
const columns = bodyItem["columns"];
columns.forEach(column => {
if (column.hasOwnProperty("items")) {
const colItems = column["items"];
const attributesToEdit = ["size", "weight", "color", "horizontalAlignment", "spacing"];
colItems.forEach(colItem => {
attributesToEdit.forEach(attr => {
if (colItem.hasOwnProperty(attr)) {
colItem[attr] = colItem[attr].charAt(0).toLowerCase() + colItem[attr].slice(1, colItem[attr].length);
}
});
});
}
});
}
});
}
}
}
});
}
}
}
/** This is the main function - It iterates over all entries of the transcript, and applies all handlers on each entry **/
function main(path) {
console.log("Started");
let contentBuffer;
try {
contentBuffer = fs.readFileSync(path);
}
catch (e) {
console.log("Cannot open file", e.path);
return;
}
let jsonTranscript = JSON.parse(contentBuffer);
for (let i = 0; i < jsonTranscript.length; i++) {
let currEntry = jsonTranscript[i];
// Here we call to all the handlers we defined
removeSchemaAttribute(currEntry);
addSeparationAttribute(currEntry);
convertNumbersToString(currEntry);
convertColumnAttributesToCamelCase(currEntry);
}
try {
const filename = path.replace(/^.*[\\\/]/, '').replace(/\.[^/.]+$/, ''); // Extracts filename without extension from full path.
fs.writeFileSync(filename + '_transformed.transcript', JSON.stringify(jsonTranscript));
console.log("Done");
} catch (e) {
console.log("Cannot write file ", e);
}
}
//Call main with file path as argument.
main(process.argv[2]);