Igor v0.3.0 Igor.Responder.ModuleHelper behaviour View Source

Helper for writing responder modules.

Responses can be defined by using the command or listen macros. If those macros are not sufficient, responses can be defined by defining a respond/2 function, where the first argument is an Igor.Message and the second argument is an Igor.

Responses can be documented using the @help attribute. These will show up when a user uses the help command, if the Igor.Responder.Help module is used. The value should be a map that can contain the following keys:

  • command :: String.t() | [String.t()] (required): the command that is implemented. If it is a sub-command, then this should be a list. Otherwise, it can be a string or a 1-element list. For example, a GitLab responder could implement a gitlab create command, in which case, this would be ["gitlab", "create"].
  • args :: Polyjuice.Client.MsgBuilder.MsgData.t() (optional): the arguments that the command takes.
  • short :: Polyjuice.Client.MsgBuilder.MsgData.t() (optional): a brief description of what the command does. This will be shown when displaying the list of commands that the bot understands.
  • long :: Polyjuice.Client.MsgBuilder.MsgData.t() (optional): a detailed description of what the command does. This will be shown when the user requests help for the command.

Example

defmodule Ping do
  use Igor.Responder

  @help %{command: "ping", args: "[response]", short: "respond with a pong"}
  command ["ping"], msg, bot do
    "pong" |> Igor.send(msg, bot)
  end
end

More complicated responders (e.g. that require state) can be created by writing a module that implements the Igor.Responder.Proto protocol.

Link to this section Summary

Functions

Defines a command that the bot understands.

Respond to messages that match a regular expression.

Callbacks

Invoked to get the help messages.

Invoked to get the help message for a certain command.

Invoked to respond to a message.

Link to this section Functions

Link to this macro

command(pattern, msg, bot, list)

View Source (macro)

Defines a command that the bot understands.

A command is represented as a list of strings, and can be pattern matched.

Examples

Define a command that adds numbers together.

command(["add" | items], msg, bot) do
  Enum.map(items, &Float.parse)
    |> Enum.reduce(fn x, acc -> x + acc end)
    |> to_string
    |> Igor.send(msg, bot)
end

A command can have multiple syntaxes.

command(["roll"], msg, bot) do
  # if no argument is given, roll a d6
  :rand.uniform(6) |> to_string |> Igor.send(msg, bot)
end
command(["roll" | dice], msg, bot) do
  # if arguments are given, roll the specified dice
  dice |> Enum.flat_map(fn spec ->
      {times, max} = parse_spec(spec) # parse_spec is left as an exercise for the reader
      Enum.map 1..times, fn _ -> :rand.uniform(max) |> to_string end
    end)
    |> Enum.join(" ")
    |> Igor.send(msg, bot)
end
Link to this macro

listen(regexp, matches, msg, bot, list)

View Source (macro)

Respond to messages that match a regular expression.

Examples

Respond to messages that contain #(number) with a link to the issue tracker.

listen ~r/#(\d+)/, matches, msg, bot do
  Enum.map(
    matches,
    fn [_, issuenum] ->
      ["https://gitlab.com/uhoreg/igor/issues/", issuenum]
    end
  )
end
|> intersperse("\n")
|> Igor.send(msg, bot)

Link to this section Callbacks

Link to this callback

help(cmdprefix)

View Source
help(cmdprefix :: String.t()) :: [String.t()]

Invoked to get the help messages.

Link to this callback

help(cmdprefix, cmd)

View Source
help(cmdprefix :: String.t(), cmd :: String.t()) :: String.t() | nil

Invoked to get the help message for a certain command.

Link to this callback

respond(msg, bot)

View Source
respond(msg :: Igor.Message.t(), bot :: Igor.t()) :: any()

Invoked to respond to a message.