• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Elixir
  • License
    MIT License
  • Created about 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A boilerplate for making telegram bots with Elixir and Nadia

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

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

λ 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

More Repositories

1

bookmarker

Export Google Chrome bookmarks into markdown files
Elixir
58
star
2

sorteios

Prize draw system built using Phoenix (LiveView + Presence + PubSub)
Elixir
42
star
3

awesome-elixir-brazil

Lista de recursos Elixir para a comunidade brasileira
42
star
4

popura

[MAL API is down 😉] NodeJS wrapper for MyAnimeList API
JavaScript
28
star
5

koa-react-redux-universal-boilerplate

[Unmaintained] A fullstack Koa + React + Redux Universal Boilerplate to speed up your development process.
JavaScript
22
star
6

liveview_playground

Your first LiveView!
Elixir
20
star
7

fly-together

Proof of concept onto how to deploy apps to fly.io using only the GraphQL API
Elixir
17
star
8

single-file-phoenix-static

Elixir
16
star
9

dotfiles

If you stare into the dotfiles, the dotfiles stares back at you
Vim Script
15
star
10

awesome-flyio-examples

Awesome list of tips and guides that just work ™️ hosting on Fly.io
15
star
11

mongo-template

Simple mongodb cluster to run on Fly.io. For help please use https://community.fly.io/
Go
14
star
12

awesome-tos

A curated list of useful Tree of Savior links.
14
star
13

js-challenges

Repo to store telegram's @js_challenges
13
star
14

fly-uptime-kuma

12
star
15

cerebro-plugins

Cerebro quick plugin list
JavaScript
9
star
16

layoffs

Elixir
8
star
17

phoenix-graceful-deploy

Elixir
8
star
18

data-structures-book

Data Structures and Their Algorithms (Jayme Luiz Szwarcfiter and Lilian Markenzon)
C
8
star
19

cool-compiler

Docker-based COOL-lang compiler environment
C++
7
star
20

changelog-vue

Changelog component built with @iagocavalcante
Vue
7
star
21

elixir-days-2024-talk

Running untrusted code with Fly.io
6
star
22

cerebro-vaporwave

Cool text bro but it need more VAPORWAVE
JavaScript
5
star
23

itermate

[WIP] Lazy iteration library based on Ramda and generators
JavaScript
5
star
24

cerebro-mal

MyAnimeList plugin for Cerebro
JavaScript
5
star
25

ufpa-paradigms-2017

Code from my Programming Paradigms class at UFPA with Eloi Favero
C
4
star
26

live-session-playground

Simple Live Session Playground for LiveView
Elixir
4
star
27

phx-tools

tools for phoenix devs
Elixir
4
star
28

ufpa-ai-2017

Code from my AI class at UFPA with Eloi Favero
Prolog
4
star
29

backend-fight-2024-q1

Elixir
3
star
30

champions

Elixir
3
star
31

cerebro-gitio

Shorten GitHub URLs with git.io
JavaScript
3
star
32

ufpa-algorithms-analysis-2017

Code from my Algorithms Analysis class at UFPA with @filipesaraiva
JavaScript
3
star
33

cerebro-hotel

Cerebro plugin that lets you start, stop and open Hotel apps
JavaScript
2
star
34

roman

Elixir
2
star
35

elixir-lang-bot

Bot that posts @elixirstatus, @rElixir, @elixir_forum and @pctguama news on Telegram
Elixir
2
star
36

bookmarks

My Google Chrome bookmarks
2
star
37

dod

2
star
38

polyglot

Know how much of what languages you've been writting on GitHub
Elixir
2
star
39

cerebro-reload

[Moved to Core] Reload Cerebro using Cerebro
JavaScript
2
star
40

phoenix-aula-components

Elixir
2
star
41

fgo-calculator

FGO Damage Calculator PWA built with VueJS
Vue
2
star
42

godot-dodge-the-creeps

Straight out of the getting started from Godot
JavaScript
2
star
43

ufpa-discrete-math-2016

Code from my Discrete Math class at UFPA with Mauro Larrat
Haskell
2
star
44

alchemists

Elixir
2
star
45

ex_limiter

Elixir
2
star
46

ufpa-advanced-databases-2018-assignment-01

Implement some queries over self made db table with 1 billion entries of 8 bytes each for @gustavopinto class
Elixir
2
star
47

elm-examples

Elm
1
star
48

liveview-litefs-cdn

Elixir
1
star
49

issue-tests

1
star
50

spider-backend-example

Example code used to teach basic REST API concepts at @projeto-spider
JavaScript
1
star
51

tosxp

Tree of Savior XP calculator
JavaScript
1
star
52

popura-cli

CLI for NodeJS's popura package
JavaScript
1
star
53

sotorrent-plots

Please don't do this
JavaScript
1
star
54

ufpa-graphs-2017

Code from my Graphs class at UFPA with Pablo Cabral and profesor Marcelle Motta
JavaScript
1
star
55

ufpa-programming-ii-model

UFPA's @ClaudomiroSales Programming II Class Final Assignment
C++
1
star
56

livestream-todomvc-liveview

Elixir
1
star
57

flat-mal

A design for myanimelist.net lists
HTML
1
star
58

easy-parser

SimpleHTMLDOM abstraction
PHP
1
star
59

vue-svg-diagrams

[WIP] Tinkering SVG Diagrams using Vue
Vue
1
star
60

phoenix-forum

Elixir
1
star
61

fly-organization-logs-watcher

Elixir
1
star
62

backend-fight-2024-q1-multi-tenant-sqlite

Elixir
1
star
63

example-vue-classroom-guris

HTML
1
star
64

fbmosaic

Cut your images in squares then separate in 9 parts to use in you FB profile as a mosaic
HTML
1
star
65

liveview_recipes

1
star
66

liveview-recipes

Elixir
1
star
67

vue-brunch-boilerplate

Boilerplate to use Vue with Brunch
Vue
1
star