-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path10-httpclient.js
More file actions
106 lines (90 loc) · 3.31 KB
/
10-httpclient.js
File metadata and controls
106 lines (90 loc) · 3.31 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
/**
* Posts a message to a Webex Teams space via a Bot account
*
* Prep work:
* from a ssh session, type the commands below:
* > xConfiguration HttpClient Mode: On
* > xConfiguration HttpClient AllowInsecureHTTPS: True
*
* Example for tshell:
* > xcommand HttpClient Post
* Url: https://api.ciscospark.com/v1/messages
* Header: "Content-Type: application/json"
* Header: "Authorization: Bearer BOT_TOKEN"
* ResultBody: "plaintext"
* AllowInsecureHTTPS: True
* > {"markdown":"hey, this is Steve","roomId":"ROOM_ID"}
* > .
*
*
*/
const xapi = require('xapi');
// Replace with your bot token
const token = "BOT_TOKEN";
// replace with a space your bot is part of
const roomId = "ROOM_ID";
function push(msg, cb) {
// Post message
let payload = {
"markdown": msg,
"roomId": roomId
}
xapi.command(
'HttpClient Post',
{
Header: ["Content-Type: application/json", "Authorization: Bearer " + token],
Url: "https://api.ciscospark.com/v1/messages",
AllowInsecureHTTPS: "True",
ResultBody: 'plaintext'
},
JSON.stringify(payload))
.then((response) => {
console.debug(`received response with status code: ${response.StatusCode}`);
if (response.StatusCode == 200) {
console.log("message pushed to Webex Teams");
// Retrieve message id
let result = JSON.parse(response.Body);
console.log(`message id: ${result.id}`);
if (cb) cb(null, result.id);
return;
}
// This should not happen as Webex REST API always return 200 OK for POST requests
console.log("failed with status code: " + response.StatusCode);
if (cb) cb("failed with status code: " + response.StatusCode, response.StatusCode);
})
.catch((err) => {
console.log(`failed with err message: ${err.message}`);
switch (err.message) {
case 'Unknown command':
// Can be caught at coding time
console.log("seems a wrong HttpClient Verb is used");
break;
case 'HttpClientPostResult':
case 'HttpClientDeleteResult':
console.log(`failed with err status: ${err.data.status}`);
console.log(Object.keys(err.data));
if (err.data.status === 'Error') {
// Typically: hostname not found
if (err.data.Message) {
console.log("data message: " + err.data.Message);
break;
}
// Typically: the response status code is 4xx or 5xx
if (err.data.StatusCode) {
console.log("status code: " + err.data.StatusCode);
// Note: err.data.Headers can also be retrieved, though not the body of the response (no ResponseBody attribute here)
break;
}
}
}
if (cb) cb("Could not post message to Webex Teams", null);
})
}
push('Hey, this is Steve', console.log);
/*
xapi.event.on('UserInterface Extensions Event Clicked', (event) => {
console.log(`new event from: ${event.Signal}`)
// Push info about the session
push('Hello World')
})
*/