X Tutup
Skip to content

Bot crashes, when user clicks on inline button of an inline query result #571

@manorom

Description

@manorom

Steps to reproduce

  1. Have a bot that
    a. Has a CallbackQueryHandler.
    b. Has a ConversationHandler registered before the CallbackQueryHandler.
    c. Displays inline buttons on an inline query result.
  2. Get a query result with inline buttons and click on them.

Expected behaviour

The bot handles the callback query.

Actual behaviour

An AttributeError: 'NoneType' object has no attribute 'id' is raised by the ConversationHandler

Configuration

Operating System: Win7

Version of Python, python-telegram-bot & dependencies:
python-telegram-bot 5.3.0
urllib3 1.20
certifi 2017.04.17
future 0.16.0
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]

I'm using the current master branch. Not the version from PyPI

Logs

2017-04-22 17:07:45,210 - telegram.ext.dispatcher - ERROR - An uncaught error wa
s raised while processing the update
Traceback (most recent call last):
  File "C:\ptb-callback-bug\env\lib\site-packages\telegram\ext\dispatcher.py", l
ine 265, in process_update
    if handler.check_update(update):
  File "C:\ptb-callback-bug\env\lib\site-packages\telegram\ext\conversationhandl
er.py", line 173, in check_update
    key = self._get_key(update)
  File "C:\ptb-callback-bug\env\lib\site-packages\telegram\ext\conversationhandl
er.py", line 154, in _get_key
    key.append(chat.id)
AttributeError: 'NoneType' object has no attribute 'id'
2017-04-22 17:08:22,658 - telegram.ext.updater - WARNING - Exiting immediately!

Example Code

from telegram import  (InlineKeyboardMarkup, InlineKeyboardButton,
                      InlineQueryResultArticle, InputTextMessageContent)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters,
                          CallbackQueryHandler, ConversationHandler, InlineQueryHandler)

import logging

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

def error(bot, update, error):
    logger.warn('Update "%s" caused error "%s"' % (update, error))


# this is just here so we can have a ConversationHandler
def convo(bot, update):
    return 1

def end_convo(bot, update):
    return ConversationHandler.END


def inline_query(bot, update):
    inline_results = [
        InlineQueryResultArticle(
            id=1,
            title='show keyboard to crash the bot',
            input_message_content=InputTextMessageContent(
                'If you press the button below, you will crash the bot'),
            reply_markup=InlineKeyboardMarkup([[
                InlineKeyboardButton('Click', callback_data='data')
            ]])) ]
    update.inline_query.answer(inline_results)

def button_pressed(bot, update):
    update.callback_query.answer()

def main():
    updater = Updater('TOKEN')
    updater.dispatcher.add_error_handler(error)
    updater.dispatcher.add_handler(ConversationHandler(
        entry_points=[CommandHandler('start', convo)],
        states={
            1: [MessageHandler(Filters.text, end_convo)]
        },
        fallbacks=[MessageHandler(Filters.text, end_convo)]
    ))
    updater.dispatcher.add_handler(InlineQueryHandler(inline_query))
    updater.dispatcher.add_handler(CallbackQueryHandler(button_pressed))
    updater.start_polling()
    updater.idle()

main()

If i'm not mistaken this could simply be fixed by adding the constraint or update.callback_query and not update.callback_query.message to the line 168 of conversationhandler.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup