Giter Club home page Giter Club logo

hedwig_slack's Introduction

Hedwig Slack Adapter

A Slack Adapter for Hedwig

Build Status

Getting started

Let's generate a new Elixir application with a supervision tree:

λ mix new alfred --sup
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/alfred.ex
* creating test
* creating test/test_helper.exs
* creating test/alfred_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd alfred
    mix test

Run "mix help" for more commands.

Change into our new application directory:

λ cd alfred

Add hedwig_slack to your list of dependencies in mix.exs:

def deps do
  [{:hedwig_slack, "~> 1.0"}]
end

Ensure hedwig_slack is started before your application:

def application do
  [applications: [:hedwig_slack]]
end

Generate our robot

λ 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.Slack
2. Hedwig.Adapters.Console
3. Hedwig.Adapters.Test

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, [])

Supervise our robot

We'll want Alfred to be supervised and started when we start our application. Let's add it to our supervision tree. Open up lib/alfred.ex and add the following to the children list:

worker(Alfred.Robot, [])

Configuration

The next thing we need to do is configure our bot for our XMPP server. Open up config/config.exs and let's take a look at what was generated for us:

use Mix.Config

config :alfred, Alfred.Robot,
  adapter: Hedwig.Adapters.Slack,
  name: "alfred",
  aka: "/",
  responders: [
    {Hedwig.Responders.Help, []},
    {Hedwig.Responders.Ping, []}
  ]

So we have the adapter, name, aka, and responders set. The adapter is the module responsible for handling all of the Slack details like connecting and sending and receiving messages over the network. The name is the name that our bot will respond to. The aka (also known as) field is optional, but it allows us to address our bot with an alias. By default, this alias is set to /.

Finally we have responders. Responders are modules that provide functions that match on the messages that get sent to our bot. We'll discuss this further in a bit.

We'll need to provide a few more things in order for us to connect to our Slack server. We'll need to provide our bot's API key as well as a list of rooms we want our bot to join once connected. Let's see what that looks like:

use Mix.Config

config :alfred, Alfred.Robot,
  adapter: Hedwig.Adapters.Slack,
  name: "alfred",
  aka: "/",
  # fill in the appropriate API token for your bot
  token: "some api token",
  # for now, you can invite your bot to a channel in slack and it will join
  # automatically
  rooms: [],
  responders: [
    {Hedwig.Responders.Help, []},
    {Hedwig.Responders.Ping, []}
  ]

Great! We're ready to start our bot. From the root of our application, let's run the following:

λ mix run --no-halt

This will start our application along with our bot. Our bot should connect to Slack and join the rooms it's in based on its Slack integration. From there, we can chat with our bot in any Slack client.

Since we have the Help responder installed, we can say alfred help and we should see a list of usage for all of the installed responders.

What's next?

Well, that's it for now. Make sure to read the Hedwig Documentation for more details on writing responders and other exciting things!

LICENSE

Copyright (c) 2016, Sonny Scroggin.

Hedwig Slack source code is licensed under the MIT License.

hedwig_slack's People

Contributors

doomspork avatar knewter avatar schlenks avatar scrogson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

hedwig_slack's Issues

Access slack data

Since the adapter is a genserver, it would be handy if it could respond to queries for information about users, channels, etc. that is being stored in the state object.

Slack has deprecated the RTM API, and it won't work with new apps (error msg: `{:error, :no_scheme}`)

If you make a new Slack app and try to use Hedwig, it won't work, you'll just get this very confusing error:

