Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported simple.js to coffeescript #28

Merged
merged 4 commits into from
Feb 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = (grunt) ->
index:
files:
'index.js': 'index.coffee'
examples:
expand: true
cwd: 'examples'
src: ['*.coffee']
dest: 'examples'
ext: '.js'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope this looks good. Never worked with grunt before!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

classes:
expand: true
cwd: 'src'
Expand Down
72 changes: 0 additions & 72 deletions examples/simple.js

This file was deleted.

89 changes: 89 additions & 0 deletions examples/simple_reverse.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 '..'

token = 'xoxb-YOUR-TOKEN-HERE' # 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()