Elixir Telegram Bot Boilerplate
A boilerplate for making bots for telegram using Elixir because of yes
Getting Started
- Setup you bot name and telegram bot token at
config/config.ex
You may set up environment-wide configurations at
dev.ex
,prod.ex
andtest
at theconfig/
folder if you have different bots for different environments
config :app,
bot_name: "bot_user_name"
config :nadia,
token: "abcdefg_12345678910_the_game"
-
Setup commands at
lib/app/commands.ex
-
Run at your shell
λ mix
Macros
command "foo" do
IO.inspect update
send_message "Hello Telegram"
end
The command/2
macro take a string and a block. In this case, it'll try to match
anything that starts with /foo
. Once it matches, it'll inject a constant named
update
at the scope of the do
block.
send_message/2
is a macro that takes a string and a keyword list of options.
But, in fact, send_message/2
maps to Nadia.send_message/3
a function that takes a chat ID as the first parameter.
The send_message/2
macro automatically understands the local scoped update
constant and properly injects the chat ID for you so you can focus on sending stuff.
Most of the methods at Nadia module have it's macro version for you. Take a look at
App.Commander to understand better.
Another feature that must be mentioned is that these macros can understand context.
Let's take a look at the get_chat_id/2
definitions:
defmacro get_chat_id do
quote do
case var!(update) do
%{inline_query: inline_query} when not is_nil(inline_query) ->
inline_query.from.id
%{callback_query: callback_query} when not is_nil(callback_query) ->
callback_query.message.chat.id
update ->
update.message.chat.id
end
end
end
If you ever used telegram bot API you may have experienced issues trying to find where is the chat ID for the current update. That's solves it under the hood in this boilerplate for you. Read more about it at App.Commands.
Matcher macros
# matches "/foo" commands
command "foo" do
end
# matches "/foo" commands from callback querys
callback_query_command "foo" do
end
# matches "/foo" commands from inline querys
inline_query_command "foo" do
end
# fallback for callback querys
callback_query do
end
# fallback for inline querys
inline_query do
end
# fallback for all updates
# must be at the end of the file
message do
end
Sender macros
answer_callback_query(options \\ [])
answer_inline_query(results, options \\ [])
send_audio(audio, options \\ [])
send_chat_action(action)
send_contact(phone_number, first_name, options \\ [])
send_document(document, options \\ [])
send_location(latitude, longitude, options \\ [])
send_message(text, options \\ [])
send_photo(photo, options \\ [])
send_sticker(sticker, options \\ [])
send_venue(latitude, longitude, title, address, options \\ [])
send_videos(video, options \\ [])
send_voice(voice, options \\ [])
Action macros
# except for inline querys
forward_message(chat_id)
get_chat
# except for inline querys
get_chat_admnistrators
get_chat_member(user_id)
get_chat_member_count
# except for inline querys
kick_chat_member(user_id)
# except for inline querys
leave_chat
# except for inline querys
unban_chat_member
get_chat_id
See also
- Rekyuu's version. I've based my bot mostly in his