Telethon delete_messages似乎没有任何作用

时间:2018-07-08 16:16:21

标签: python message telegram telethon

帮助:Telethon相对较新,并且delete_messages()功能存在问题。似乎对我无能为力...

最初,我从send_message获取消息ID,即从返回对象的ID值获取。它返回的值如1、2等。虽然不确定这是消息ID。

我以delete_messages的身份发送给delete_messages(channel, [id])

我从以下渠道获得频道(这是私人频道)

def resolve_channel_id(self, name):
    try:
        if name in self.__channel_ids:
            return self.__channel_ids[name]

        channel_id = self.client(ResolveUsernameRequest(name))
        self.__channel_ids[name] = channel_id
        return channel_id

    # except UserNameNotOccupiedError as err:
    except Exception as err:
        # try for private channel
        chatinvite = self.client(CheckChatInviteRequest(name))
        channel_id = chatinvite.chat.id
        access_hash_channel = chatinvite.chat.access_hash
        channel_id = InputChannel(channel_id, access_hash_channel)
        self.__channel_ids[name] = channel_id
        return channel_id

2 个答案:

答案 0 :(得分:0)

通过更新至最新的telethon:1.0.3和以下文档进行工作... https://telethon.readthedocs.io/en/latest/extra/basic/asyncio-magic.html

答案 1 :(得分:0)

我希望这个例子对朋友有用:‌

通过此代码,如果将包含Hello文本的消息发送到ID为-1001300989487的网上论坛,则该文本将被删除。

import logging


from telethon import TelegramClient, events

logging.basicConfig(level=logging.INFO)

api_id = XXXXXX
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXX'
phone_number = '+989XXXXXXXX'

################################################

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


@client.on(events.NewMessage(chats=(-1001300989487)))
async def my_event_handler(event):
    if 'hello' in event.raw_text:
        await event.delete()


client.start()
client.run_until_disconnected()

print('finish')