diff --git a/.formatter.exs b/.formatter.exs index 8632f81..6d844ed 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -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 ] ] diff --git a/.travis.yml b/.travis.yml index 6280f93..b3a754b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: elixir elixir: - - 1.7 + - 1.9 otp_release: - - 21.0 + - 22.0 cache: directories: - _build diff --git a/lib/ex_gram/bot.ex b/lib/ex_gram/bot.ex index c54fab4..60a8e06 100644 --- a/lib/ex_gram/bot.ex +++ b/lib/ex_gram/bot.ex @@ -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() @@ -72,6 +73,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{ @@ -126,6 +129,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 diff --git a/lib/ex_gram/macros.ex b/lib/ex_gram/macros.ex index 9eadf8d..35dcfdc 100644 --- a/lib/ex_gram/macros.ex +++ b/lib/ex_gram/macros.ex @@ -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]}, [], []} diff --git a/lib/ex_gram/middleware/builder.ex b/lib/ex_gram/middleware/builder.ex index e678bc7..f55dc15 100644 --- a/lib/ex_gram/middleware/builder.ex +++ b/lib/ex_gram/middleware/builder.ex @@ -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 diff --git a/templates/bot.ex b/templates/bot.ex index 273d0d8..2a5a7ce 100644 --- a/templates/bot.ex +++ b/templates/bot.ex @@ -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