All Projects → hedwig-im → Hedwig

hedwig-im / Hedwig

Licence: mit
An Adapter-based Bot Framework for Elixir Applications

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Hedwig

Go Sarah
Simple yet customizable bot framework written in Go.
Stars: ✭ 188 (-69.13%)
Mutual labels:  bot, slack, xmpp
Phabulous
A Phabricator bot for Slack
Stars: ✭ 222 (-63.55%)
Mutual labels:  bot, slack
Serverless Slack App
A Serverless.js Slack App Boilerplate with OAuth and Bot actions
Stars: ✭ 217 (-64.37%)
Mutual labels:  bot, slack
Intelligo
🤖 Chatbot Framework for Node.js.
Stars: ✭ 347 (-43.02%)
Mutual labels:  bot, slack
Flottbot
A chatbot framework written in Go. All configurations are made in YAML files, or inside scripts written in your favorite language.
Stars: ✭ 175 (-71.26%)
Mutual labels:  bot, slack
Hubot Slack
Slack Developer Kit for Hubot
Stars: ✭ 2,260 (+271.1%)
Mutual labels:  bot, slack
Slack Meme
A Meme Bot for Slack.
Stars: ✭ 322 (-47.13%)
Mutual labels:  bot, slack
Office Simulator
Miss the office life? You won't any more with this wonderful office slack simulator.
Stars: ✭ 152 (-75.04%)
Mutual labels:  bot, slack
Pokemongo Bot
The Pokemon Go Bot, baking with community.
Stars: ✭ 3,730 (+512.48%)
Mutual labels:  bot, slack
Matterbridge
bridge between mattermost, IRC, gitter, xmpp, slack, discord, telegram, rocketchat, twitch, ssh-chat, zulip, whatsapp, keybase, matrix, microsoft teams, nextcloud, mumble, vk and more with REST API (mattermost not required!)
Stars: ✭ 4,452 (+631.03%)
Mutual labels:  slack, xmpp
Elixir Slack
Slack real time messaging and web API client in Elixir
Stars: ✭ 587 (-3.61%)
Mutual labels:  bot, slack
Slack Starterbot
Python-powered simple starter Slack bot.
Stars: ✭ 169 (-72.25%)
Mutual labels:  bot, slack
Joe
A general-purpose bot library inspired by Hubot but written in Go. 🤖
Stars: ✭ 417 (-31.53%)
Mutual labels:  bot, slack
Sactive Bot
😈 An extensible chat bot framework. sactive-bot is an evolution of the open source hubot project. - https://www.shipengqi.top/sactive-bot .
Stars: ✭ 212 (-65.19%)
Mutual labels:  bot, slack
Php Slack Bot
Slack bot user written in PHP
Stars: ✭ 161 (-73.56%)
Mutual labels:  bot, slack
st2chatops
Packaging environment for building StackStorm chatops native packages
Stars: ✭ 26 (-95.73%)
Mutual labels:  slack, xmpp
Trebekbot
An addictive Jeopardy! bot for Slack. Fun fact, after I added this to my work Slack I was told to limit it to a single channel because productivity ground to a halt. (Five years later, the #jeopardy channel is still going strong.)
Stars: ✭ 147 (-75.86%)
Mutual labels:  bot, slack
Slick
Slick, a Slack bot in Go
Stars: ✭ 150 (-75.37%)
Mutual labels:  bot, slack
Bottender
⚡️ A framework for building conversational user interfaces.
Stars: ✭ 3,803 (+524.47%)
Mutual labels:  bot, slack
Awesome Bots
Awesome Links about bots.
Stars: ✭ 412 (-32.35%)
Mutual labels:  bot, slack

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:

defp deps do
  [{:hedwig, "~> 1.0"}]
end

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.

def applications do
  [applications: [:hedwig]]
end

Fetch the dependencies:

$ mix 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:

$ mix hedwig.gen.robot

Welcome to the Hedwig Robot Generator!

Let's get started.

What would you like to name your bot?: alfred

Available adapters

1. Hedwig.Adapters.Console

Please select an adapter: 1

* creating lib/alfred
* creating lib/alfred/robot.ex
* updating config/config.exs

Don't forget to add your new robot to your supervision tree
(typically in lib/alfred.ex):

    worker(Alfred.Robot, [])
defmodule Alfred.Robot do
  use Hedwig.Robot, otp_app: :alfred

  ...
end

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.

# config/config.exs

config :alfred, Alfred.Robot,
  adapter: Hedwig.Adapters.Console,
  name: "alfred",
  aka: "/",
  responders: [
    {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:

# add this to the list of your supervisor's children
worker(Alfred.Robot, [])

Trying out the console adapter:

mix run --no-halt

Hedwig Console - press Ctrl+C to exit.

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

scrogson> alfred help
alfred> alfred help <query> - Displays all help commands that match <query>.
alfred help - Displays all of the help commands that alfred knows about.
alfred: ping - Responds with 'pong'
scrogson>

Starting bots manually:

# Start the bot via the module. The configuration options will be read in from
# config.exs
{:ok, pid} = Hedwig.start_robot(Alfred.Robot)

# You can also pass in a list of options that will override the configuration
# provided in config.exs (except for the adapter as that is compiled into the
# module).
{:ok, 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:

defmodule Alfred.Robot do
  use Hedwig.Robot, otp_app: :alfred

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

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

# Start the robot
Hedwig.start_robot(Alfred.Robot)
# Get the pid of the robot by name
pid = :global.whereis_name("alfred")

# Start a new robot with a different name
Hedwig.start_robot(Alfred.Robot, [name: "jeeves"])
# Get the pid
pid = :global.whereis_name("jeeves")
# Stop the robot
Hedwig.stop_robot(pid)

Sending Messages

# Get the pid of the robot
pid = :global.whereis_name("alfred")

# Create a Hedwig message
msg = %Hedwig.Message{
  type: "groupchat",
  room: "[email protected]",
  text: "hello world"
}

# Send the message
Hedwig.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:

defmodule MyApp.Responders.GreatSuccess do
  @moduledoc """
  Borat, Great Success!

  Replies with a random link to a Borat image when a message contains
  'great success'.
  """

  use Hedwig.Responder

  @links [
    "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"
  ]

  @usage """
  <text> (great success) - Replies with a random Borat image.
  """
  hear ~r/great success(!)?/i, msg do
    reply msg, random(@links)
  end
end

Hear vs. Respond

The two responder macros are use for different reasons:

  • hear - matches messages containing the regular expression
  • respond - matches only when prefixed by your robot's configured name or aka value.

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:

# test/my_app/responders/great_success_test.exs

defmodule MyApp.Responders.GreatSuccessTest do
  use Hedwig.RobotCase

  @tag start_robot: true, name: "alfred", responders: [{MyApp.Responders.GreatSuccess, []}]
  test "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")
  end
end

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].