atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.36k stars 221 forks source link

Keep getting a QUERY_ID_INVALID exception #33

Closed objective-mj closed 8 years ago

objective-mj commented 8 years ago

Hey I keep receiving this error when trying to make Inline work

#<Telegram::Bot::Exceptions::ResponseError: Telegram API has returned the error. (ok: "false", error_code: "400", description: "[Error]: Bad Request: QUERY_ID_INVALID")>

And this is my stacktrace

C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/api.rb:49:in `call'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/api.rb:40:in `method_missing'
main.rb:135:in `block (2 levels) in <main>'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/client.rb:43:in `block in fetch_updates'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/client.rb:38:in `each'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/client.rb:38:in `fetch_updates'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/client.rb:30:in `listen'
main.rb:121:in `block in <main>'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/client.rb:23:in `run'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/telegram-bot-ruby-0.4.1/lib/telegram/bot/client.rb:11:in `run'
main.rb:15:in `<main>'

With my own lines of code looking like this:

135: bot.api.answer_inline_query(inline_query_id: message.id, results: results)
121: bot.listen do |message|
15: Telegram::Bot::Client.run(token) do |bot|

As far as I can see it has something to do with the getting update part. The query_id changes when new updates are gathered which means that request take are a bit slow will continue to be computed while the id changes.

EDIT: sorry for the close, reopen - was a mistake Now I have tried parsing an overwriting timeout var to the Client.run method like so:

Telegram::Bot::Client.run(token, timeout: 100) do |bot|

But it doesn't seem to help.

atipugin commented 8 years ago

Could you provide your code?

objective-mj commented 8 years ago

Gonna boil it down to avoid clutter.

This is my main.rb

Telegram::Bot::Client.run(token) do |bot| # line 15

  bot.listen do |message|  # line 121
    case message
    when Telegram::Bot::Types::InlineQuery
      arr = ComicVine.make_call(message.query)
      results = []
      count = 0
      arr.take(50).each do |char|
        results << Telegram::Bot::Types::InlineQueryResultArticle
                   .new(id: count += 1,
                        title: char[0],
                        thumb_url: char[1],
                        description: char[2],
                        parse_mode: char[3],
                        message_text: char[4]
                        )
      end
      bot.api.answer_inline_query(inline_query_id: message.id, results: results) # line 135
    end
  end
end

And this is my ComicVine.rb

def self.make_call(query)
  url = "SOME_URL" 
  url = URI.encode(url)
  return (JSON.parse httpclient.get_content(url))['result'] # this call can be slow for large results
end

IMPORTANT NOTE: It is worth noticing that if I have a http call that takes only a couple of seconds, everything works fine. For example, I make an inline query with the text 'owl' and I have results showing without exceptions within 5 seconds. image But if I make, say 'Avengers' there are more results from the api that I call and so it will take ~30 seconds before I am returning results. And then upon the bot.api.answer_inline_query method the exception happens

atipugin commented 8 years ago

Ok, i've reproduced your error, and i have only one idea - inline query id is valid only for a limited period of time (about ~10 seconds). It's pretty reasonable because inline bots are supposed to be fast.

Anyway, i didn't find anything about it in official docs.