bot4s / telegram

Telegram Bot API Wrapper for Scala
Apache License 2.0
418 stars 101 forks source link

GameShortName doesn't handle at receiveCallbackQuery #35

Closed AlexanderBykin closed 7 years ago

AlexanderBykin commented 7 years ago

here is the code

object PokerTelegramBot extends TelegramBot with Polling with Messages {
  lazy val token = "SOME_TOKEN"

  override def receiveMessage(msg: Message) = {
    msg.text.foreach {
      case "/start" ⇒
        println("start handled")
        val inlineBtns = InlineKeyboardMarkup(Seq(Seq(InlineKeyboardButton(
          text = "Play now!",
          callbackData = Some("{ game_short_name: \"cpptest\" }")))))
        reply("To play game press button", replyMarkup = Some(inlineBtns))(msg)

      case any ⇒ println(s"unhandled message <$any>")
    }
  }

  override def receiveCallbackQuery(callbackQuery: CallbackQuery) = {
    println(s"gameShortName: ${callbackQuery.gameShortName}")
    println(s"data: ${callbackQuery.data}")
  }
}

after button click we see callbackQuery.gameShortName = None and callbackQuery.data = Some({ game_short_name: "cpptest" })

mukel commented 7 years ago

There's no need pass JSON strings around; the API is fully typed.

reply(...) is just a wrapper for SendMessage(...). For "sending" a game you should use SendGame(..); which makes me think about adding a replyWithGame method (see code below).

Here's my version of your example with some extra goodies.

class PokerBot extends TelegramBot with Polling with Commands with Callbacks {

  lazy val token = ...

  onCommand("/start") { implicit msg =>
    // Note that the button doesn't contain the game_short_name.
    val playNowBtn = InlineKeyboardButton.callbackGame("🎮 Play now!")
    // or equivalent InlineKeyboardButton("Play Now!", callbackGame = Some(CallbackGame))

    val inlineBtns = InlineKeyboardMarkup(Seq(Seq(playNowBtn)))

    request(
      SendGame(msg.source,
        gameShortName = "play_2048",
        replyMarkup = Some(inlineBtns)))
  }

  onCallbackQuery { implicit callbackQuery =>
    println(s"gameShortName: ${callbackQuery.gameShortName}")
    println(s"data: ${callbackQuery.data}")

    // You must acknowledge callback queries, even if there's no response.
    // e.g. just ackCallback()

    // To open game, you may need to pass extra (url-encoded) information to the game.
    ackCallback(url = Some("https://my.awesome.game.com/awesome"))
  }

  def replyWithGame(gameShortName       : String,
                    disableNotification : Option[Boolean] = None,
                    replyToMessageId    : Option[Long] = None,
                    replyMarkup         : Option[ReplyMarkup] = None)
                   (implicit msg: Message): Future[Message] = {
    request(
      SendGame(msg.source,
        gameShortName = gameShortName,
        replyMarkup = replyMarkup,
        disableNotification = disableNotification,
        replyToMessageId = replyToMessageId
      ))
  }

  onCommand('trivia) { implicit msg =>
    replyWithGame("trivia_game")
  }

  onCommand('tictactoe, 'xoxo) { implicit msg =>
    replyWithGame("tictactoe_game")
  }
}
AlexanderBykin commented 7 years ago

@mukel thanks for code sharing your code should be placed into Examples

AlexanderBykin commented 7 years ago

@mukel i am definitely don't understand why ackCallback(url = Some("https://my.awesome.game.com/awesome")) throws me Telegram API exception info.mukel.telegrambot4s.api.TelegramApiException: Bad Request: URL_INVALID

maybe i missed something?

AlexanderBykin commented 7 years ago

as described in Gaming Platform documentation

Launching the Game

Once the game is created, your bot can send it to chats as regular messages, or offer them via inline mode. The game message will always have an inline Play button.

When this button is pressed, your bot gets a callback query that indicates the requested game. You provide the correct URL for this particular user and the app automatically opens the game in the in-app browser.

but no body explain what is "correct URL"

AlexanderBykin commented 7 years ago

exception Bad Request: URL_INVALID was happening when i used Desktop version, next i tried to open it with Android app where i noticed to allow to send my credentials and now all working fine