-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
51 lines (42 loc) · 1.34 KB
/
bot.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
const utils = require('./utils')
const express = require('express')
const app = express()
app.use(express.json())
ACCESS_TOKEN = '<insert_your_access_token_here>'
const Webex = require('webex');
const webex = Webex.init({
credentials: {
access_token: ACCESS_TOKEN
}
});
utils.setUpWebhook(webex)
webex.people.get('me')
.then(function (myDetails) {
global.myId = myDetails.id
app.listen(5000)
console.log("Listening on port 5000")
})
.catch(function(err) {
console.log("ERROR: Failed to fetch bot details\n" + err)
})
app.post('/', function (req, res) {
res.sendStatus(200)
if (req.body.data.personId === global.myId)
return
processMessage(req.body.data)
})
async function processMessage(messageData) {
var sender = await webex.people.get(messageData.personId);
var message = await webex.messages.get(messageData.id);
var response = 'Hi ' + sender.firstName + ' your message was "' + message.text + '"'
if (message.roomType === 'direct')
sendDirectMessage(message.personEmail, response)
if (message.roomType === 'group')
sendMessageInRoom(message.roomId, response)
}
function sendDirectMessage(personEmail, message) {
webex.messages.create({ toPersonEmail: personEmail, markdown: message });
}
function sendMessageInRoom(roomId, message) {
webex.messages.create({ roomId: roomId, markdown: message });
}