slack-rtm-receiver
A Ruby gem. It connects to Slack Real Time Messaging API to receive events. Runs on EventMachine.
Requirements
- Ruby 2.0.0 <=
- eventmachine 1.0 <=
- em-http-request 1.1 <=
- faye-websocket 0.8 <=
Getting Started
Install
$ gem install slack-rtm-receiver
Use
require 'slack-rtm-receiver'
Create an object to respond to received events. You can subclass EventHandler.
class MyHandler < SlackRTMReceiver::EventHandler
def process_event(event, session)
if event[:text] == 'hi'
res_event = {
type: 'message',
channel: event[:channel],
text: 'Hi!'
}
session.send_event(res_event)
end
end
end
SlackRTMReceiver.add_event_handler(MyHandler.new)
Or, you can pass a block. The following works the same as above.
SlackRTMReceiver::EventHandler.add_type('message') do |event, session|
if event[:text] == 'hi'
res_event = {
type: 'message',
channel: event[:channel],
text: 'Hi!'
}
session.send_event(res_event)
end
end
Start the reactor to connect to Slack.
opts = {token: 'xoxb-1234abcd5678efgh'}
SlackRTMReceiver::Reactor.run(opts)