import pyrogram import typing from .executors import MtProtoTask from .webhook import WebHookDataForward from .reformat import message_to_bot_api from pyrogram.api.types import ChannelMessagesFilterEmpty from pyrogram.api.types.updates import ChannelDifference, ChannelDifferenceTooLong, ChannelDifferenceEmpty from pyrogram.api.functions.updates.get_channel_difference import GetChannelDifference class ChannelHistoryReadTask(MtProtoTask): _channel: typing.Union[pyrogram.Chat, str] _client: pyrogram.Client _pts: int _webhook: str def __init__(self, client: pyrogram.Client, channel: str, webhook: str): super().__init__() self._pts = False self._client = client self._channel = channel self._webhook = webhook async def setup(self): self._channel = await self._client.resolve_peer(self._channel) async def process(self) -> typing.Union[bool, int]: response = await self._client.send( GetChannelDifference( channel=self._channel, filter=ChannelMessagesFilterEmpty(), pts=self._pts if self._pts else 0xFFFFFFF, limit=0xFFFFFFF, force=True ) ) if isinstance(response, ChannelDifference): self._pts = response.pts users = {i.id: i for i in response.users} chats = {i.id: i for i in response.chats} for message in response.new_messages: data = await message_to_bot_api(self._client, users, chats, message) forwarder = WebHookDataForward(self._webhook, data) await self.future(forwarder) if not response.final: return 1 return response.timeout if isinstance(response, ChannelDifferenceEmpty): self._pts = response.pts return response.timeout if isinstance(response, ChannelDifferenceTooLong): return False