Created
December 16, 2016 21:50
-
-
Save mukel/40b06e97069ab08bdecf3ba9df8776d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.mukel.telegrambot4s.examples | |
import akka.actor.{Actor, ActorRef, Props, Terminated} | |
import info.mukel.telegrambot4s.actors.{ActorDispatcher} | |
import info.mukel.telegrambot4s.api.{AkkaDefaults, Commands, Polling} | |
import info.mukel.telegrambot4s.models.{Message, Update} | |
class ActorBot(token: String) extends TestBot(token) with Polling with Commands with PerChatDispatcher { | |
on("/hello") { implicit msg => _ => | |
reply("Hello World!") | |
} | |
} | |
trait PerChatDispatcher extends ActorDispatcher with Commands with AkkaDefaults { | |
class Broker extends Actor { | |
val chatActors = collection.mutable.Map[Long, ActorRef]() | |
def receive = { | |
case u : Update => | |
u.message.foreach { m => | |
val id = m.chat.id | |
val handler = chatActors.getOrElseUpdate(m.chat.id, { | |
val worker = system.actorOf(Props(new Worker), s"worker_$id") | |
context.watch(worker) | |
worker | |
}) | |
handler ! m | |
} | |
case Terminated(worker) => | |
chatActors.find(_._2 == worker).foreach { | |
case (k, _) => chatActors.remove(k) | |
} | |
case _ => | |
} | |
} | |
class Worker extends Actor { | |
def receive = { | |
case m : Message => | |
reply(self.toString)(m) | |
case _ => | |
} | |
} | |
override val dispatcher = Some(system.actorOf(Props(new Broker), "broker")) | |
} | |
object Launcher extends App { | |
new ActorBot("TOKEN").run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment