Skip to content

paoloboni/telegram4s

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

telegram4s

Build Status Latest version

Telegram client for Scala

Quick start

If you use sbt add the following dependency to your build file:

libraryDependencies += "io.github.paoloboni" %% "telegram4s" % "<version>"

Example

import cats.effect.{ExitCode, IO, IOApp}
import log.effect.LogWriter
import org.telegram.api.updates.TLAbsUpdates
import org.telegram.bot.kernel.MainHandlerFactory
import org.telegram.bot.structure.IUser
import org.telegram4s.TelegramClient

object TelegramSampleApp extends IOApp {

  private implicit val mainHandlerFactory: MainHandlerFactory[IO] = new MainHandlerFactory[IO]

  private val log: LogWriter[IO] = implicitly

  private val clientApiKey  = 123455
  private val clientApiHash = "6e6bc4e49dd477ebc98ef4046c067b5f"
  private val phoneNumber   = "+4407700000000"

  val bob: IUser = ???

  override def run(args: List[String]) = {
    val program = for {
      client <- TelegramClient.initialise(
        clientApiKey = clientApiKey,
        clientApiHash = clientApiHash,
        phoneNumber = phoneNumber,
        stateFilePath = "/tmp/api_state",
        getAuthCode = getAuthCode
      )
      events <- client.getUpdatesStream
      processEvents <- events
        .map(eventHandler)
        .evalMap(log.info(_))
        .compile
        .drain
        .start
      _ <- client.sendMessage(bob, "Hi Bob!")
      _ <- processEvents.join
    } yield ()
    program.redeem(
      { t =>
        t.printStackTrace()
        ExitCode(1)
      },
      _ => ExitCode.Success
    )
  }

  private def eventHandler(updates: TLAbsUpdates): String = {
    // handle possible instances of org.telegram.api.updates.TLAbsUpdates
    "event received"
  }

  private def getAuthCode: IO[String] =
    for {
      _        <- IO.pure(println(s"Enter authentication code: "))
      authCode <- IO.pure(scala.io.StdIn.readLine())
    } yield authCode

}

About

Telegram client for Scala

Resources

License

Stars

Watchers

Forks

Packages

No packages published