-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathroomid-phantom.js
97 lines (79 loc) · 3.85 KB
/
roomid-phantom.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
//
// Copyright (c) 2016 Cisco Systems
// Licensed under the MIT License
//
/*
* a bot that gives you instant info about a room id
*
* note : this example could work with both a human (developer) or a bot account,
* but the philosophy of this bot is really to use it with a bot account
*
*/
const debug = require("debug")("samples");
const fine = require("debug")("samples:fine");
// Starts your Bot with default configuration. The Webex Teams API access token is read from the ACCESS_TOKEN env variable
const WebexChatBot = require("node-sparkbot");
const bot = new WebexChatBot();
// do not listen to ourselves
// uncomment if you're running the bot from your Developer access token and you want to invoke in a 1-1 room
//bot.interpreter.ignoreSelf = false;
// Removing the bot default triggering '/' filter
bot.interpreter.prefix = "";
const SparkClient = require("node-sparky");
const sparky = new SparkClient({ token: process.env.ACCESS_TOKEN });
bot.onCommand("about", function (command) {
sparky.messageSend({
roomId: command.message.roomId,
markdown: "```\n{\n 'author':'Stève Sfartz <stsfartz@cisco.com>',\n 'code':'https://github.com/CiscoDevNet/node-sparkbot-samples/blob/master/examples/roomid-phantom.js',\n 'description':'a handy tool to retreive Webex Teams space identifiers',\n 'healthcheck':'GET https://sparkbot-roomid.herokuapp.com',\n 'webhook':'POST https://sparkbot-roomid.herokuapp.com'\n}\n```"
});
});
bot.onCommand("fallback", function (command) {
// so happy to join
sparky.messageSend({
roomId: command.message.roomId,
text: "sorry, I did not understand. Try help."
})
.then(function (message) {
// show how to use
showHelp(command.message.roomId);
});
});
bot.onCommand("help", function (command) {
showHelp(command.message.roomId);
});
function showHelp(roomId) {
sparky.messageSend({
roomId: roomId,
markdown: "Well, I am an ephemeral bot, serving developers. My primary mission is to fetch identifiers: add me to a space, and I'll send back - the room's identifier - in a private 1-1 message, then will silently leave the space I just got added to.\n\nAlso, be aware that my skills are limited:\n- about\n- help"
});
}
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) {
debug("bot has just been added to space: " + trigger.data.roomId);
// only take action if it is not the bot who created the room, to send the message back
if (trigger.actorId != bot.interpreter.person.id) {
// Retreive actorEmail
sparky.personGet(trigger.actorId)
.then(function (person) {
let email = person.emails[0];
debug("found inquirer: " + email);
// Send a direct message
sparky.messageSend({
toPersonEmail: email,
markdown: "Found roomId: **" + newMembership.roomId + "**\n\nWill now leave the space you asked me to inquire on..."
})
.then(function (message) {
// Leave inquired room
sparky.membershipRemove(newMembership.id)
.then(function () {
sparky.messageSend({
toPersonEmail: email,
markdown: "Job done: I have silently left the space... Let me know when you need other identifiers ;-)"
});
})
});
})
}
}
});