-
Notifications
You must be signed in to change notification settings - Fork 659
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