Skip to content

Commit

Permalink
Merge pull request #49 from rockneurotiko/setup_bot_commands
Browse files Browse the repository at this point in the history
Setup bot commands on startup
  • Loading branch information
rockneurotiko committed Apr 13, 2020
2 parents fdb364f + a93fce8 commit ad8252b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,17 @@
"lib/**/*.{ex,exs}",
"test/**/*.{ex,exs}",
"mix.exs"
],
locals_without_parens: [
command: 1,
command: 2,
middleware: 1,
answer: 2,
answer: 3,
answer: 4,
edit: 5,
row: 1,
button: 1,
button: 2
]
]
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: elixir
elixir:
- 1.7
- 1.9
otp_release:
- 21.0
- 22.0
cache:
directories:
- _build
Expand Down
21 changes: 21 additions & 0 deletions lib/ex_gram/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule ExGram.Bot do
end

username = Keyword.fetch(ops, :username)
setup_commands = Keyword.get(ops, :setup_commands, false)

commands = quote do: commands()

Expand Down Expand Up @@ -64,6 +65,8 @@ defmodule ExGram.Bot do
other
end

maybe_setup_commands(unquote(setup_commands), unquote(commands), token)

bot_info = maybe_fetch_bot(unquote(username), token)

dispatcher_opts = %ExGram.Dispatcher{
Expand Down Expand Up @@ -118,6 +121,24 @@ defmodule ExGram.Bot do
_ -> nil
end
end

defp maybe_setup_commands(true, commands, token) do
send_commands =
commands
|> Stream.filter(fn command ->
not is_nil(command[:description])
end)
|> Enum.map(fn command ->
%ExGram.Model.BotCommand{
command: command[:command],
description: command[:description]
}
end)

ExGram.set_my_commands(send_commands, token: token)
end

defp maybe_setup_commands(_, _commands, _token), do: :nop
end
end
end
4 changes: 2 additions & 2 deletions lib/ex_gram/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ defmodule ExGram.Macros do

def orT(x, y), do: {:|, [], [x, y]}

def nameAssignT(n, t) when is_atom(n), do: {:::, [], [type_to_spec(n), t]}
def nameAssignT(n, t), do: {:::, [], [n, t]}
def nameAssignT(n, t) when is_atom(n), do: {:"::", [], [type_to_spec(n), t]}
def nameAssignT(n, t), do: {:"::", [], [n, t]}

def type_to_spec(:string),
do: {{:., [], [{:__aliases__, [alias: false], [:String]}, :t]}, [], []}
Expand Down
7 changes: 6 additions & 1 deletion lib/ex_gram/middleware/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ defmodule ExGram.Middleware.Builder do

defmacro command(command, opts \\ []) do
name = Keyword.get(opts, :name, String.to_atom(command))
description = Keyword.get(opts, :description)

quote do
@commands [command: unquote(command), name: unquote(name)]
@commands [
command: unquote(command),
name: unquote(name),
description: unquote(description)
]
end
end

Expand Down
12 changes: 10 additions & 2 deletions templates/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ defmodule <%= app_module %>.Bot do
@bot <%= inspect(app) %>

use ExGram.Bot,
name: @bot
name: @bot,
setup_commands: true

command("start")
command("help", description: "Print the bot's help")

middleware(ExGram.Middleware.IgnoreUsername)

def bot(), do: @bot

def handle({:command, "start", _msg}, context) do
def handle({:command, :start, _msg}, context) do
answer(context, "Hi!")
end

def handle({:command, :help, _msg}, context) do
answer(context, "Here is your help:")
end
end

0 comments on commit ad8252b

Please sign in to comment.