- Notifications
You must be signed in to change notification settings - Fork2
Functional Telegram Bot API wrapper for Scala on top of akka, circe, cats, and shapeless
License
nikdon/telepooz
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
telepooz is a scala wrapper forTelegram Bot API.
telepooz built forscala-2.12. To use it add following to build file:
resolvers+="jitpack" at"https://jitpack.io"libraryDependencies+="com.github.nikdon"%"telepooz"%"0.5.6"
And configure telepooz via thereference.conf
oraplication.conf
or by, for ex., env variables:
telegram { host="api.telegram.org" token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" token=${?telegram_token} polling { interval=1000// in milliseconds interval=${?telegram_update_interval} limit=100 limit=${?telegram_update_limit} parallelism=2 parallelism=${?telegram_polling_parallelism} } reactor { parallelism=2 parallelism=${?telegram_reactor_parallelism} }}
The only one reason to write was curiosity. Telepooz written on top ofAkka Streams with intention tomaximize using of functional abstractions provided bycats. For example, APIrequests are composable:
importcom.github.nikdon.telepooz.api._importcom.github.nikdon.telepooz.model.methodsimportcom.github.nikdon.telepooz.engine.ApiRequestExecutorvalapiRequestExecutor=newApiRequestExecutor() {}valreq=for { a← methods.GetMe b← methods.SendMessage("abc", a.result.fold("empty")(_.first_name))}yield bvalres= req.foldMap(apiRequestExecutor)whenReady(res){ m⇒ m shouldBe a [Response[_]] m.ok shouldEqualtrue m.result shouldBe defined m.result.value shouldBe a [model.Message]}
telepooz is far from completion, here is a list of some desired features to implemented in future:
- File uploading via multipart/form-data
In general, bot consists of three parts:ApiRequestExecutor
,Polling
orWebHook
andReactor
.ApiRequestExecutor
creates requests to the telegram bot API endpoint.Polling
asks telegram server about newupdates viaApiRequestExecutor
and send them to theReactor
.WebHook
receives new updates via incoming requestsfrom telegram. FinallyReactor
reacts on an input stream of incoming updates from thePolling
orWebHook
.ToplevelTelepooz
trait provides a methodinstance
that is aReaderT[Future, (ApiRequestExecutor, Polling, Reactor), Done]
. To start a bot provide a valid inputforinstance.run(...)
with all three components described above.
/** Just an example of how the bot might look like*/importcom.github.nikdon.telepooz.engine._objectNaiveBotextendsTelepoozwithApp {implicitvalare=newApiRequestExecutor {}valpoller=newPollingvalreactor=newReactor {valreactions=CommandBasedReactions() .on("/start")(implicit message⇒ args⇒ reply("You are started!")) .on("/test")(implicit message⇒ args⇒ reply("Hi there!")) } instance.run((are, poller, reactor))}
importakka.stream.ActorMaterializerimportcom.github.nikdon.telepooz.engine._objectWebhookBotextendsTelepoozwithApp {implicitvalasm=ActorMaterializer()implicitvalare=newMockApiRequestExecutor(1) {}valpoller=newWebhook(endpoint="test", interface="127.0.0.1")valreactor=newReactor {valreactions=CommandBasedReactions() .on("/start")(implicit message⇒ args⇒ { println(s"You are started!$args") reply("You are started!") }) .on("/test")(implicit message⇒ args⇒ { println(s"You are tested!$args") reply("You are tested!") }) } webhook.run((poller, reactor))}
telepooz support theTypelevelcode of conduct,contributions are always welcome. Good ways to contribute include:
- Raising bugs and feature requests,
- Fixing bugs and developing new features (I will attempt to merge in pull requests ASAP),
- Improving the performance oftelepooz,
- Provide examples of bots.
telepooz is licensed under theApache License, Version 2.0 (the "License");you may not use this software except in compliance with the License.
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
About
Functional Telegram Bot API wrapper for Scala on top of akka, circe, cats, and shapeless