Telega

Package Version Hex Docs

A Gleam library for the Telegram Bot API.

It provides

Quick start

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 and wisp as a dependencies:

$ gleam new first_tg_bot
$ cd first_tg_bot
$ gleam add telega wisp mist gleam_erlang

Replace the first_tg_bot.gleam file content with the following code:

import gleam/erlang/process
import gleam/option.{None}
import gleam/result
import mist
import telega
import telega/adapters/wisp as telega_wisp
import telega/reply
import telega/update.{CommandUpdate, TextUpdate}
import wisp

fn handle_request(bot, req) {
  use <- telega_wisp.handle_bot(req, bot)
  wisp.not_found()
}

fn echo_handler(ctx) {
  use <- telega.log_context(ctx, "echo")

  case ctx.update {
    TextUpdate(text: text, ..) -> reply.with_text(ctx, text)
    CommandUpdate(command: command, ..) -> reply.with_text(ctx, command.text)
    _ -> Error("No text message")
  }
  |> result.map(fn(_) { Nil })
}

pub fn main() {
  wisp.configure_logger()

  let assert Ok(bot) =
    telega.new(
      token: "your bot token from @BotFather",
      url: "your bot url",
      webhook_path: "secret path",
      secret_token: None,
    )
    |> telega.handle_all(echo_handler)
    |> telega.init_nil_session

  let assert Ok(_) =
    wisp.mist_handler(handle_request(bot, _), wisp.random_string(64))
    |> mist.new
    |> mist.port(8000)
    |> mist.start_http

  process.sleep_forever()
}

Replace "your bot token from @BotFather" with the token you received from the BotFather. Set the url and webhook_path to your server’s URL and the desired path for the webhook. If you don’t have a server yet, you can use ngrok or localtunne to create a tunnel to your local machine.

Then run the bot:

$ gleam run

And it will echo all received text messages.

Congratulations! You just wrote a Telegram bot :)

Examples

Other examples can be found in the examples directory.

Development

gleam run   # Run the project
gleam test  # Run the tests
gleam shell # Run an Erlang shell
Search Document