Simple slack bot framework.
Latest commit 4bb0691 Dec 8, 2016 @wakaba260 Bump up version 0.6.3


Gem Version Build Status Code Climate Test Coverage Dependency Status

This gem support to make slack bot, inspire from hubot and sinatra.

Use the Slack Realtime API and Web API(see the official-documentation).

Quick Start

1. Generate Slack API Token

Slack API Token generate from official page.

2. Set environment variable to Slack API Token

Slappy references ENV['SLACK_TOKEN'] default.

3. Run generator

Execute then command to generate project:

$ slappy new project-name

If you want to use current directory, execute then command:

$ slappy new

(If you use bundler, add bundle exec prefix)

4. Write code

Create ruby file to under project-name/slappy-scripts, and written code.


# catch pattern
hear '^hello, slappy!' do |event| 'received message!'
  say 'hello!!', channel: #=> respond message to channel

There scripts not share scopes with other scripts.

5. Slappy start

Execute then command in project root directory.

$ slappy start

(Stop: Input Ctrl+c)


Add this line to your application's Gemfile:

gem 'slappy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install slappy



Store configuration value in environment variables. They are easy to change between deploys without changing any code.

SLACK_TOKEN - required (when not configured)


Configure default settings. There configrations effect on send message to slack when use say method and should override when option given.


require 'slappy'

Slappy.configure do |config|
  config.token = 'foobar'
  config.robot.username   = 'slappy'    = '#general'
  config.robot.icon_emoji = ':slappy:'

Slappy.say 'hello!' #=> username: slappy, channel: '#general', icon_emoji: ':slappy:'

Configuration Parameters

token            - default: ENV['SLACK_TOKEN']
scripts_dir_path - default : 'slappy-scripts'
stop_with_error  - default: true

robot.botname    - not effect now
robot.username   - default: 'slappy'
robot.icon_emoji - default: nil    - default: '#general'
robot.icon_url   - default: nil
robot.as_user    - default: false

Basic Usage

# called when start up
hello do 'successfly connected'

Send and Receive Message

# called when match message
hear 'foo' do 'foo'

# use regexp in string literal
hear 'bar (.*)' do |event| event.matches[1] #=> Event#matches return MatchData object

# event object is slack event JSON (convert to Hashie::Mash)
hear '^bar (.*)' do |event|
  say 'slappy!', channel:  #=> to received channel object
  say 'slappy!', channel: '#general'     #=> send to public channel
  say 'slappy!', channel: 'private-room' #=> send to private group
  say 'slappy!', username: 'slappy!', icon_emoji: ':slappy:' #=> change config

# use regexp literal
hear(/^foobar/) do 'slappppy!'

Monitoring Types Event

monitor 'channel_joined' do |event|
  say "Welcome to #{}!", channel:

monitor 'team_join' do |event|
  say "Welcome to this team!", channel: "#general"

Schedule Event

schedule '* * * * *' do 'Slappy alive...' #=> Repeat every minutes.

Sleep Event

There conditions, this event be called:

  • raise StandardError in script
  • trap SIGTERM or SIGINT
goodnight do 'goodnight'

From Option

From Option enable to hear, monitor, and respond. This option specify reaction target!

# specify target channel
hear 'slappy', from: { channel: '#slappy' } do |event| 'slappy!'

# specify target user
hear 'slappy', from: { username: '@slappy' } do |event| 'slappy!'

# specify target user and channel
hear 'slappy', from: { channel: '#slappy', username: '@slappy' } do |event| 'slappy!'

DSL Methods

method when execute callback
hello start up
hear message (match pattern)
respond message (match pattern and botname prefix given)
monitor Slack RTM event
schedule specify time (Syntax is here) - Thanks to Chrono!
goodinight bot dead (StandardError, SIGTERM, and SIGINT received)

Event Methods

hear 'slappy' do |event|
  return if event.bot_message? #=> check message from webhook or integration
  event.relpy 'slappy'         #=> relpy to event channel
  event.reaction 'thumbsup'    #=> add reaction to event message
method description
data get response JSON Hash (Hashie::Mash) from Slack API
bot_message? check message from bot (webhook or integration is true)
reply reply message to event channel
reaction add reaction to event message

In your Application

If you not want execute slappy start command, written by (require 'slappy/dsl' use DSL):

require 'slappy'

Slappy.hello do
  # In your code..

Slappy.say 'message to slack'

Slappy.start #=> Start WebSocket connection

Release Note

  • v0.6.3

    • Fix did not work monitor reaction_added RTM (contribute from noir-neo #43)
    • Fix option name in configuration template (contribute from Tomohiro #42)
  • v0.6.2

    • Fix dsl config (contribute from Sho2010 #41)
  • v0.6.1

    • Fix ts validation
      • validation skil when ts be nil
    • Fix option be bang change in Messenger#initialize
    • Add Event#data
    • Add stop_with_error option
  • v0.6.0

    • Add from option to hear, monitor
      • specify event target
    • respond method
      • respond event call when add message to botname prefix
    • goodnight method
      • goodnight event call when bod dead (StandardError, SIGTERM, and SIGINT received)
    • New SlackAPI
      • File
      • Pin
    • New Event methods
      • bot_message? (contribute from dnond)
        • check message from bot(webhook, and integration)
      • reaction
        • reaction to event(add emoji reaction)
      • reply
        • relpy to event
  • v0.5.2

    • Add debug logging
    • Fix schedule id is not normality
  • v0.5.1

    • Fix monitor event disabled
  • v0.5.0

    • Support Schedule
    • Support Slack RMT Event
    • Support private group
    • Support as_user option
    • Introduce SlackAPI objects
      • Group
      • Channel
      • Direct
      • User
  • v0.4.0

    • Support logger
    • Support load lib directory
    • Add New command
      • version
    • Choise dsl enabled in slappy_config
  • v0.3.0

    • Introduce DSL
    • Introduce CLI commands
      • start
      • new
    • Use String literal for hear parameter
    • Chose dsl use in slappy_config
  • v0.2.0

    • Modify interface
  • v0.1.0

    • Release


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request


The gem is available as open source under the terms of the MIT License.