Open alirezaafzalaghaei opened 6 years ago
The function telepot.message_identifier(msg)
gives you the, well, message identifier to use for the method deleteMessage()
:smile:
@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"}
You should do:
bot.deleteMessage(telepot.message_identifier(msg))
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
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!
@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
@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.
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.
@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.
Still following. @suibaf you're right. I need to delete a message sent from user to my bot.
@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))
@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!
According to telegram bot api
delete message
function needschat_id
andmsg_id
but telepot api only gets 'msg_id':