A Gleam library for the Telegram Bot API on BEAM.
- an interface to the Telegram Bot HTTP-based APIs
telega/api - an client for the Telegram Bot API
telega/client - adapter to use with wisp
- polling implementation
- session bot implementation
- conversation implementation
- convenient utilities for common tasks
If you are new to Telegram bots, read the official Introduction for Developers written by the Telegram team.
First, visit @BotFather to create a new bot. Copy the token and save it for later.
Initiate a gleam project and add telega as a dependencies:
$ gleam new first_tg_bot
$ cd first_tg_bot
$ gleam add telegaReplace the first_tg_bot.gleam file content with the following code:
import telega
import telega/polling
import telega/reply
import telega/router
import telega/update
fn handle_text(ctx, text) {
use ctx <- telega.log_context(ctx, "echo_text")
let assert Ok(_) = reply.with_text(ctx, text)
Ok(ctx)
}
fn handle_command(ctx, command: update.Command) {
use ctx <- telega.log_context(ctx, "echo_command")
let assert Ok(_) = reply.with_text(ctx, "Command: " <> command.text)
Ok(ctx)
}
pub fn main() {
let router =
router.new("echo_bot")
|> router.on_any_text(handle_text)
|> router.on_commands(["start", "help"], handle_command)
let assert Ok(bot) =
telega.new_for_polling(token: "BOT_TOKEN")
|> telega.with_router(router)
|> telega.init_for_polling_nil_session()
let assert Ok(poller) = polling.start_polling_default(bot)
polling.wait_finish(poller)
}Replace "BOT_TOKEN" with the token you received from the BotFather. Then run the bot:
$ gleam runAnd it will echo all received text messages.
Congratulations! You just wrote a Telegram bot :)
Other examples can be found in the examples directory.
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell