telega

Package Version Hex Docs

A Gleam library for the Telegram Bot API.

It provides:

Installation

gleam add telega

Simple usage

To start using the library, you must install wisp and use the telega adapter middleware.

import gleam/erlang/process
import gleam/result
import gleam/option.{None}
import mist
import wisp
import telega
import telega/update.{CommandUpdate, TextUpdate}
import telega/api as telega_api
import telega/adapters/wisp as telega_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, ..) -> telega_api.reply(ctx, text)
    CommandUpdate(command: command, ..) -> telega_api.reply(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
    |> result.nil_error

  process.sleep_forever()
}

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