Closed AlexanderBykin closed 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")
}
}
@mukel thanks for code sharing your code should be placed into Examples
@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?
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"
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
here is the code
after button click we see
callbackQuery.gameShortName = None
andcallbackQuery.data = Some({ game_short_name: "cpptest" })