crystal-slack v0.0.1

Slack integration for apps and bots - new features to be added


This shard is designed to make creating apps for slack in crystal easy... version 0 currently only supports sending/receiving messages via the slack Events API. Keep an eye on the repo to see when new features are coming.


  1. Add the dependency to your shard.yml:

         github: leahpleurodon/crystal-slack
  2. Run shards install


Crystal-slack shard is aimed to be as unopinionated as possible while still being user friendly...

Your bot can just listen for events and it can respond to them.


You can create several bots in your app if you so choose.

To create a bot, your bot requires a slack bot token, to acquire one, follow these instructions, (the creating a bot section and installing to workspace section, sections 1 and 3).

  bot =, optional-name)


Commands are actions that the bot performs when it picks up specified events...

Creating commands is super easy, create a class that inherits from the command class... the call method must be over-ridden becausen it is the method the server will run when the command is called.

  require "crystal-slack"
  class CustomCommand < Command
      def call(event : Slack::Api::Event)
          #do some really cool stuff
          #pretty much anything goes

To create the command object from our custom command that the bot will interact with create the following, NAME), 

The above command will listen for any messages that equal exactly "hello..." you may put regex in this field to match dynamically... the action that will occur as a result of the command being matched is specified in the CustomCommand class as shown above.

Command Types

The CommandTypes are especially fun, they save a little bit of string interpolation/regex by allowing you to specify whether or not you want the bots name/id to be called before the command.

The two command types are CommandType::DEMAND and CommandType::HEAR

Using CommandType::HEAR means that the bot does not need to be called in the message, think of it as an "as is" matcher.

Using CommandType::DEMAND allows you to specify that the bot needs to be called specifically using it's @handle or it's display name before the pattern/ string to match. e.g. for the command below, the message "hello..." will not match... in order to invoke this command the message would have to read "@botname hello..." or bot-display-name hello...
    bot:, NAME), 
    command_type: CommandType::DEMAND, 
    matcher: "hello..." 


Now the real stuff begins... so you want your bot to say stuff...who doesn't? This is where the Response class comes in...

You can create a response and make it post at any time using the post! command on the Response

e.g the following repsonse sends "is it me you're looking for" to the specified channel...
              text: "is it me you're looking for",
              channel: CHANNELID,
              bot: BOT,
              timestamp: "0"

The timestamp is the only real gotcha here, in order to post in the channel and not in a thread the time stamp must be "0" otherwise the timestamp of the thread as a string is required.


Crystal slack comes with a logger in built into the App singleton class... the default log level is warning...

to change the log level use following method:

  App.singleton.set_log_level(severity: Severity::INFO)

Severity levels are defined by crystal see here for more info.

Putting it all together...

Here I have a simple app with a bot that response to any messages that equal "hello..." with "is it me you're looking for"
require "crystal-slack"
require "./richie_command"

module RichieBot
  app = App.singleton
  command =
require "crystal-slack"
class RichieCommand < Command
    def call(event : Slack::Api::Event)
            text: "is it me you're looking for",
            channel: event.event_channel,
            bot: @bot,
            timestamp: "0"
        ).post!"posted the response 'is it me you\'re looking for' in the channel: #{event.event_channel}")

Slack set up.

You need to create a slack app and add a bot user. Following that you need to enable the events api see this slack article on how to do so.

The following events are currently supported by the shard:

  • message.channels

    A message was posted to a channel

  • message.groups

    A message was posted to a private channel


    A message was posted in a direct message channel


A message was posted in a multiparty direct message channel

Testing your app

What you will notice is that the events API the events api requires an endpoint to send the events to... if you wish to test locally rather than in the cloud, this article is a useful article written by slack on how to do so.


  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 a new Pull Request


  • Leah - creator and maintainer
github statistic
  • 2
  • 1
  • 0
  • 1
  • 7 months ago
  • July 3, 2019

GNU Affero General Public License v3.0

Synced at

Fri, 28 Feb 2020 20:12:32 GMT