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
26.12k stars 5.29k forks source link

[BUG] Exception when starting bot with Persistence enabled where non-default `store_data` value is provided #3971

Closed msenior85 closed 11 months ago

msenior85 commented 11 months ago

Steps to Reproduce

  1. Create a telegram application with persistence enabled and store_data argument of the Persistence class having a non-default value
    persistence = PicklePersistence(
    filepath=".bot_data",
    store_data=PersistenceInput.bot_data,
    )
    app = (
    ApplicationBuilder()
    .token("BOT_TOKEN")
    .persistence(persistence)
    .build()
    )
  2. Run the bot
  3. AttributeError Exception will be raised when starting the bot

Expected behaviour

Bot will start up without exception and start polling the telegram server for updates.

Actual behaviour

Exception is raised when starting the bot

Traceback (most recent call last):
  File "/home/george/Projects/echo/bot.py", line 51, in <module>
    main()
  File "/home/george/Projects/echo/bot.py", line 43, in main
    .build()
  File "/home/george/Projects/telegram-quiz-bot/venv/lib/python3.10/site-packages/telegram/ext/_applicationbuilder.py", line 328, in build
    persistence.set_bot(bot)
  File "/home/george/Projects/telegram-quiz-bot/venv/lib/python3.10/site-packages/telegram/ext/_basepersistence.py", line 181, in set_bot
    if self.store_data.callback_data and (not isinstance(bot, ExtBot)):
AttributeError: '_collections._tuplegetter' object has no attribute 'callback_data'

Operating System

Ubuntu 22.04

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 20.6
Bot API 6.9
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]

Relevant log output

Traceback (most recent call last):
  File "/home/george/Projects/echo/bot.py", line 51, in <module>
    main()
  File "/home/george/Projects/echo/bot.py", line 43, in main
    .build()
  File "/home/george/Projects/telegram-quiz-bot/venv/lib/python3.10/site-packages/telegram/ext/_applicationbuilder.py", line 328, in build
    persistence.set_bot(bot)
  File "/home/george/Projects/telegram-quiz-bot/venv/lib/python3.10/site-packages/telegram/ext/_basepersistence.py", line 181, in set_bot
    if self.store_data.callback_data and (not isinstance(bot, ExtBot)):
AttributeError: '_collections._tuplegetter' object has no attribute 'callback_data'

Additional Context

Exception is raised only when persistence is enabled with store_data having a user defined value such as store_data=PersistenceInput.{bot, user, chat, callback}_data. Defining persistence with default value for store_data does not raise an exception.

Example code is provided below:

import logging
import os

from telegram import Update
from telegram.ext import (
    ApplicationBuilder,
    CommandHandler,
    ContextTypes,
    PersistenceInput,
    PicklePersistence,
)

logger = logging.getLogger("testbot")
logging.basicConfig(
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await context.bot.send_message(
        update.effective_chat.id,
        f"Welcome {update.effective_user.first_name}. Use /echo [text] for the bot to respond with [text].",
    )

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    text = " ".join(context.args)
    await update.message.reply_text(text)

def main():
    persistence = PicklePersistence(
        filepath=".bot_data",
        store_data=PersistenceInput.bot_data,
    )
    app = (
        ApplicationBuilder()
        .token("BOT_TOKEN")
        .persistence(persistence)
        .build()
    )
    app.add_handler(CommandHandler("start", start))
    app.add_handler(CommandHandler("echo", echo))
    app.run_polling()

if __name__ == "__main__":
    main()
Bibo-Joshi commented 11 months ago

@msenior85 Hi. Parameter store_data of telegram.ext.BasePersistence expects and instance of the telegram.ext.PersistenceInput class, so you need to build such an instance.

msenior85 commented 11 months ago

Thanks @Bibo-Joshi. I guess the wiki and documentation could be improved to make this clear. I will submit a PR with an example bot using persistenceinput.