|
@@ -17,8 +17,6 @@
|
|
|
|
|
|
package org.congressodeiradicali.karlmarx
|
|
|
|
|
|
-import cats.instances.future._
|
|
|
-import cats.syntax.functor._
|
|
|
import com.bot4s.telegram.api.RequestHandler
|
|
|
import com.bot4s.telegram.api.declarative.{Command, Commands}
|
|
|
import com.bot4s.telegram.clients.ScalajHttpClient
|
|
@@ -61,20 +59,22 @@ class Bot(val token: String) extends TelegramBot
|
|
|
.accept(cmd)
|
|
|
}
|
|
|
|
|
|
+ def runHandler(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command, message: Message): Future[String] = {
|
|
|
+ if (runAgainst(handlerPair, botUser, cmd))
|
|
|
+ handlerPair._2(message).flatMap {
|
|
|
+ case Some(s) => Future(s)
|
|
|
+ case None => Future.failed(new RuntimeException("Handler did not return a message"))
|
|
|
+ }
|
|
|
+ else
|
|
|
+ Future.failed(new RuntimeException("Filter does not apply"))
|
|
|
+ }
|
|
|
|
|
|
onExtMessage { case (message, botUser) =>
|
|
|
using(command) { cmd =>
|
|
|
- val promises = plugins
|
|
|
+ Future.sequence(plugins
|
|
|
.flatMap(_.handlers)
|
|
|
- .filter(runAgainst(_, botUser, cmd)) // Filter handlers that match the command
|
|
|
- .map(_._2(message)) // Run the handler
|
|
|
- .map {
|
|
|
- _.map {
|
|
|
- case Some(s) => reply(s)(message).void
|
|
|
- }
|
|
|
- } // Send replies
|
|
|
-
|
|
|
- Future.sequence(promises).flatMap(_ => Future{unit}) // Discard the content
|
|
|
+ .map(runHandler(_, botUser, cmd, message))
|
|
|
+ ).flatMap(_ => Future{unit}) // Discard the content
|
|
|
}(message)
|
|
|
}
|
|
|
}
|