'Python Telegram bot dispather issue with two modules
I am using python-telegram-bot (https://github.com/python-telegram-bot/python-telegram-bot) to write telegram bot on python.
I have 2 modules: main.py and game.py
main.py:
from game import Game
...
def menu_query(update: Update, context: CallbackContext):
chat = update.effective_chat
query = update.message.text
if "Game" in query:
game = Game(update, context)
game.pick()
game.combat()
game.combat_query()
def main():
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(MessageHandler(Filters.regex(r"Game"), menu_query))
dispatcher.add_handler(MessageHandler(Filters.regex(r"Attack") | Filters.regex(r"Defense"), Game.combat_query))))
updater.start_polling()
updater.idle()
main()
game.py:
class Game:
def __init__(self, update, context):
self.update = update
self.context = context
self.chat = update.effective_chat
def combat_query(self):
query = self.update.message.text
if "Attack" in query:
self.context.bot.send_message(chat_id=self.chat.id, text="ATT")
if "Defense" in query:
self.context.bot.send_message(chat_id=self.chat.id, text="DEF")
The problem is that "combat_query" is a method from game.py and I am receiving error:
No error handlers are registered, logging exception.
Traceback (most recent call last):
File "C:\Users\PycharmProjects\bot\venv\lib\site-packages\telegram\ext\dispatcher.py", line 555, in process_update
handler.handle_update(update, self, check, context)
File "C:\Users\PycharmProjects\bot\venv\lib\site-packages\telegram\ext\handler.py", line 198, in handle_update
return self.callback(update, context)
TypeError: combat_query() takes 1 positional argument but 2 were given
How can I solve it?
And also - is it possible to use or call dispatcher and handlers inside game.py?
UDP: I found this solution:
def game_f(update: Update, context: CallbackContext):
game = Game(update, context)
return game.combat_query()
...
dispatcher.add_handler(MessageHandler(Filters.regex(r"Attack") | Filters.regex(r"Defense"), game_f))))
Is that good or maybe I can do something instead?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
