[Python] Telegram bot use_context problem when trying to start [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I try to make a Telegram bot using Visual Studio Code. My Telegram bot code is:

    from telegram.ext import Updater, CommandHandler, MessageHandler, filters
    import requests

    texts = {"a": "", "b": "", "c": ""}

    def start(update, context):
    update.message.reply_text('Hello! Send /set a, b, or c followed by the text to set.')

    def set_text(update, context):
    try:
    key = context.args[0]
    text = " ".join(context.args[1:])
    if key in texts:
    texts[key] = text
    update.message.reply_text(f'Text for {key} set to: {text}')

    # HTTP POST request to a C# application
    url = "http://localhost:5000/settext"
    payload = {"key": key, "text": text}
    requests.post(url, data=payload)
    else:
    update.message.reply_text('Invalid key. Use a, b, or c.')
    except IndexError:
    update.message.reply_text('Usage: /set [a|b|c] [text]')

    def main():
    updater = Updater("TOKEN", use_context=True)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("set", set_text))

    updater.start_polling()
    updater.idle()

    if __name__ == '__main__':
    main()


    I got an error when trying to start the bot. The Python version is 3.12.0 and the python-telegram-bot version is 21.3

    Error:

    Traceback (most recent call last):
    File "E:\Users\SLap\source\repos\CrypTechTool\CrypTechTool\bin\Debug\net6.0-windows\telegram\bot.py", line 37, in <module>
    main()
    File "E:\Users\SLap\source\repos\CrypTechTool\CrypTechTool\bin\Debug\net6.0-windows\telegram\bot.py", line 27, in main
    updater = Updater("TOKEN", use_context=True)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: Updater.__init__() got an unexpected keyword argument 'use_context'


    How can I fix this? I used ChatGPT and searched on Google, but there wasn't any result.

    I want to start this bot, but I got an error when I tried to start it.

    Continue reading...

Compartilhe esta Página