Telegram Bot API Wrapper for Scala
100% idiomatic Scala wrapper for the Telegram Bot API. The entire API is supported, inline mode, callbacks, editing, sending files, chat actions... strongly-typed (no JSON stuff/strings), asynchronous, and transparently camelCased.
I encourage users to report any bug or broken functionality, I'll do my best to give proper support in a reasonable time frame.
resolvers += Resolver.sonatypeRepo("snapshots")
libraryDependencies += "info.mukel" %% "telegrambot4s" % "1.1.4-SNAPSHOT"
Or pick latest snapshot from Jitpack
resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies += "com.github.mukel" %% "telegrambot4s" % "master-SNAPSHOT"
Make sure to specify scala version in your build file. You can also pull any branch or tagged version from Jitpack, check it out.
Please DO NOT SHARE BOT TOKENS in any form.
In order to avoid unintentional TOKEN sharing, a simple but efficient method is to store a separate file UNTRACKED, OUTSIDE THE REPO!!! e.g. "bot.token" and spawn your bot as follows:
Then you can safely share your code and submit pull requests.
object SafeBot extends TelegramBot with Polling with Commands {
def token = Source.fromFile("./bot.token").getLines().next
on("/hello") { implicit msg => _ =>
reply("My token is SAFE!")
}
}
SafeBot.run()
- Self-signed certificates (usable, but must handle the certificates yourself)
Both methods are supported.
Polling is by far the easiest method, and can be used locally without any additional requirements. Polling has been radically improved, it doesn't flood the server and it's very fast.
Using webhooks requires a server (it won't work on your laptop). Self signed certificates wont work are supported now since August 29th, see the API not so recent changes.
Beside the usual ways, I've managed to run FlunkeyBot successfully on a Raspberry Pi 2, and most notably on an old Android (4.1.2) phone with a broken screen.
Contributions are highly appreciated, documentation improvements/corrections, better ways to do things. Writing a bot using this library is also a way to contribute, I'll add a link to your bot here anytime. Thanks to Andrey Romanov and ex0ns for their contributions.
val helloBot = new TelegramBot with Polling with Commands {
def token = Source.fromFile("./bot.token").getLines().next
}
helloBot.on("/hello") { implicit message => _ =>
reply("Hello from Telegram!")
}
helloBot.run()
Or
object LmgtfyBot extends TelegramBot with Polling with Commands {
def token = "TOKEN"
on("/lmgtfy") { implicit message => args =>
reply(
"http://lmgtfy.com/?q=" + URLEncoder.encode(args mkString " ", "UTF-8"),
disableWebPagePreview = true
)
}
}
LmgtfyBot.run()
Using webhooks
object WebhookBot extends TestBot with Webhook {
def port = 8443
def webhookUrl = "https://ed88ff73.ngrok.io"
def toL337(s: String) = s.map("aegiost".zip("4361057").toMap.withDefault(identity))
override def handleMessage(message: Message): Unit = {
for (text <- message.text)
api.request(SendMessage(message.sender, toL337(text)))
}
}
WebhookBot.run()
There are some additional examples showing more functionality.