首页 > 解决方案 > 我无法使用机器人固定电报群聊消息

问题描述

我是 python 新手,我试图通过机器人在电报群聊中固定一条消息,我正在使用“python-telegram-bot”包,但它抛出了一个错误我不知道我做错了什么.

编码

def pinMsg(update, context):
    Bot.pin_chat_message(chat_id=update.message.chat.id, message_id=update.message.message_id, disable_notification=None, timeout=None)

def main():
    updater = Updater(TOKEN, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.group, pinMsg))

我得到的错误

C:/Users/Jo/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/Jo/Documents/main.py"
2020-10-13 21:11:22,342 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "C:\Users\Jo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telegram\ext\dispatcher.py", line 340, in process_update
    handler.handle_update(update, self, check, context)
  File "C:\Users\Jo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telegram\ext\handler.py", line 119, in handle_update
    return self.callback(update, context)
  File "c:/Users/Jo/Documents/main.py", line 21, in pinMsg
    Bot.pin_chat_message(chat_id=update.message.chat.id, message_id=update.message.message_id, disable_notification=None, timeout=None)
TypeError: pin_chat_message() missing 1 required positional argument: 'self'

我检查了文档仍然找不到问题

标签: pythontelegram-botpython-telegram-bot

解决方案


在调用 pin_chat_message() 之前,您需要实例化一个 Bot 实例。

所以你会有这样的事情:

# Create an instance of the bot
my_bot = Bot(token: 'YOUR TOKEN', ...)

def pinMsg(update, context):
    # Notice how we use my_bot instead of Bot
    my_bot.pin_chat_message(chat_id=update.message.chat.id, message_id=update.message.message_id, disable_notification=None, timeout=None)

推荐阅读