wmealing / telega

Gleam Telegram Bot Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

telega

Package Version Hex Docs

A Gleam library for the Telegram Bot API.

It provides:

  • an inteface to the Telegram Bot HTTP-based APIs telega/api
  • adapter to use with wisp

Installation

gleam add telega

Simple usage

To start using the library you need to install wisp and use the telega adapter middleware.

import gleam/erlang/process
import gleam/result
import gleam/option.{None, Some}
import mist
import wisp.{type Request, type Response}
import telega.{type Bot, type Context, HandleAll}
import telega/adapters/wisp as telega_wisp
import telega/api as telega_api

fn handle_request(bot: Bot, req: Request) -> Response {
  use <- telega_wisp.handle_bot(req, bot)
  wisp.not_found()
}

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

  case ctx.message.raw.text {
    Some(text) ->
      telega_api.reply(ctx, text)
      |> result.map(fn(_) { Nil })
    None -> Error("No text in message")
  }
}

pub fn main() {
  wisp.configure_logger()
  let bot =
    telega.new(
      token: "your bot token from @BotFather",
      url: "your bot url",
      webhook_path: "secret path",
      secret_token: None,
    )
    |> telega.add_handler(HandleAll(echo_handler))

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

  let assert Ok(_) = telega_api.set_webhook(bot)

  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

About

Gleam Telegram Bot Library

License:Apache License 2.0


Languages

Language:Gleam 100.0%