Last active
February 2, 2016 18:18
-
-
Save djbender/a83b325264a39a93e895 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
I assume that the bot user is already in some channel. | |
In my case I had the bot invited to #bot-test in a slack I have admin privileges for. | |
$ SLACK_API_KEY=xoxb-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXX ruby test-slack.rb |
This file contains hidden or 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
require 'rest-client' | |
require 'faye/websocket' | |
require 'eventmachine' | |
require 'json' | |
require 'yaml' | |
class RTMSession | |
def self.url | |
response = RestClient.post( | |
"https://slack.com/api/rtm.start", | |
token: ENV['SLACK_API_KEY'] | |
) | |
json = JSON.parse(response.body) | |
if json.fetch("ok") | |
puts "connecting to #{json.fetch('url')}" | |
json.fetch("url") | |
else | |
raise RTMSessionStartFail, json["errors"] | |
end | |
end | |
end | |
class RTMSessionStartFail < StandardError; end | |
def start(url) | |
EM.run { | |
ws = Faye::WebSocket::Client.new(url) | |
ws.onopen = lambda do |event| | |
p [:open, ws.headers] | |
end | |
ws.onclose = lambda do |close| | |
p [:close, close.code, close.reason] | |
EM.stop | |
end | |
ws.onerror = lambda do |error| | |
p [:error, error.message] | |
end | |
ws.onmessage = lambda do |message| | |
message_json = JSON.parse message.data | |
p [:message, message_json] | |
end | |
} | |
end | |
start(RTMSession.url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment