rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.68k stars 1.18k forks source link

Error answering inline query: [400] Bad Request: can't find field "type" #199

Closed Bmak closed 7 years ago

Bmak commented 7 years ago

Hello! I've got this error every time when I'm trying to use AnswerInlineQuery with InlineQueryResultGame.

Here's the stack trace: org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error answering inline query: [400] Bad Request: can't find field "type" at org.telegram.telegrambots.api.methods.AnswerInlineQuery.deserializeResponse(AnswerInlineQuery.java:152) at org.telegram.telegrambots.api.methods.AnswerInlineQuery.deserializeResponse(AnswerInlineQuery.java:21) at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:678) at org.telegram.telegrambots.bots.AbsSender.answerInlineQuery(AbsSender.java:74) at org.telegram.updateshandlers.MyProjectHandler.SetAnswerInlineQuery(MyProjectHandler.java:202) at org.telegram.updateshandlers.MyProjectHandler.onUpdateReceived(MyProjectHandler.java:107) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:241)

Here's my code: ... AnswerInlineQuery answer = new AnswerInlineQuery(); answer.setInlineQueryId(inlineQuery.getId()); answer.setResults(getInlineGame(answer.getInlineQueryId(), inlineQuery.getQuery())); try { answerInlineQuery(answer); } catch (TelegramApiException e) { e.printStackTrace(); } ... private InlineQueryResultGame getInlineGame(String id, String game) { InlineQueryResultGame resultGame = new InlineQueryResultGame(); resultGame.setId(id); resultGame.setGameShortName(game); return resultGame; }

I can't understand why it's happening because InlineQueryResultGame does have field "type" private static final String TYPE_FIELD = "type"; ... @JsonProperty(TYPE_FIELD) private final String type = "game"; ///< Type of the result, must be "game"

Bmak commented 7 years ago

The problem is solved. But I think you need to update InlineQueryResultGame class toString() method. You forgot to add type field. Just did not notice that. Should look like this: @Override public String toString() { return "InlineQueryResultGame{" + "type='" + type + '\'' + ", id='" + id + '\'' + ", gameShortName='" + gameShortName + '\'' + ", replyMarkup=" + replyMarkup + '}'; }

rubenlagus commented 7 years ago

Will be present in next version

rubenlagus commented 7 years ago

Should be also fixed ;)