forked from BeepBoopHQ/starter-node-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (44 loc) · 1.46 KB
/
index.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
var Slack = require('slack-client')
// Expect a SLACK_TOKEN environment variable
var slackToken = process.env.SLACK_TOKEN
if (!slackToken) {
console.error('SLACK_TOKEN is required!')
process.exit(1)
}
// Create a new Slack client with our token
var slack = new Slack(slackToken, true, true)
// bot will be initialized when logged in
var bot
// handle loggedIn event
slack.on('loggedIn', function (user, team) {
bot = user
console.log('I am ' + user.name + ' of team ' + team.name)
})
// show status when we conntect
slack.on('open', function () {
console.log('Connected')
})
// register a handler for messages
slack.on('message', function (message) {
if (isMe(message, bot.id)) return
if (!isMentioned(message, bot.id) && !isDM(message, bot.id)) return
console.log('Received message ' + message)
// We recieved a message that wasn't from us and either mentioned us or was a DM to us
// Let's ust say hi
var channel = slack.getChannelGroupOrDMByID(message.channel)
channel.send('Good day to you from Wired Dude!')
})
// initiate login
slack.login()
// ----------------------------------------------------------------------------
// Utilities
// ----------------------------------------------------------------------------
function isMentioned (message, userId) {
return message.text.indexOf('<@' + userId) >= 0
}
function isMe (message, userId) {
return message.user === userId
}
function isDM (message, userId) {
return message.channel[0] === 'D'
}