Code Monkey home page Code Monkey logo

elixir-telegram-bot-boilerplate's Introduction

Elixir Telegram Bot Boilerplate

A boilerplate for making bots for telegram using Elixir because of yes

Getting Started

  1. Setup you bot name and telegram bot token at config/config.ex

You may set up environment-wide configurations at dev.ex, prod.ex and test at the config/ folder if you have different bots for different environments

```elixir
config :app,
  bot_name: "bot_user_name"

config :nadia,
  token: "abcdefg_12345678910_the_game"
```
  1. Setup commands at lib/app/commands.ex

  2. Run at your shell

```sh
λ 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

License

MIT

elixir-telegram-bot-boilerplate's People

Contributors

lubien avatar

Watchers

 avatar  avatar

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.