forked from damirn/bot-sdk-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddbot.js
More file actions
103 lines (87 loc) · 2.03 KB
/
addbot.js
File metadata and controls
103 lines (87 loc) · 2.03 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
const https = require("https");
const botName = "doraemon";
const serviceId = "";
const providerId = "";
const email = "";
const password = "";
/*
1. log in => get authorization bearer
2. use authorization bearer to get bot convo ID
3. use service and provider IDs to join convo with bot
*/
var httpsOpts = {
"host": "prod-nginz-https.wire.com",
"port": 443,
"path": "/login",
"method": "POST",
"headers": {
"Content-Type": "application/json"
}
};
var req = https.request(httpsOpts, (resp) => {
var str = "";
resp.on("data", (chunk) => {
str += chunk;
});
resp.on("end", () => {
console.log(`login response: ${str}`);
try {
var jsonObj = JSON.parse(str);
var token = jsonObj["access_token"];
getBotId(token);
} catch(e) {
console.log(`parsing error ${e}`);
}
});
});
req.write(`{"email": "${email}", "password": "${password}"}`);
req.end();
function getBotId(token) {
var optsUpdate = {
"path": "/conversations",
"headers": {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
}
};
Object.assign(httpsOpts, optsUpdate);
var request = https.request(httpsOpts, (resp) => {
var str = "";
resp.on("data", (chunk) => {
str += chunk
});
resp.on("end", () => {
console.log(`get bot ID resp: ${str}`);
try {
var jsonObj = JSON.parse(str);
var botId = jsonObj.id;
startConvo(botId, token);
} catch(e) {
console.log(`parsing error ${e}`);
}
});
});
request.write(`{"users": [], "name": "${botName}"}`);
request.end();
}
function startConvo(botId, token) {
var optsUpdate = {
"path": `/conversations/${botId}/bots`,
"headers": {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
}
};
Object.assign(httpsOpts, optsUpdate);
var request = https.request(httpsOpts, (resp) => {
var str = "";
resp.on("data", (chunk) => {
str += chunk
});
resp.on("end", () => {
console.log(`start convo resp: ${str}`);
});
});
request.write(`{"service": "${serviceId}", "provider": "${providerId}"}`);
request.end();
}