GithubHelp home page GithubHelp logo

drruisseau / telegramium Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apimorphism/telegramium

0.0 1.0 0.0 876 KB

Telegramium or F[ Tg ] - pure functional Telegram Bot API implementation for Scala.

License: MIT License

Scala 100.00%

telegramium's Introduction

F[Tg] - Telegramium

Telegram Scala Steward badge Maven Central

F[Tg] is a pure functional Telegram Bot API for Scala.

This project is a try to provide a comprehensive, well documented and Scala-idiomatic client/server implementations to work with Telegram Bot API. Please refer to telegramium-examples module for usage examples. There is a support both for polling and webhooks. API core is generated from the official documentation, so it is believed to cover all the available methods, entities and to be up to date.

Currently the following backends are supported:

  • http4s
  • cats-effect
  • circe
  • uPickle (MsgPack binary serialization, to be used for storing API messages in DB and sending over network)

You may want to start with Methods.scala and EchoBot.scala.

How to use

Create the dependency by adding the following lines to your build.sbt:

libraryDependencies += "io.github.apimorphism" %% "telegramium-core" % "<version>"
libraryDependencies += "io.github.apimorphism" %% "telegramium-high" % "<version>"

Imports:

import telegramium.bots.high._
import telegramium.bots.high.implicits._

Use the Methods factory to create requests. You will need an instance of the BotApi class to execute them:

BlazeClientBuilder[F](ExecutionContext.global).resource.use { httpClient =>
  implicit val api: Api[F] = BotApi(http, baseUrl = s"https://api.telegram.org/bot$token")
  val bot = new MyLongPollBot()
  bot.start()
}

Long polling

class MyLongPollBot[F[_]: Sync: Timer: Parallel]()(implicit api: Api[F]) extends LongPollBot[F](api) {
  override def onMessage(msg: Message): F[Unit] =
    Methods.sendMessage(chatId = ChatIntId(msg.chat.id), text = "Hello, world!").exec.void
}

LongPollBot and WebhookBot extend the Methods trait so you can call sendMessage directly.

Webhooks

class MyWebhookBot[F[_]: ConcurrentEffect: ContextShift: Timer](port: Int, url: String, path: String)(
  implicit api: Api[F]
) extends WebhookBot[F](api, port, url, path) {
  override def onMessage(msg: Message): F[Unit] =
    sendMessage(chatId = ChatIntId(msg.chat.id), text = "Hello, world!").exec.void
}

You can also perform a request to the Bot API while sending an answer to the webhook:

override def onMessageReply(msg: Message): F[Option[Method[_]]] =
  Sync[F].pure(Some(sendMessage(chatId = ChatIntId(msg.chat.id), text = "Hello, world!")))

Keyboards

To use smart and safe constructors for keyboard markups, import telegramium.bots.high.keyboards._:

val button: KeyboardButton = KeyboardButtons.text("Hello, world!")
val keyboard: ReplyKeyboardMarkup = ReplyKeyboardMarkups.singleButton(button)

val inlineButton: InlineKeyboardButton =
  InlineKeyboardButtons.callbackData(text = "Press me", callbackData = "button_pressed")

val inlineKeyboard: InlineKeyboardMarkup =
  InlineKeyboardMarkups.singleButton(inlineButton)

Versioning

X.Y.Z where

  • X major changes to telegramium high or internals of the core.
  • Y denotes Telegram Bot API version which is supported by this X.Y.Z.
  • Z bug fix changes.

Please, note, that this versioning scheme is started from version 1.48.0.

Contribution

I'd love to have more testing and more example bots. Ideas and PRs on telegramium-high - high level interface for the bot API are also highly encouraged.

If you want to change something in telegramium-core or found a bug in it, please create an issue. Do not create pull requests with changes on telegramium-core as we use semi-automatic way to work with it. Except that any PR-s are welcome.

Alternatives

You may also want to have a look at these projects:

Known usages

Note for Yan

Yan, напиши бота.

telegramium's People

Contributors

johnspade avatar scala-steward avatar sherzodv avatar kabowyad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.