Packages
ex_gram
0.65.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.0
0.62.0
0.61.0
0.60.0
0.58.0
0.57.0
0.56.1
0.56.0
0.55.1
0.55.0
0.54.0
0.53.0
0.52.2
0.52.1
0.52.0
0.51.1
0.51.0
0.50.2
0.50.1
0.50.0
0.41.0
0.40.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.0
0.5.0-rc6
0.5.0-rc5
0.5.0-rc4
0.5.0-rc3
0.5.0-rc2
Telegram Bot API low level and framework
Current section
Files
Jump to
Current section
Files
guides/getting-started.md
# Getting Started
This guide walks you through creating your first ExGram bot from scratch.
## Prerequisites
Make sure you have:
- Elixir installed (1.15 or later)
- A Telegram Bot Token from [@BotFather](https://t.me/botfather)
- Completed the [Installation](installation.md) guide
## Create a New Project
Create a new Elixir project with supervision tree:
```bash
mix new my_bot --sup
cd my_bot
```
## Install ExGram
Add ExGram to `mix.exs` as shown in the [Installation](installation.md) guide, then:
```bash
mix deps.get
```
## Add formatter (Optional)
It is recommended to add `ExGram` to your `.formatter.exs` deps, that way the DSL will look clean.
```elixir
# .formatter.exs
[
# ....
import_deps: [:ex_gram] # Add :ex_gram here
]
```
## Generate Your Bot
ExGram provides a Mix task to generate a bot module:
```bash
mix bot.new
```
This creates `lib/my_bot/bot.ex` with a basic bot structure:
```elixir
defmodule MyBot.Bot do
@bot :my_bot
use ExGram.Bot,
name: @bot,
setup_commands: true
command("start")
command("help", description: "Print the bot's help")
middleware(ExGram.Middleware.IgnoreUsername)
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
```
### Understanding the Generated Bot
- `@bot :my_bot` - Internal name for your bot
- `use ExGram.Bot` - Imports the bot framework
- `setup_commands: true` - Automatically registers commands with Telegram
- `command/1-2` - Declares commands that your bot handles
- `middleware/1` - Adds middleware to the processing pipeline (see `ExGram.Middleware`)
- `handle/2` - Handles incoming updates (callback from `c:ExGram.Handler.handle/2`)
## Configure Your Application
Get your bot token from [@BotFather](https://t.me/botfather) and configure it in `config/config.exs`:
```elixir
import Config
config :ex_gram,
token: "YOUR_BOT_TOKEN_HERE"
```
**Security Note:** For production, use environment variables instead:
```elixir
config :ex_gram,
token: System.get_env("BOT_TOKEN")
```
## Add Bot to Supervision Tree
Open `lib/my_bot/application.ex` and add ExGram and your bot to the children list:
```elixir
defmodule MyBot.Application do
use Application
@impl true
def start(_type, _args) do
children = [
ExGram,
{MyBot.Bot, [method: :polling, token: Application.fetch_env!(:ex_gram, :token)]}
]
opts = [strategy: :one_for_one, name: MyBot.Supervisor]
Supervisor.start_link(children, opts)
end
end
```
**Key points:**
- `ExGram` must be started before your bot
- `method: :polling` - Use polling to receive updates, learn more about how to get updates [in this guide](polling-and-webhooks.md)
- `token:` - Pass the token explicitly
## Run Your Bot
Start your bot:
```bash
mix run --no-halt
```
Open Telegram and send `/start` to your bot. It should reply with "Hi!"
## Token Configuration Options
### 1. Global Config + Explicit on bot
If you just have one bot, this combination will allow you to use the DSL and the normal methods without any problem.
```elixir
# config/config.exs
config :ex_gram, token: "TOKEN"
# lib/my_bot/application.ex
token = System.get_env("BOT_TOKEN") || Application.fetch_env!(:ex_gram, :token)
{MyBot.Bot, [method: :polling, token: token]}
```
### 2. Only global config
If you are just going to use the normal methods, no `ExGram.Bot` bots, then you can just configure the global token.
```elixir
# config/config.exs
config :ex_gram, token: "TOKEN"
```
### 3. Runtime Configuration
**DO NOT** hardcode any token in any configuration file or source code. It's the most common reason people publish secret tokens by mistake.
The easiest way to avoid this is to have a `config/runtime.exs`
```elixir
# config/runtime.exs
import Config
if config_env() == :prod do
config :ex_gram, token: System.fetch_env!("BOT_TOKEN")
end
# config/config.exs
config :ex_gram, token: "fake token"
```
## Next Steps
Now that you have a working bot:
- [Handling Updates](handling-updates.md) - Learn about different update types
- [Sending Messages](sending-messages.md) - Explore the DSL for building responses
- [Polling and Webhooks](polling-and-webhooks.md) - Configure how your bot receives updates
## Troubleshooting
### Bot doesn't respond
1. Verify your token is correct
2. Check logs for errors: `Logger.configure(level: :debug)`
### "Registry.ExGram not started"
Make sure `ExGram` is in your supervision tree before your bot.