Skip to content

Latest commit

 

History

History
106 lines (73 loc) · 2.14 KB

BUTTON.md

File metadata and controls

106 lines (73 loc) · 2.14 KB

Slack Button Integration Basics

Make a Team

Sign up at slack.com, my team is dblockdotorg.slack.com.

Create a New Slack App

This is something done in Slack, under New Application. Make the redirect URL http://localhost:4567.

Client ID and Secret

Note the Client ID and Client Secret and export them into ENV.

export SLACK_CLIENT_ID=... SLACK_CLIENT_SECRET=...

Bot User

Add a Bot User with a default username.

Slack Button

Follow the Add the Slack Button link. Check bot.

Copy-paste the slack button code into an index.html file. See index.html for an example.

Gemfile

source 'http://rubygems.org'

gem 'http'
gem 'faye-websocket'
gem 'eventmachine'
gem 'slack-ruby-bot'
gem 'sinatra'

Service Implementation

require 'http'
require 'json'
require 'faye/websocket'
require 'eventmachine'
require 'sinatra'

Thread.new do
  EM.run do
  end
end

get '/' do
  if params.key?('code')
    rc = JSON.parse(HTTP.post('https://slack.com/api/oauth.access', params: {
      client_id: ENV['SLACK_CLIENT_ID'],
      client_secret: ENV['SLACK_CLIENT_SECRET'],
      code: params['code']
    }))

    token = rc['bot']['bot_access_token']

    rc = JSON.parse(HTTP.post('https://slack.com/api/rtm.start', params: {
      token: token
    }))

    url = rc['url']

    ws = Faye::WebSocket::Client.new(url)

    ws.on :open do
      p [:open]
    end

    ws.on :message do |event|
      data = JSON.parse(event.data)
      p [:message, JSON.parse(event.data)]
      if data['type'] == 'message' && data['text'] == 'hi'
        ws.send({ type: 'message', text: "hi <@#{data['user']}>", channel: data['channel'] }.to_json)
      end
    end

    ws.on :close do |event|
      p [:close, event.code, event.reason]
      ws = nil
      EM.stop
    end

    "Team Successfully Registered"
  else
    "Hello World"
  end
end

See serivce.rb for a working example.

Run

Run the application with bundle exec ruby service.rb and open index.html in a browser to register the app.