All Projects → augustjune → Canoe

augustjune / Canoe

Licence: mit
Functional Telegram Bot API for Scala

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Canoe

Messaging Apis
Messaging APIs for multi-platform
Stars: ✭ 1,754 (+1180.29%)
Mutual labels:  bot, telegram
Madelineproto
Async PHP client/server API for the telegram MTProto protocol
Stars: ✭ 1,776 (+1196.35%)
Mutual labels:  bot, telegram
Zanzara
Asynchronous PHP Telegram Bot Framework built on top of ReactPHP
Stars: ✭ 107 (-21.9%)
Mutual labels:  bot, telegram
Cats Stm
An STM implementation for Cats Effect
Stars: ✭ 106 (-22.63%)
Mutual labels:  cats, functional-programming
Test State
Scala Test-State.
Stars: ✭ 119 (-13.14%)
Mutual labels:  cats, functional-programming
Botogram
Just focus on your bots.
Stars: ✭ 106 (-22.63%)
Mutual labels:  bot, telegram
Novagram
An Object-Oriented PHP library for Telegram Bots
Stars: ✭ 112 (-18.25%)
Mutual labels:  bot, telegram
Lua Telegram Bot
Lua Library for the Telegram Bot API
Stars: ✭ 100 (-27.01%)
Mutual labels:  bot, telegram
Moodle Downloader 2
A Moodle downloader that downloads course content fast from Moodle (eg. lecture pdfs)
Stars: ✭ 118 (-13.87%)
Mutual labels:  bot, telegram
Node Telegram Api
A simple API to create and control Telegram bots
Stars: ✭ 117 (-14.6%)
Mutual labels:  bot, telegram
Hackernewsbot
📰 Telegram bot that posts new hot stories from Hacker News to telegram channel
Stars: ✭ 103 (-24.82%)
Mutual labels:  bot, telegram
Telegram.bot
.NET Client for Telegram Bot API
Stars: ✭ 1,964 (+1333.58%)
Mutual labels:  bot, telegram
Telegram Bot Github
Allows to you receive GitHub notifications right in the Telegram
Stars: ✭ 103 (-24.82%)
Mutual labels:  bot, telegram
Telegram Instapy Scheduling
A Telegram bot for scheduling InstaPy
Stars: ✭ 136 (-0.73%)
Mutual labels:  bot, telegram
Ex gram
Telegram Bot API low level API and framework
Stars: ✭ 103 (-24.82%)
Mutual labels:  bot, telegram
Awesome Telegram Bot
Manual picked bot of Telegram.
Stars: ✭ 107 (-21.9%)
Mutual labels:  bot, telegram
Monocle
Optics library for Scala
Stars: ✭ 1,357 (+890.51%)
Mutual labels:  cats, functional-programming
Roguebot
My simple rogue-like game for Telegram
Stars: ✭ 100 (-27.01%)
Mutual labels:  bot, telegram
Telegram Clonebot
Simple Bot to clone Google Drive Files (or Folders) to your Team Drive[or Normal Drive]. P.S This is not a Mirror Bot. Enjoy ✌🏻
Stars: ✭ 114 (-16.79%)
Mutual labels:  bot, telegram
Telegram Forward Bot
Simple Telegram Bot for forwarding messages easily between various related channels and groups.
Stars: ✭ 132 (-3.65%)
Mutual labels:  bot, telegram

canoe

Build Status Gitter

Maven Central Telegram

Overview

canoe is a purely functional, compositional library for building interactive Telegram bots. It provides functional streaming interface over Telegram Bot API with built-in abstractions for describing your chatbot behavior.

Getting started

sbt dependency:

libraryDependencies += "org.augustjune" %% "canoe" % "<version>"

You can find the latest version in releases tab or by clicking on the maven-central badge. The library is available for Scala 2.12, 2.13, and Scala.js.

Imports:

import canoe.api._
import canoe.syntax._

The problem

Building interactive chatbots requires maintaining the state of each conversation, with possible interaction across them and/or using shared resources. The complexity of this task grows rapidly with the advancement of the bot. canoe solves this problem by decomposing behavior of the bot into a set of scenarios which the chatbot will follow.

Basic example

Here's a quick example of how the definition of simple bot behavior looks like in canoe. More samples can be found here.

import canoe.api._
import canoe.syntax._
import cats.effect.ConcurrentEffect
import fs2.Stream

def app[F[_]: ConcurrentEffect]: F[Unit] =
  Stream
    .resource(TelegramClient.global[F](token))
    .flatMap { implicit client => Bot.polling[F].follow(greetings) }
    .compile.drain

def greetings[F[_]: TelegramClient]: Scenario[F, Unit] =
    for {
      chat <- Scenario.expect(command("hi").chat)
      _    <- Scenario.eval(chat.send("Hello. What's your name?"))
      name <- Scenario.expect(text)
      _    <- Scenario.eval(chat.send(s"Nice to meet you, $name"))
    } yield ()

Scenarios are executed concurrently in a non-blocking fashion, allowing to handle multiple users at the same time. In fact, even the same scenario can be triggered multiple times before the previous execution is completed. This can be extremely useful when you allow users to schedule long-running jobs and don't want to make them wait before they can schedule the new ones. As example may serve a simple alarm clock implementation.

Telegram Bot API methods

Low level abstractions are available through standalone Telegram Bot API methods from canoe.methods package. Having instance of TelegramClient in implicit scope, you can use call method on constructed action in order to execute it in effect F.

def sendText[F[_]: TelegramClient](chatId: Long, text: String): F[TextMessage] =
  SendMessage(chatId, text).call

As an alternative, all the methods from Telegram Bot API are available from corresponding models, e.g.chat.kickUser(user.id), message.editText("edited").

Webhook support

canoe also provides support for obtaining messages from Telegram by setting a webhook. Full example may be found here.

Handling errors

There's a lot of things that may go wrong during your scenarios executions, from user input to the network issues. For this reason, Scenario forms a MonadError for any F. It means that you can use built-in handleErrorWith and attempt methods, in order to react to the raised error or ensure that bot workflow won't break. Full example may be found here.

Contribution

If you're interested in the project PRs are very welcomed. In case it's a feature you'd like to introduce, it is recommended to discuss it first by raising an issue or simply using gitter.

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].