nickoala / telepot

Python framework for Telegram Bot API
MIT License
2.43k stars 475 forks source link

Delete message function #349

Open alirezaafzalaghaei opened 6 years ago

alirezaafzalaghaei commented 6 years ago

According to telegram bot api delete message function needs chat_id and msg_id but telepot api only gets 'msg_id':

def deleteMessage(self, msg_identifier):
        """
        See: https://core.telegram.org/bots/api#deletemessage

        :param msg_identifier:
            Same as ``msg_identifier`` in :meth:`telepot.Bot.editMessageText`,
            except this method does not work on inline messages.
        """
        p = _strip(locals(), more=['msg_identifier'])
        p.update(_dismantle_message_identifier(msg_identifier))
        return self._api_request('deleteMessage', _rectify(p))
nickoala commented 6 years ago

The function telepot.message_identifier(msg) gives you the, well, message identifier to use for the method deleteMessage() :smile:

alirezaafzalaghaei commented 6 years ago

@nickoala I have that. Telegram needs one more argument chat_id which deleteMessage function didn't get this. For example when i call this:

bot.deleteMessage(msg_id)

This error will raise:

{"ok":false,"error_code":400,"description":"Bad Request: chat identifier is not specified"}
nickoala commented 6 years ago

You should do:

bot.deleteMessage(telepot.message_identifier(msg))
lespeol commented 6 years ago

Hi, I'm trying to delete a user message by using bot.deleteMessage(telepot.message_identifier(msg)). I'm able to delete every bot message, but when I'm trying to delete a user message I get this:

TelegramError: (u"Bad Request: message can't be deleted", 400, {u'error_code': 400, u'ok': False, u'description': u"Bad Request: message can't be deleted"}) I'm not figuring out why.

Thank you in advance

nickoala commented 6 years ago

By "user message", do you mean "a message sent by a human user"? If so, then of course the bot will not be able to delete it. The bot is allowed to delete messages sent by itself only!

lespeol commented 6 years ago

@nickoala Exactly, I mean a message sent by a human user. But how can I clear a whole chat then? No method to do this either? For example: https://github.com/nickoala/telepot/issues/339

Thanks nick for your answer and your work

PabloLP97 commented 6 years ago

@lespeol In groups make sure the bot have delete permissions. In private chats, you can't erase user messages. I use my bot to ban certain sticker packs in my group.

proditaki commented 6 years ago

You can make a tuple of the chat_id and the message_id and that will work for deleting the messages. example

test = bot.sendMessage(chat_id, 'Test')
messageId = test['message_id']
bot.deleteMessage((chat_id, message_id))

i do similar things for my Bot.

suibaf commented 5 years ago

@proditaki With your example you delete a message sent by a bot. If I understand your example. But also I need to delete a message from human user as @lespeol . The user send a password, if correct then he can continue to use the bot otherwise not. Is there a solution? Thank you in advance.

lespeol commented 5 years ago

Still following. @suibaf you're right. I need to delete a message sent from user to my bot.

proditaki commented 5 years ago

@suibaf @lespeol

Sorry for the reply. When handling the incoming message you can do this. What you can do is (the bot needs to be channel superuser/admin)

# create bot
bot = telepot.Bot(APIKEY)
# handler
MessageLoop(bot, handle).run_as_thread()

#inside handler
def handle(msg):
        # after handling the msg you can delete it like this
    bot.deleteMessage(telepot.message_identifier(msg))
lespeol commented 5 years ago

@proditaki And you, my friend, have a paid bier if you come to Italy. So it was all "just" to create a handler. I don't know why I didn't think about, but that is.

It Works!

Thank you so much!