【问题标题】:Python-Telegram-Bot: send message through bot in different modulePython-Telegram-Bot:通过不同模块中的机器人发送消息
【发布时间】:2021-03-22 02:44:29
【问题描述】:

我正在使用 python-telegram-bot 库编写 Telegram 机器人。
当发布新的 YoutubeChannel 时,机器人应该向用户发送消息。 我创建了一个 telegram_bot.py,在其中创建了一个 TelegramBot 类。 在这个类中,我有这个功能:
telegram_bot.py

def __init__(self):
    self.updater = Updater(token=telegram_token, use_context=True)
    self.dispatcher = self.updater.dispatcher  
    self.updater.start_polling()

def send_message(self, text_message, context: CallbackContext):
    context.bot.send_message(
        chat_id="@<<my username>>", text=text_message)

在 main.py 中,我有一行代码应该使用上述函数发送消息,如下所示:
ma​​in.py

from telegram_bot import TelegramBot  

tg_bot = TelegramBot()
tg_bot.send_message("New video!")

但是,当我运行上面的代码时,我得到了这个错误:

TypeError: send_message() 缺少 1 个必需的位置参数:'context'

但是在 send_message 定义中我已经定义了 context

【问题讨论】:

    标签: python telegram python-telegram-bot


    【解决方案1】:

    这样解决:
    ma​​in.py

    tg_bot = TelegramBot()
    tg_bot.send_message("New video!", context=tg_bot.dispatcher)
    

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 2016-05-09
      • 2021-11-09
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多