hedwig-im/hedwig

Name: hedwig

Owner: Hedwig IM

Description: An Adapter-based Bot Framework for Elixir Applications

Created: 2014-03-31 04:16:17.0

Updated: 2018-01-17 15:50:21.0

Pushed: 2018-01-08 15:32:03.0

Homepage:

Size: 374

Language: Elixir

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Hedwig

An Adapter-based Bot Framework for Elixir Applications

Build Status Coverage Status

Hedwig

Hedwig is a chat bot, highly inspired by GitHub's Hubot.

See the online documentation for more information.

Hedwig was designed for 2 use-cases:

  1. A single, stand-alone OTP application.
  2. Included as a dependency of other OTP applications (or an umbrella).

You can spawn multiple bots at run-time with different configurations.

Adapters

Check out enilsen16/awesome-hedwig for a curated list of adapters, responders, and other resources.

Getting started

Hedwig ships with a console adapter to get you up and running quickly. It's great for testing how your bot will respond to the messages it receives.

To add Hedwig to an existing Elixir application, add :hedwig to your list of dependencies in your mix.exs file:

 deps do
:hedwig, "~> 1.0"}]

Update your applications list to include :hedwig. This will ensure that the Hedwig application, along with it's supervision tree is started when you start your application.

applications do
pplications: [:hedwig]]

Fetch the dependencies:

x deps.get
Create a robot module

Hedwig provides a convenient mix task to help you generate a basic robot module.

Run the following and follow the prompts:

x hedwig.gen.robot

ome to the Hedwig Robot Generator!

s get started.

 would you like to name your bot?: alfred

lable adapters

edwig.Adapters.Console

se select an adapter: 1

eating lib/alfred
eating lib/alfred/robot.ex
dating config/config.exs

t forget to add your new robot to your supervision tree
ically in lib/alfred.ex):

worker(Alfred.Robot, [])
lixir
odule Alfred.Robot do
e Hedwig.Robot, otp_app: :alfred

.

Configuration

The generator will automatically generate a default configuration in config/config.exs. You will need to customize it further depending on the adapter you will use.

This is mainly to setup the module to be compiled along with the adapter. An adapter can inject functionality into your module if needed.

nfig/config.exs

ig :alfred, Alfred.Robot,
apter: Hedwig.Adapters.Console,
me: "alfred",
a: "/",
sponders: [
{Hedwig.Responders.Help, []},
{Hedwig.Responders.Ping, []}

Start a bot.

You can start your bot as part of your application's supervision tree or by using the supervision tree provided by Hedwig.

Starting as part of your supervision tree:
d this to the list of your supervisor's children
er(Alfred.Robot, [])
Trying out the console adapter:
run --no-halt

ig Console - press Ctrl+C to exit.

console adapter is useful for quickly verifying how your
will respond based on the current installed responders.

gson> alfred help
ed> alfred help <query> - Displays all help commands that match <query>.
ed help - Displays all of the help commands that alfred knows about.
ed: ping - Responds with 'pong'
gson>
Starting bots manually:
art the bot via the module. The configuration options will be read in from
nfig.exs
, pid} = Hedwig.start_robot(Alfred.Robot)

u can also pass in a list of options that will override the configuration
ovided in config.exs (except for the adapter as that is compiled into the
dule).
, pid} = Hedwig.start_robot(Alfred.Robot, [name: "jeeves"])
Registering your robot process

If you want to start, stop, and send messages to your bot without keeping track of its pid, you can register your robot in the handle_connect/1 callback in your robot module like so:

odule Alfred.Robot do
e Hedwig.Robot, otp_app: :alfred

f handle_connect(%{name: name} = state) do
if :undefined == :global.whereis_name(name) do
  :yes = :global.register_name(name, self())
end
{:ok, state}
d

Process registration via Process.register/2 is simple. However, since the name can only be an atom it may not work for all use-cases. If you are using the same module for many robots, you'll need to reach for something more flexible like:

Finding your robot
art the robot
ig.start_robot(Alfred.Robot)
t the pid of the robot by name
= :global.whereis_name("alfred")

art a new robot with a different name
ig.start_robot(Alfred.Robot, [name: "jeeves"])
t the pid
= :global.whereis_name("jeeves")
op the robot
ig.stop_robot(pid)
Sending Messages
t the pid of the robot
= :global.whereis_name("alfred")

eate a Hedwig message
= %Hedwig.Message{
pe: "groupchat",
om: "my_room@example.com",
xt: "hello world"


nd the message
ig.Robot.send(pid, msg)
Building Responders

Responders are processes that will handle incoming messages.

All that's needed is to use Hedwig.Responder and use the hear/2, or respond/2 macros to define a pattern to listen for and how to respond in the block when a message matches.

Here is an example:

odule MyApp.Responders.GreatSuccess do
oduledoc """
rat, Great Success!

plies with a random link to a Borat image when a message contains
reat success'.
"

e Hedwig.Responder

inks [
"http://mjanja.co.ke/wordpress/wp-content/uploads/2013/09/borat_great_success.jpg",
"http://s2.quickmeme.com/img/13/1324dfd733535e58dba70264e6d05c9b70346204d2cacef65abef9c702746d1c.jpg",
"https://www.youtube.com/watch?v=r13riaRKGo0"


sage """
ext> (great success) - Replies with a random Borat image.
"
ar ~r/great success(!)?/i, msg do
reply msg, random(@links)
d

Hear vs. Respond

The two responder macros are use for different reasons:

Testing responders:

Hedwig ships with a ExUnit-based module sepecifically made to test responders: Hedwig.RobotCase.

In order to test the above responder, you need to create an ExUnit test case:

st/my_app/responders/great_success_test.exs

odule MyApp.Responders.GreatSuccessTest do
e Hedwig.RobotCase

ag start_robot: true, name: "alfred", responders: [{MyApp.Responders.GreatSuccess, []}]
st "great success - responds with a borat url", %{adapter: adapter, msg: msg} do
send adapter, {:message, %{msg | text: "great success"}}
assert_receive {:message, %{text: text}}
assert String.contains?(text, "http")
d

To run the tests, use mix test

@usage

The @usage module attribute works nicely with Hedwig.Responders.Help. If you install the help handler, your bot will listen for <your-bots-nickname> help and respond with a message containing all of the installed handlers @usage text.

License

The MIT License (MIT)

Copyright (c) 2015 Sonny Scroggin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.