如何从消息中删除图像(Telethon,Python)

问题描述 投票:0回答:1

请告诉我如何使用 event.edit 通过删除照片来编辑消息:

@client.on(events.CallbackQuery(func=lambda e: e.data == b"code"))
async def add_ads(event: CallbackQuery.Event):
    await event.edit(file=???)

消息最初带有图像

或者是event.edit无法使用而需要使用其他方法吗?

event.edit(file=None) 不起作用

python telegram-bot
1个回答
0
投票

要在 Python 中使用 Telethon 从消息中删除图像,可以使用

delete
对象的
Message
方法。这是一个基本示例:

from telethon.sync import TelegramClient

api_id = 'your_api_id'
api_hash = 'your_api_hash'
phone_number = 'your_phone_number'

client = TelegramClient('session_name', api_id, api_hash)

async def remove_image_from_message(chat_id, message_id):
    try:
        message = await client.get_messages(chat_id, ids=message_id)
        if message.media:
            await client.delete_messages(chat_id, message_id)
            print("Image removed successfully.")
        else:
            print("The specified message does not contain an image.")
    except Exception as e:
        print(f"Error: {e}")

async def main():
    await client.start(phone_number)
    chat_id = 'your_chat_id'  # Replace with the actual chat ID
    message_id = 123  # Replace with the actual message ID
    await remove_image_from_message(chat_id, message_id)
    await client.disconnect()

if __name__ == '__main__':
    client.loop.run_until_complete(main())

确保将

'your_api_id'
'your_api_hash'
'your_phone_number'
'your_chat_id'
123
替换为您的实际 API 凭据、电话号码、聊天 ID 和消息 ID。

此脚本定义了一个函数

remove_image_from_message
,它接受
chat_id
message_id
,检索指定的消息,检查它是否包含媒体(图像),如果包含则删除该消息。
main
函数初始化客户端,从指定消息中删除图像,然后断开连接。

© www.soinside.com 2019 - 2024. All rights reserved.