Telegram Bot API Wrapper for Scala
100% idiomatic Scala wrapper for the Telegram Bot API. The full API is supported, inline queries, callbacks, editing, markups, sending files, chat actions... strongly-typed (no JSON stuff/strings), fully asynchronous (ont top of Akka), 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.
As SBT dependency from Jitpack
resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies += "com.github.mukel" %% "telegrambot4s" % "v1.2.1"
Make sure to specify scala version in your build file. You can also pull any branch or release from Jitpack, check it out.
Please DO NOT SHARE 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:
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()
Both methods are fully 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 are supported, but you must issue the certificates yourself.
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, idiomatic Scala, bug reports, even feature requests. Creating a bot, is also a contribution, I'll add a link to your bot here anytime.
Just add import info.mukel.telegrambot4s._, api._, methods._, models._, Implicits._
and you are good to go.
object LmgtfyBot extends TelegramBot with Polling with Commands {
def token = "TOKEN"
on("/lmgtfy") { implicit msg => args =>
reply(
"http://lmgtfy.com/?q=" + URLEncoder.encode(args mkString " ", "UTF-8"),
disableWebPagePreview = true
)
}
}
LmgtfyBot.run()
object TextToSpeechBot extends TelegramBot with Polling with Commands with ChatActions {
def token = "TOKEN"
val ttsApiBase = "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en-us&q="
on("/speak") { implicit msg => args =>
val text = args mkString " "
val url = ttsApiBase + URLEncoder.encode(text, "UTF-8")
for {
response <- Http().singleRequest(HttpRequest(uri = Uri(url)))
if response.status.isSuccess()
bytes <- Unmarshal(response).to[ByteString]
} /* do */ {
uploadingAudio // hint the user
val voiceMp3 = InputFile.FromByteString("voice.mp3", bytes)
api.request(SendVoice(msg.sender, voiceMp3))
}
}
}
object WebhookBot extends TelegramBot with Webhook {
def token = "TOKEN"
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()
Check out the sample bots for more functionality.