All Projects → apimorphism → telegramium

apimorphism / telegramium

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

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to telegramium

cats-effect-testing
Integration between cats-effect and test frameworks
Stars: ✭ 155 (+269.05%)
Mutual labels:  cats, cats-effect
tutorials
🎥 Source code of the examples shown in the video tutorials
Stars: ✭ 18 (-57.14%)
Mutual labels:  cats, cats-effect
influencer-stats
Playground for measuring performance of functional programming tools in Scala. Gathers statistics about videos.
Stars: ✭ 24 (-42.86%)
Mutual labels:  cats, cats-effect
cats-helper
Helpers for cats & cats-effect
Stars: ✭ 19 (-54.76%)
Mutual labels:  cats, cats-effect
scala-functional-programming-tutorial
Functional Programming in Scala Tutorial
Stars: ✭ 23 (-45.24%)
Mutual labels:  cats, cats-effect
swam
WebAssembly engine in Scala
Stars: ✭ 38 (-9.52%)
Mutual labels:  cats, cats-effect
classy-optics
🔎 Source code shown at my talks at Scale by the Bay 2018 and Scalar 2019
Stars: ✭ 25 (-40.48%)
Mutual labels:  cats, cats-effect
typelevel-stack.g8
📚 Unofficial Giter8 template for the Typelevel Stack (Http4s / Doobie / Circe / Cats Effect / Fs2) based on Cats v1.x.x
Stars: ✭ 63 (+50%)
Mutual labels:  cats, cats-effect
tagless-final-jam
Workshop On Tagless Final Interpreters
Stars: ✭ 37 (-11.9%)
Mutual labels:  cats, cats-effect
pfps-examples
🏮 Standalone examples shown in the book "Practical FP in Scala: A hands-on approach"
Stars: ✭ 167 (+297.62%)
Mutual labels:  cats, cats-effect
vault4s
Vault Client Library For Scala
Stars: ✭ 15 (-64.29%)
Mutual labels:  cats, cats-effect
fs2-ssh
A wrapper around Apache SSHD targeting cats-effect and fs2
Stars: ✭ 36 (-14.29%)
Mutual labels:  cats, cats-effect
fs2-ftp
Simple client for Ftp/Ftps/Sftp
Stars: ✭ 24 (-42.86%)
Mutual labels:  cats, cats-effect
Monix
Asynchronous, Reactive Programming for Scala and Scala.js.
Stars: ✭ 1,819 (+4230.95%)
Mutual labels:  cats, cats-effect
console4cats
💻 Effect-type agnostic Console I/O for Cats Effect (archived, use Cats Effect 3 instead)
Stars: ✭ 55 (+30.95%)
Mutual labels:  cats, cats-effect
tinyweb
Simple and lightweight HTTP async server for micropython
Stars: ✭ 182 (+333.33%)
Mutual labels:  cats, cats-effect
skafka
Scala wrapper for kafka consumer and producer
Stars: ✭ 35 (-16.67%)
Mutual labels:  cats, cats-effect
tradeio
A disciplined way to purely functional domain models in Scala
Stars: ✭ 19 (-54.76%)
Mutual labels:  cats, cats-effect
ecommerce
A project for exploring Akka with Scala
Stars: ✭ 24 (-42.86%)
Mutual labels:  cats
fs2-es
Event sourcing utilities for FS2
Stars: ✭ 75 (+78.57%)
Mutual labels:  cats-effect

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

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].resource.use { httpClient =>
  implicit val api: Api[F] = BotApi(httpClient, baseUrl = s"https://api.telegram.org/bot$token")
  val bot = new MyLongPollBot()
  bot.start()
}

Long polling

class MyLongPollBot[F[_]: Async: 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[_]: Async](url: String, path: String)(
  implicit api: Api[F]
) extends WebhookBot[F](api, 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!")))

In addition, the library provides a way to compose multiple Webhook bots into a single Http4s Server that will handle the webhooks registration as well as the incoming requests of all the composed bots. You ultimately decide at which host:port the server will be binded to:

val api1: Api[IO] = BotApi(http, baseUrl = s"https://api.telegram.org/bot$bot_token1")
val api2: Api[IO] = BotApi(http, baseUrl = s"https://api.telegram.org/bot$bot_token1")
val bot1: MyWebhookbot = new MyWebhookbot[IO](api1, "ServerVisibleFromOutside", s"/$bot_token1")
val bot2: MyWebhookbot = new MyWebhookbot[IO](api2, "ServerVisibleFromOutside", s"/$bot_token2")

WebhookBot.compose[IO](
    List(bot1, bot2),
    8080,
    "127.0.0.1" //optional, localhost as default
  ).useForever.runSyncUnsafe()

For details, have a look at the Github Issue and the related Pull Request.

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 that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].