Skip to content

Latest commit

 

History

History
96 lines (73 loc) · 1.69 KB

File metadata and controls

96 lines (73 loc) · 1.69 KB

Web API Basics

Web API reference is here. It includes methods that manipulate channels, chats, emojis, files, groups, ims, pins, reactions, search, stars, teams and users.

Make a Team

Sign up at slack.com.

Create a New Bot Integration

This is something done in Slack, under integrations. Create a new bot, and note its API token.

Gemfile

source 'http://rubygems.org'

Test

require 'http'
require 'json'

rc = HTTP.post("https://slack.com/api/api.test")
puts JSON.pretty_generate(JSON.parse(rc.body))
{
  "ok": true
}

Auth

Failing Auth

rc = HTTP.post("https://slack.com/api/auth.test")
puts JSON.pretty_generate(JSON.parse(rc.body))
{
  "ok": false,
  "error": "not_authed"
}

Succeeding Auth

rc = HTTP.post("https://slack.com/api/auth.test", params: { token: ENV['SLACK_API_TOKEN'] })
puts JSON.pretty_generate(JSON.parse(rc.body))
{
  "ok": true,
  "url": "https://dblockdotorg.slack.com/",
  "team": "dblock",
  "user": "rubybot",
  "team_id": "T04KB5WQH",
  "user_id": "U07518DTL"
}

Messages

HTTP.post("https://slack.com/api/chat.postMessage", params: {
  token: ENV['SLACK_API_TOKEN'],
  channel: '#general',
  text: 'hello world'
})
{
  "ok": true,
  "channel": "C04KB5X4D",
  "ts": "1444747472.000002",
  "message": {
    "text": "hello world",
    "username": "bot",
    "type": "message",
    "subtype": "bot_message",
    "ts": "1444747472.000002"
  }
}

Post with as_user: true on behalf of the bot user.