-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhelloworld.js
99 lines (85 loc) · 3.44 KB
/
helloworld.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
//
// Copyright (c) 2016 Cisco Systems
// Licensed under the MIT License
//
/*
* a Webex Teams bot that:
* - sends a welcome message as he joins a room,
* - answers to a /hello command, and greets the user that chatted him
* - supports /help and a 'fallback' helper message
*
* + leverages the "node-sparkclient" library for bot to Webex communications.
*
*/
const WebexChatBot = require("node-sparkbot");
const bot = new WebexChatBot();
// Remove comment to overload default '/' prefix to identify bot commands
//bot.interpreter.prefix = "#";
const SparkAPIWrapper = require("node-sparkclient");
if (!process.env.ACCESS_TOKEN) {
console.log("Could not start as this bot requires a Webex Teams API access token.");
console.log("Please add env variable ACCESS_TOKEN on the command line");
console.log("Example: ");
console.log("> ACCESS_TOKEN=XXXXXXXXXXXX DEBUG=sparkbot* node helloworld.js");
process.exit(1);
}
const client = new SparkAPIWrapper(process.env.ACCESS_TOKEN);
//
// Help and fallback commands
//
bot.onCommand("help", function (command) {
client.createMessage(command.message.roomId, "Hi, I am the Hello World bot !\n\nType /hello to see me in action.", { "markdown":true }, function(err, message) {
if (err) {
console.log("WARNING: could not post message to room: " + command.message.roomId);
return;
}
});
});
bot.onCommand("fallback", function (command) {
client.createMessage(command.message.roomId, "Sorry, I did not understand.\n\nTry /help.", { "markdown":true }, function(err, response) {
if (err) {
console.log("WARNING: could not post Fallback message to room: " + command.message.roomId);
return;
}
});
});
//
// Bots commands here
//
bot.onCommand("hello", function (command) {
let email = command.message.personEmail; // User that created the message orginally
client.createMessage(command.message.roomId, `Hello, your email is: **${email}**`, { "markdown":true }, function(err, message) {
if (err) {
console.log("WARNING: could not post message to room: " + command.message.roomId);
return;
}
});
});
//
// Welcome message
// sent as the bot is added to a Room
//
bot.onEvent("memberships", "created", function (trigger) {
let newMembership = trigger.data; // see specs here: https://developer.webex.com/endpoint-memberships-get.html
if (newMembership.personId != bot.interpreter.person.id) {
// ignoring
console.log("new membership fired, but it is not us being added to a room. Ignoring...");
return;
}
// so happy to join
console.log("bot's just added to room: " + trigger.data.roomId);
client.createMessage(trigger.data.roomId, "Hi, I am the Hello World bot !\n\nType /hello to see me in action.", { "markdown":true }, function(err, message) {
if (err) {
console.log("WARNING: could not post Hello message to room: " + trigger.data.roomId);
return;
}
if (message.roomType == "group") {
client.createMessage(trigger.data.roomId, "**Note that this is a 'Group' room. I will wake up only when mentionned.**", { "markdown":true }, function(err, message) {
if (err) {
console.log("WARNING: could not post Mention message to room: " + trigger.data.roomId);
return;
}
});
}
});
});