Skip to content

Commit 7f53357

Browse files
author
James Elliott
committed
Removed old javascript example. Replaced with coffeescript example
Better explanation on how to run simple_reverse.coffee example Added examples to the Gruntfile Responded to issues from code review.
1 parent 438aebc commit 7f53357

File tree

3 files changed

+34
-97
lines changed

3 files changed

+34
-97
lines changed

Gruntfile.coffee

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ module.exports = (grunt) ->
2626
index:
2727
files:
2828
'index.js': 'index.coffee'
29+
examples:
30+
expand: true
31+
cwd: 'examples'
32+
src: ['*.coffee']
33+
dest: 'examples'
34+
ext: '.js'
2935
classes:
3036
expand: true
3137
cwd: 'src'

examples/simple.js

-72
This file was deleted.

examples/simple_reverse.coffee

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
#!/usr/bin/env coffee
21
# This is a simple example of how to use the slack-client module in CoffeeScript. It creates a
32
# bot that responds to all messages in all channels it is in with a reversed
43
# string of the text received.
54
#
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+
#
1017

1118
Slack = require '..'
1219

@@ -20,50 +27,46 @@ slack.on 'open', ->
2027
channels = []
2128
groups = []
2229
unreads = slack.getUnreadCount()
23-
key
2430

2531
# Get all the channels that bot is a member of
2632
channels = ("##{value.name}" for key, value of slack.channels when slack.channels[key].is_member)
2733
# 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)
2937

3038

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(', ')
3442

35-
messages =
36-
if unreads is 1
37-
'message'
38-
else
39-
'messages'
43+
messages = if unreads is 1 then 'message' else 'messages'
4044

4145
console.log "You have #{unreads} unread #{messages}"
4246

4347

4448
slack.on 'message', (message) ->
45-
type = message.type
4649
channel = slack.getChannelGroupOrDMByID(message.channel)
4750
user = slack.getUserByID(message.user)
48-
time = message.ts
49-
text = message.text
5051
response = ''
5152

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 ''
5756
channelName += channel.name
5857

59-
console.log "Received: #{type} #{channelName} @#{user.name} #{time} \"#{text}\""
58+
console.log """
59+
Received: #{type} #{channelName} @#{user.name} #{ts} "#{text}"
60+
"""
6061

6162
# Respond to messages with the reverse of the text received.
6263

6364
if type is 'message'
6465
response = text.split('').reverse().join('')
6566
channel.send response
66-
console.log "@#{slack.self.name} responded with \"#{response}\""
67+
console.log """
68+
@#{slack.self.name} responded with "#{response}"
69+
"""
6770

6871

6972
slack.on 'error', (error) ->

0 commit comments

Comments
 (0)