python-telegram-bot / python-telegram-bot

We have made you a wrapper you can't refuse
https://python-telegram-bot.org
GNU General Public License v3.0
25.99k stars 5.26k forks source link

[BUG] AttributeError: 'bool' object has no attribute 'de_json' #2290

Closed PXNX closed 3 years ago

PXNX commented 3 years ago

Using 13.1 within an hour I get following error after which the bot does not respond anymore.

2021-01-04 17:16:16,674 - telegram.ext.updater - ERROR - unhandled exception in Bot:1415969330:updater
Traceback (most recent call last):
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 236, in _thread_wrapper
    target(*args, **kwargs)
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 441, in _start_polling
    self._network_loop_retry(
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 465, in _network_loop_retry
    if not action_cb():
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 416, in polling_action_cb
    updates = self.bot.get_updates(
  File "<decorator-gen-31>", line 2, in get_updates
  File "C:\ptb-realme\lib\site-packages\telegram\bot.py", line 135, in decorator
    result = func(*args, **kwargs)
  File "C:\ptb-realme\lib\site-packages\telegram\bot.py", line 2653, in get_updates
    return [Update.de_json(u, self) for u in result]  # type: ignore
  File "C:\ptb-realme\lib\site-packages\telegram\bot.py", line 2653, in <listcomp>
    return [Update.de_json(u, self) for u in result]  # type: ignore
  File "C:\ptb-realme\lib\site-packages\telegram\update.py", line 249, in de_json
    data['message'] = Message.de_json(data.get('message'), bot)
  File "C:\ptb-realme\lib\site-packages\telegram\message.py", line 452, in de_json
    data['chat'] = Chat.de_json(data.get('chat'), bot)
  File "C:\ptb-realme\lib\site-packages\telegram\chat.py", line 185, in de_json
    data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot)
AttributeError: 'bool' object has no attribute 'de_json'
Exception in thread Bot:1415969330:updater:
Traceback (most recent call last):
  File "C:\Users\Pentex\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Pentex\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 236, in _thread_wrapper
    target(*args, **kwargs)
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 441, in _start_polling
    self._network_loop_retry(
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 465, in _network_loop_retry
    if not action_cb():
  File "C:\ptb-realme\lib\site-packages\telegram\ext\updater.py", line 416, in polling_action_cb
    updates = self.bot.get_updates(
  File "<decorator-gen-31>", line 2, in get_updates
  File "C:\ptb-realme\lib\site-packages\telegram\bot.py", line 135, in decorator
    result = func(*args, **kwargs)
  File "C:\ptb-realme\lib\site-packages\telegram\bot.py", line 2653, in get_updates
    return [Update.de_json(u, self) for u in result]  # type: ignore
  File "C:\ptb-realme\lib\site-packages\telegram\bot.py", line 2653, in <listcomp>
    return [Update.de_json(u, self) for u in result]  # type: ignore
  File "C:\ptb-realme\lib\site-packages\telegram\update.py", line 249, in de_json
    data['message'] = Message.de_json(data.get('message'), bot)
  File "C:\ptb-realme\lib\site-packages\telegram\message.py", line 452, in de_json
    data['chat'] = Chat.de_json(data.get('chat'), bot)
  File "C:\ptb-realme\lib\site-packages\telegram\chat.py", line 185, in de_json
    data['pinned_message'] = Message.de_json(data.get('pinned_message'), bot)
AttributeError: 'bool' object has no attribute 'de_json'
2021-01-04 17:16:17,549 - telegram.ext.dispatcher - CRITICAL - stopping due to exception in another thread
PXNX commented 3 years ago

Seems like replacing line 185 in chat.py with

try:
    data['pinned_messages']= Message.de_json(data.get('pinned_message'), bot)
except:
    data['pinned_messages'] = None

fixes it.

Bibo-Joshi commented 3 years ago

Hi. This is a very strange error, because Message is definetly a class and not a bool. Did you do something like telegram.Masseg = True somewhere in your code? Can you provide a minimal reproducable example?

PXNX commented 3 years ago

Here we go:

import os
import telegram
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext

TOKEN = "TOKEN"

def test(update: Update, context: CallbackContext):
    delay_group(update, context, "Fancy text")

def delay_group(update: Update, context: CallbackContext, text: str):
    if update.message.reply_to_message:
        update.message.reply_to_message.reply_text(
            text=text,
            parse_mode=telegram.ParseMode.HTML)
    else:
        reply_message = context.bot.send_message(
            chat_id=update.message.chat_id,
            text=text,
            parse_mode=telegram.ParseMode.HTML)
        context.job_queue.run_once(delete, 30, context=reply_message.chat_id, name=str(reply_message.message_id))

    update.message.delete()

def delete(context: CallbackContext):
    telegram.Message = context.bot.delete_message(chat_id=str(context.job.context), message_id=context.job.name)

if __name__ == '__main__':
    updater = Updater(TOKEN, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("test", test))

    PORT = int(os.environ.get('PORT', 5000))

    updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)
    updater.bot.setWebhook('https://app-name.herokuapp.com/' + TOKEN)

    updater.start_polling()
    updater.idle()
PXNX commented 3 years ago

Changing

def delete(context: CallbackContext):
    telegram.Message = context.bot.delete_message(chat_id=str(context.job.context), message_id=context.job.name)

to

def delete(context: CallbackContext):
    context.bot.delete_message(chat_id=str(context.job.context), message_id=context.job.name)

resolved this issue.

My bad. That was quite stupid.