Project

botaku

0.0
No commit activity in last 3 years
No release in over 3 years
A Slack bot abstraction, built on top of faye-websocket and httpclient.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

botaku

A Slack bot abstraction, built on top of faye-websocket and httpclient.

use

Botaku provides Botaku::Client and Botaku::Bot.

Botaku::Client

require 'botaku'

#c = Botaku::Client.new(token: 'xxxx-111111111111-aaaaaaaaaaaaaaaaaaaaaaaa')
  # or
c = Botaku::Client.new(token: '.slack_api_token')

c.on('hello') do
  p [ :hello ]
  c.say('born to be alive...', '#test')
end
c.on('message') do |data|
  p [ :message, data ]
  c.say('pong', channel: data['channel']) if data['text'].match(/\A\s*ping\b/)
end

(See test/client0.rb)

Botaku::Bot

require 'botaku'

class ZeroBot < Botaku::Bot

  def on_hello

    p [ :on_hello, _self['id'] ]

    say(
      "I am #{name} (#{self.class}) from #{`uname -a`}...",
      '#test')
  end

  def on_message(data)

    typing(data['channel'])
    say("#{data['uname']} said: #{data['text'].inspect}", data['channel'])
  end
end

#ZeroBot.new(token: 'xxxx-111111111111-aaaaaaaaaaaaaaaaaaaaaaaa')
  # or
ZeroBot.new(token: 'test/.slack_api_token').run

(See test/bot0.rb)

Botaku::Bot#on_message

Upon receiving a message in a channel it participates to, a bot will consider all its on_message[...] methods sorted in alphabetical order and call each of them, until one of them returns true.

For example, this bot will invoke #on_message then #on_message_b and stop since #on_message_b returns true.

require 'botaku'

class OneBot < Botaku::Bot

  def on_message(data)

    say("0 #{data['uname']} said: #{data['text'].inspect}", data['channel'])
  end

  def on_message_b(data)

    say("1 #{data['uname']} said: #{data['text'].inspect}", data['channel'])

    true # stops calling the on_message_xxx chain
  end

  def on_message_c(data)

    say("1 #{data['uname']} said: #{data['text'].inspect}", data['channel'])
  end
end

OneBot.new(token: 'test/.slack_api_token').run

(See test/bot1.rb)

Here is perhaps a better example:

require 'botaku'

class TwoBot < Botaku::Bot

  def on_message_cheese(data)

    if data['text'].match(/\bcheese\b/i)
      say(
        "We have some Gruyères or some Vacherin Fribourgeois, " +
        "would you like to order some?",
        data['channel'])
      true # stop looking at #on_message...
    end
  end

  def on_message_wine(data)

    if data['text'].match(/\b(wine|red)\b/i)
      say("Sorry #{data['uname']}, we're out of wine", data['channel'])
      true # stop looking at #on_message...
    end
  end
end

TwoBot.new(token: 'test/.slack_api_token').run

(See test/bot2.rb)

Botaku::Bot#on_command_xxx

Here is a bot that accepts 3 commands, weather, args, and echo:

require 'botaku'

class ThreeBot < Botaku::Bot

  def on_command_weather(data)

    say("it's fine today", data['channel'])
  end

  # The `data['match']` hash contains the array of
  # arguments of the command

  def on_command_args(data)

    say(data['match'].inspect, data['channel'])
  end

  # The `data['line']` hash contains the rest of the string
  # after the command

  def on_command_echo(data)

    say(data['line'], data['channel'])
  end
end

ThreeBot.new(token: 'test/.slack_api_token').run

license

MIT, see LICENSE.txt.