简体   繁体   中英

I can't pin a telegram group chat message using bot

I am new to python and I was trying to pin a message in a telegram group chat via a bot, and I am using the "python-telegram-bot" package, but it throws an error I don't know what I did wrong.

The code

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))

The error I got

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'

I checked the documentation still I can't find the problem

You need to instantiate an instance of a Bot before you can call pin_chat_message().

So you would have something like this:

# 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM