outside-fetcher/tasks/channel_history.py

59 lines
1.8 KiB
Python
Raw Normal View History

2019-11-03 22:11:45 +01:00
import pyrogram
import aiohttp
from async_worker.async_worker import AsyncTask
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
from pyrogram.client.types.messages_and_media import Message as MessagePyrogram
class ChannelHistoryReadTask(AsyncTask):
channel: pyrogram.Chat
client: pyrogram.Client
pts: int
webhook: str
def setup(self, client: pyrogram.Client, channel: pyrogram.Chat, webhook: str):
self.client = client
self.channel = channel
self.pts = False
self.webhook = webhook
async def process(self):
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}
http = aiohttp.ClientSession()
for message in response.new_messages:
message = await MessagePyrogram._parse(self.client, message, users, chats)
await http.post(self.webhook, data=bytes(str(message), "utf8"))
await http.close()
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