我想重写控制台IO应用程序(总和计数)到messenger-bot。 StdIn.readLine()
让我在递归中获得下一个输入数字。
object HelloCountBot extends TelegramBot with Polling with Commands {
def go(number: Long, chatId: Long): IO[Long] =
for {
input <- ??? /*here I need get the next number*/
acc <- input.toLong match {
case 0L => sendMessageMethodIO(chatId, "The sum is:") *> IO(0L)
case _ => go(number + 1, chatId)
}
acc <- IO(acc + input.toLong)
} yield acc
/*input point for every new message*/
override def onMessage(message: Message) = message.text match {
case Some(text) if text == "start" => go(1, message.chat.id)
.unsafeRunSync
}
def main(args: Array[String]): Unit = HelloCountBot.run()
}
如何组织代码以从带有Unit返回的方法onMessage获取递归内的下一条消息。
我没有在https://github.com/bot4s/telegram找到任何以你想要的方式接收消息的方法。所以我认为最好的选择是创建有状态机器人,如下例所示:https://github.com/bot4s/telegram/blob/master/examples/src/StatefulBot.scala。
因此,如果我正确理解您的代码,可以按以下方式重新组织(特征PerChatState
取自上面的链接):
object HelloCountBot
extends TelegramBot
with Polling
with Commands
with PerChatState[Long] {
override def onMessage(message: Message): Future[Unit] = {
implicit val msg = message
message.text match {
case Some(text) if text == "start" =>
Future {
setChatState(0L)
}
case Some(value) if value == "0" =>
withChatState {
sum =>
reply(s"The sum is ${sum.getOrElse(0L)}")
.map(_ => clearChatState)
}
case Some(value) =>
withChatState {
mayBeSum =>
Future {
mayBeSum.foreach(
sum => setChatState(sum + value.toLong)
)
}
}
}
}
def main(args: Array[String]): Unit = HelloCountBot.run()
}
它使用Futures,但如果您愿意,可以将其重写为IO。