1
- # !/usr/bin/env coffee
2
1
# This is a simple example of how to use the slack-client module in CoffeeScript. It creates a
3
2
# bot that responds to all messages in all channels it is in with a reversed
4
3
# string of the text received.
5
4
#
6
- # To run, copy your token below, then:
7
- # npm install
8
- # cd examples
9
- # coffee simple_reverse.coffeescript
5
+ # To run, copy your token below, then, from the project root directory:
6
+ #
7
+ # To run the script directly
8
+ # npm install
9
+ # node_modules/coffee-script/bin/coffee examples/simple_reverse.coffee
10
+ #
11
+ # If you want to look at / run / modify the compiled javascript
12
+ # npm install
13
+ # node_modules/coffee-script/bin/coffee -c examples/simple_reverse.coffee
14
+ # cd examples
15
+ # node simple_reverse.js
16
+ #
10
17
11
18
Slack = require ' ..'
12
19
@@ -20,50 +27,46 @@ slack.on 'open', ->
20
27
channels = []
21
28
groups = []
22
29
unreads = slack .getUnreadCount ()
23
- key
24
30
25
31
# Get all the channels that bot is a member of
26
32
channels = (" ##{ value .name } " for key, value of slack .channels when slack .channels [key].is_member )
27
33
# Get all groups that are open and not archived
28
- groups = (value .name for key, value of slack .groups when slack .groups [key].is_open and not slack .groups [key].is_archived )
34
+ groups = (for key, value of slack .groups
35
+ if slack .groups [key].is_open and not slack .groups [key].is_archived
36
+ value .name )
29
37
30
38
31
- console .log ' Welcome to Slack. You are @%s of %s ' , slack .self .name , slack .team .name
32
- console .log ' You are in: %s ' , channels .join (' , ' )
33
- console .log ' As well as: %s ' , groups .join (' , ' )
39
+ console .log " Welcome to Slack. You are @#{ slack .self .name } of #{ slack .team .name } "
40
+ console .log ' You are in: ' + channels .join (' , ' )
41
+ console .log ' As well as: ' + groups .join (' , ' )
34
42
35
- messages =
36
- if unreads is 1
37
- ' message'
38
- else
39
- ' messages'
43
+ messages = if unreads is 1 then ' message' else ' messages'
40
44
41
45
console .log " You have #{ unreads} unread #{ messages} "
42
46
43
47
44
48
slack .on ' message' , (message ) ->
45
- type = message .type
46
49
channel = slack .getChannelGroupOrDMByID (message .channel )
47
50
user = slack .getUserByID (message .user )
48
- time = message .ts
49
- text = message .text
50
51
response = ' '
51
52
52
- channelName =
53
- if channel .is_channel
54
- " #"
55
- else
56
- " "
53
+ {type , ts , text } = message
54
+
55
+ channelName = if channel .is_channel then ' #' else ' '
57
56
channelName += channel .name
58
57
59
- console .log " Received: #{ type} #{ channelName} @#{ user .name } #{ time} \" #{ text} \" "
58
+ console .log """
59
+ Received: #{ type} #{ channelName} @#{ user .name } #{ ts} "#{ text} "
60
+ """
60
61
61
62
# Respond to messages with the reverse of the text received.
62
63
63
64
if type is ' message'
64
65
response = text .split (' ' ).reverse ().join (' ' )
65
66
channel .send response
66
- console .log " @#{ slack .self .name } responded with \" #{ response} \" "
67
+ console .log """
68
+ @#{ slack .self .name } responded with "#{ response} "
69
+ """
67
70
68
71
69
72
slack .on ' error' , (error ) ->
0 commit comments