-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_reverse.coffee
89 lines (67 loc) · 2.93 KB
/
simple_reverse.coffee
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
# This is a simple example of how to use the slack-client module in CoffeeScript. It creates a
# bot that responds to all messages in all channels it is in with a reversed
# string of the text received.
#
# To run, copy your token below, then, from the project root directory:
#
# To run the script directly
# npm install
# node_modules/coffee-script/bin/coffee examples/simple_reverse.coffee
#
# If you want to look at / run / modify the compiled javascript
# npm install
# node_modules/coffee-script/bin/coffee -c examples/simple_reverse.coffee
# cd examples
# node simple_reverse.js
#
Slack = require 'slack-client'
token = 'xoxb-7212819110-AT2jJNWQlmR1WYLX2FBHdtAH' # Add a bot at https://my.slack.com/services/new/bot and copy the token here.
autoReconnect = true
autoMark = true
slack = new Slack(token, autoReconnect, autoMark)
slack.on 'open', ->
channels = []
groups = []
unreads = slack.getUnreadCount()
# Get all the channels that bot is a member of
channels = ("##{channel.name}" for id, channel of slack.channels when channel.is_member)
# Get all groups that are open and not archived
groups = (group.name for id, group of slack.groups when group.is_open and not group.is_archived)
console.log "Welcome to Slack. You are @#{slack.self.name} of #{slack.team.name}"
console.log 'You are in: ' + channels.join(', ')
console.log 'As well as: ' + groups.join(', ')
messages = if unreads is 1 then 'message' else 'messages'
console.log "You have #{unreads} unread #{messages}"
slack.on 'message', (message) ->
channel = slack.getChannelGroupOrDMByID(message.channel)
user = slack.getUserByID(message.user)
response = ''
{type, ts, text} = message
channelName = if channel?.is_channel then '#' else ''
channelName = channelName + if channel then channel.name else 'UNKNOWN_CHANNEL'
userName = if user?.name? then "@#{user.name}" else "UNKNOWN_USER"
console.log """
Received: #{type} #{channelName} #{userName} #{ts} "#{text}"
"""
# Respond to messages with the reverse of the text received.
if type is 'message' and text? and channel?
response = text.split('').reverse().join('')
channel.send response
console.log """
@#{slack.self.name} responded with "#{response}"
"""
else
#this one should probably be impossible, since we're in slack.on 'message'
typeError = if type isnt 'message' then "unexpected type #{type}." else null
#Can happen on delete/edit/a few other events
textError = if not text? then 'text was undefined.' else null
#In theory some events could happen with no channel
channelError = if not channel? then 'channel was undefined.' else null
#Space delimited string of my errors
errors = [typeError, textError, channelError].filter((element) -> element isnt null).join ' '
console.log """
@#{slack.self.name} could not respond. #{errors}
"""
slack.on 'error', (error) ->
console.error "Error: #{error}"
slack.login()