我是python的新手,我试图通过bot将消息固定在电报群聊中,并且我使用的是“ 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'
我检查了文档仍然找不到问题
答案 0 :(得分:0)
在调用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)