Slacker's an Elixir bot library for Slack.
It has chat matching functionality built-in, but you can extend it to handle all kinds of events.
Slacker can match regex or literal strings, then execute a given function (module optional).
defmoduleTARSdouseSlackeruseSlacker.Matchermatch~r/Sense of humor\. New level setting: ([0-9]+)%/,:set_humormatch"Great idea. A massive, sarcastic robot.",[CueLight,:turn_on]defset_humor(tars,msg,level)doreply="Sense of humor set to #{level}"saytars,msg["channel"],replyendendSlacker will call your function with the matching message hash. You can use say/3 to respond, be sure to include the channel you want to talk to.
Your robot is really just a GenServer, you can catch RTM events from Slack and do whatever you like with them.
defmoduleCASEdouseSlackerdefhandle_cast({:handle_incoming,"presence_change",msg},state)dosayself(),msg["channel"],"You're the man who brought us the probe?"{:noreply,state}endendYou can also use Slack's "Web API" via the Slacker.Web module. All of the available RPC methods are downcased and underscored.
users.getPresence -> Slacker.Web.users_get_presence("your_api_key", user: "U1234567890")
Add this to your deps:
defdepsdo[{:websocket_client,github: "jeremyong/websocket_client"},{:slacker,"~> 0.0.3"}]endCreate a bot user in the Slack GUI, and then pass your api token to your bot's start_link/1:
{:ok, tars} = TARS.start_link("your_api_token")
It's up to you to supervise your brand new baby bot.
You're going to need to invite your bot to a channel by @-mentioning them.
Gimme dem PR's.
Some of this stuff is a real pain in the ass to test, just do your best. 🚀
TODO:
- Keep a map of usernames to ids.
- Keep a map of channel names to ids.
- Private messaging support.
- RTM tests.
See the LICENSE file. (MIT)