Git Product home page Git Product logo

telegrambot4s's Introduction

TelegramBot4s

Travis CI Build Status Telegram API License Release

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.

As SBT dependency

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.

About TOKEN safety

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()
  

Not supported yet

  • Self-signed certificates (usable, but must handle the certificates yourself)

Webhooks vs Polling

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.

Bonus (or how to turn a spare phone into a Telegram Bot)

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.

Contributors

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.

Usage

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.

telegrambot4s's People

Contributors

mukel avatar drewnoff avatar alari avatar kirhgoff avatar rema7 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.