20.0에 해당하는 글 1

AttributeError: 'Updater' object has no attribute 'dispatcher'

반응형

안녕하세요?

 

텔레그램봇을 다시 설치하거나, 따라하기 하다보면, 잘 안되는경우가 있을 겁니다.

최근에  telegram.ext 라이브러리가 upgrade되면서 Updater 사용법이 많이 변경되었습니다.

 

구글링에서 가장 많은 예시 들이 존재하는 버젼이 python-telegram-bot-13.3 버젼입니다.

20.0버젼에 맞는 예시들이 아직은 거의 없는것 같습니다. 

 

python-telegram-bot-13.3 사용시에는 다음과 같이 사용합니다.

def start(update: Update, _: CallbackContext) -> None:
    user = update.effective_user
    update.message.reply_markdown_v2(
        fr'Hi {user.mention_markdown_v2()}\!',
        reply_markup=ForceReply(selective=True),
    )


def help_command(update: Update, _: CallbackContext) -> None:
    update.message.reply_text('Help!') 

def run_bot(update: Update, _: CallbackContext) -> None:
    replica = update.message.text
    answer = bot(replica)
    update.message.reply_text(answer) 

    print(replica)
    print(answer)
    print()
    
def main() -> None:
    updater = Updater("token")
    dispatcher = updater.dispatcher
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, run_bot))

    # Initialize bot
    updater.start_polling()

    updater.idle()

main()

 

python-telegram-bot-20.0 에서는 다음과 같이 사용합니다. 많이 틀리죠?

from telegram.ext import *
import keys
    
print('Starting a bot....')
     
async def start_commmand(update, context):
    await update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    application = Application.builder().token(keys.token).build()

    # Commands
    application.add_handler(CommandHandler('start', start_commmand))

    # Run bot
    application.run_polling(1.0)

 

13.3 버젼을 사용하고 싶으시다면 제일 마직막에 13.3버젼으로 python-telegram-bot 을 다시 설치하시면 됩니다.

 

>pip install telegram
> pip uninstall python-telegram-bot telegram -y
>pip install python-telegram-bot
>pip install python-telegram-bot --upgrade
>pip install python-telegram-bot==13.3 # 20.0a0버젼부터는 dispatcher를 더이상 사용불가합니다. 

 

이상입니다.

 

https://stackoverflow.com/questions/74986002/attributeerror-updater-object-has-no-attribute-dispatcher

 

AttributeError: 'Updater' object has no attribute 'dispatcher'

When I run this code: from telegram.ext import * import keys print('Starting a bot....') def start_commmand(update, context): update.message.reply_text('Hello! Welcome To Store!') if

stackoverflow.com

 

반응형

댓글()