RealTime Messaging API reference is here.
Sign up at slack.com.
This is something done in Slack, under integrations. Create a new bot, and note its API token.
source 'http://rubygems.org'
gem 'http'
gem 'faye-websocket'
gem 'eventmachine'
require 'http'
require 'json'
rc = JSON.parse HTTP.post("https://slack.com/api/rtm.start", params: {
token: ENV['SLACK_API_TOKEN'],
})
puts rc['url']
wss://ms364.slack-msgs.com/websocket/...
require 'faye/websocket'
require 'eventmachine'
EM.run {
ws = Faye::WebSocket::Client.new(url)
ws.on :open do |event|
p [:open]
end
ws.on :message do |event|
p [:message, JSON.parse(event.data)]
end
ws.on :close do |event|
p [:close, event.code, event.reason]
ws = nil
end
}
ws.on :message do |event|
data = JSON.parse(event.data) if event && event.data
if data && data['type'] == 'message' && data['text'] == 'hi'
ws.send({ type: 'message', text: "hi <@#{data['user']}>", channel: data['channel'] }.to_json)
end
end
Working code in 04-rtm.rb.