[error] GenServer #PID<0.592.0> terminating
** (MatchError) no match of right hand side value: {:error, :no_scheme}
    (hedwig_slack 1.0.0) lib/hedwig_slack/connection.ex:16: HedwigSlack.Connection.start/1
    (hedwig_slack 1.0.0) lib/hedwig_slack/adapter.ex:48: Hedwig.Adapters.Slack.handle_info/2
    (stdlib 3.15.2) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.15.2) gen_server.erl:771: :gen_server.handle_msg/6
    (stdlib 3.15.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:EXIT, #PID<0.590.0>, {{:badmatch, {:error, :no_scheme}}, [{HedwigSlack.Connection, :start, 1, [file: 'lib/hedwig_slack/connection.ex', line: 16]}, {Hedwig.Adapters.Slack, :handle_info, 2, [file: 'lib/hedwig_slack/adapter.ex', line: 48]}, {:gen_server, :try_dispatch, 4, [file: 'gen_server.erl', line: 695]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 771]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 226]}]}}
State: {:state, {#PID<0.592.0>, Supervisor.Default}, :simple_one_for_one, {[Hedwig.Responder], %{Hedwig.Responder => {:child, :undefined, Hedwig.Responder, {Hedwig.Responder, :start_link, []}, :transient, false, 5000, :worker, [Hedwig.Responder]}}}, {:maps, %{#PID<0.593.0> => [Hedwig.Responders.Help, {nil, "DubzCat", [], #PID<0.590.0>}], #PID<0.594.0> => [Hedwig.Responders.Ping, {nil, "DubzCat", [], #PID<0.590.0>}], #PID<0.595.0> => [DubzCat.Responders.Meow, {nil, "DubzCat", [], #PID<0.590.0>}]}}, 3, 5, [], 0, :never, Supervisor.Default, {:ok, {%{intensity: 3, period: 5, strategy: :simple_one_for_one}, [{Hedwig.Responder, {Hedwig.Responder, :start_link, []}, :transient, 5000, :worker, [Hedwig.Responder]}]}}}

The issue is that Slack has deprecated the RTM API, in favour of the Events API - see https://api.slack.com/authentication/basics

If you use IO.inspect on the data you get back here https://github.com/hedwig-im/hedwig_slack/blob/master/lib/hedwig_slack/adapter.ex#L45

you'll see %{"error" => "invalid_auth", "ok" => false}

I think the only way to fix this for new apps (my old app seems to still work) will be to upgrade this adapter to not use the RTM API

Only hear when mentioned?

Its possible to only hear when the bot is mentioned unless in its own chat? This would only affect responders. I would be much nicer in slack to write @bot help instead of bot help.

Issue Connecting to RTM and WS behind Proxy

Using Hedwig behind a firewall does not connect to the Slack RTM api.

Issue:

When connecting, hedwig will receive a {:error, :nxdomain} message.

Requested Usage:
Add configuration options for proxy and accompanying settings.

Examples:
config :alfred,
Alfred.Robot,
adapter: Hedwig.Adapters.Slack,
name: "aflred",
aka: "/",
token: System.get_env("SLACK_API_TOKEN"),
rooms: [],
proxy: System.get_env("HTTP_PROXY"),
ssl_options: [insecure: true, ssl_options: [protocol: 'tlsv1.2']]
responders: [
{Hedwig.Responders.Help, []},
{Hedwig.Responders.Ping, []}
]

Hedwig Slack dependencies incompatible with Phoenix

I attempted to include Hedwig with Slack in an umbrella which also included a Phoenix project and there are dependency conflicts:

➜  omgbot mix deps.update --all
Running dependency resolution

Failed to use "cowlib" (versions 1.0.0 to 1.0.2) because
  cowboy (versions 1.0.0 to 1.0.4) requires ~> 1.0.0
  gun (version 1.0.0-pre.1) requires 1.3.0

Failed to use "poison" (versions 1.3.0 to 1.5.2) because
  ecto (version 1.1.9) requires ~> 1.0 or ~> 2.0
  hedwig_slack (version 0.1.0) requires ~> 2.0
  phoenix (versions 1.1.1 and 1.1.2) requires ~> 1.3
  phoenix_ecto (version 2.0.2) requires ~> 1.3 or ~> 2.0

Failed to use "ranch" (versions 1.2.0 and 1.2.1) because
  cowboy (versions 1.0.0 to 1.0.4) requires ~> 1.0
  gun (version 1.0.0-pre.1) requires 1.1.0

** (Mix) Hex dependency resolution failed, relax the version requirements of your dependencies or unlock them (by using mix deps.update or mix deps.unlock)

It appears that cowboy requires an older version of ranch and cowlib than gun, which is required by hedwig_slack. It also appears that the newer version of cowlib is not backwards compatible and I was unable to get it to work with cowboy. There is no more recent version of cowboy.

Not sure how I can get around this problem.

Supervisor: Simple_one_for_one strategy is deprecated

mix run --no-halt
warning: :simple_one_for_one strategy is deprecated, please use DynamicSupervisor instead
  (elixir 1.10.4) lib/supervisor.ex:604: Supervisor.init/2
  (elixir 1.10.4) lib/supervisor.ex:556: Supervisor.start_link/2
  (alfred 0.1.0) lib/hedwig/robot.ex:89: Alfred.Robot.init/1
  (stdlib 3.8) gen_server.erl:374: :gen_server.init_it/2
  (stdlib 3.8) gen_server.erl:342: :gen_server.init_it/6

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.