2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
2021-01-01 21:58:48 +00:00
|
|
|
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
|
2018-04-06 16:36:29 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# This file is part of Pyrogram.
|
2018-04-06 16:36:29 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2018-04-06 16:36:29 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
2018-04-06 16:36:29 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-06-17 16:41:07 +00:00
|
|
|
import asyncio
|
2020-08-25 07:32:39 +00:00
|
|
|
import inspect
|
2018-04-06 16:36:29 +00:00
|
|
|
import logging
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-12-17 15:40:06 +00:00
|
|
|
import pyrogram
|
2020-08-22 06:05:05 +00:00
|
|
|
from pyrogram import utils
|
|
|
|
from pyrogram.handlers import (
|
|
|
|
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
|
|
|
|
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
|
2021-03-17 14:11:23 +00:00
|
|
|
ChosenInlineResultHandler, ChatMemberUpdatedHandler
|
2020-08-22 06:05:05 +00:00
|
|
|
)
|
|
|
|
from pyrogram.raw.types import (
|
2019-09-07 13:56:37 +00:00
|
|
|
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
|
|
|
|
UpdateEditMessage, UpdateEditChannelMessage,
|
|
|
|
UpdateDeleteMessages, UpdateDeleteChannelMessages,
|
|
|
|
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
|
2020-04-03 15:15:28 +00:00
|
|
|
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
|
2021-03-17 14:11:23 +00:00
|
|
|
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant
|
2019-09-07 13:56:37 +00:00
|
|
|
)
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2019-09-08 17:24:06 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2018-04-06 16:36:29 +00:00
|
|
|
|
|
|
|
class Dispatcher:
|
2018-04-28 21:48:38 +00:00
|
|
|
NEW_MESSAGE_UPDATES = (
|
2019-09-07 13:56:37 +00:00
|
|
|
UpdateNewMessage,
|
|
|
|
UpdateNewChannelMessage,
|
|
|
|
UpdateNewScheduledMessage
|
2018-04-08 10:43:47 +00:00
|
|
|
)
|
|
|
|
|
2018-04-28 21:48:38 +00:00
|
|
|
EDIT_MESSAGE_UPDATES = (
|
2019-09-07 13:56:37 +00:00
|
|
|
UpdateEditMessage,
|
|
|
|
UpdateEditChannelMessage,
|
2018-04-08 10:43:47 +00:00
|
|
|
)
|
|
|
|
|
2018-12-17 15:40:06 +00:00
|
|
|
DELETE_MESSAGES_UPDATES = (
|
2019-09-07 13:56:37 +00:00
|
|
|
UpdateDeleteMessages,
|
|
|
|
UpdateDeleteChannelMessages
|
2018-06-19 14:18:12 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 09:54:27 +00:00
|
|
|
CALLBACK_QUERY_UPDATES = (
|
2019-09-07 13:56:37 +00:00
|
|
|
UpdateBotCallbackQuery,
|
|
|
|
UpdateInlineBotCallbackQuery
|
2018-10-19 09:54:27 +00:00
|
|
|
)
|
|
|
|
|
2021-03-17 14:11:23 +00:00
|
|
|
CHAT_MEMBER_UPDATES = (
|
|
|
|
UpdateChatParticipant,
|
|
|
|
UpdateChannelParticipant
|
|
|
|
)
|
|
|
|
|
2018-04-28 21:48:38 +00:00
|
|
|
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
|
2018-04-08 10:43:47 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
def __init__(self, client: "pyrogram.Client"):
|
2018-04-06 16:36:29 +00:00
|
|
|
self.client = client
|
2020-08-22 06:05:05 +00:00
|
|
|
self.loop = asyncio.get_event_loop()
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
self.handler_worker_tasks = []
|
2019-06-21 22:45:49 +00:00
|
|
|
self.locks_list = []
|
|
|
|
|
2018-11-13 19:36:04 +00:00
|
|
|
self.updates_queue = asyncio.Queue()
|
2018-06-17 16:41:07 +00:00
|
|
|
self.groups = OrderedDict()
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-11-09 09:10:26 +00:00
|
|
|
async def message_parser(update, users, chats):
|
2020-08-22 06:05:05 +00:00
|
|
|
return await pyrogram.types.Message._parse(
|
2019-09-07 14:01:31 +00:00
|
|
|
self.client, update.message, users, chats,
|
|
|
|
isinstance(update, UpdateNewScheduledMessage)
|
|
|
|
), MessageHandler
|
2018-11-09 09:10:26 +00:00
|
|
|
|
|
|
|
async def deleted_messages_parser(update, users, chats):
|
2019-06-20 02:17:24 +00:00
|
|
|
return utils.parse_deleted_messages(self.client, update), DeletedMessagesHandler
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2018-11-09 09:10:26 +00:00
|
|
|
async def callback_query_parser(update, users, chats):
|
2020-08-22 06:05:05 +00:00
|
|
|
return await pyrogram.types.CallbackQuery._parse(self.client, update, users), CallbackQueryHandler
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2018-11-09 09:10:26 +00:00
|
|
|
async def user_status_parser(update, users, chats):
|
2020-08-22 06:05:05 +00:00
|
|
|
return pyrogram.types.User._parse_user_status(self.client, update), UserStatusHandler
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2019-03-22 12:36:27 +00:00
|
|
|
async def inline_query_parser(update, users, chats):
|
2020-08-22 06:05:05 +00:00
|
|
|
return pyrogram.types.InlineQuery._parse(self.client, update, users), InlineQueryHandler
|
2018-11-09 12:08:28 +00:00
|
|
|
|
2019-05-06 15:27:21 +00:00
|
|
|
async def poll_parser(update, users, chats):
|
2020-08-22 06:05:05 +00:00
|
|
|
return pyrogram.types.Poll._parse_update(self.client, update), PollHandler
|
2019-04-14 18:50:13 +00:00
|
|
|
|
2020-04-03 15:19:06 +00:00
|
|
|
async def chosen_inline_result_parser(update, users, chats):
|
2020-08-22 06:05:05 +00:00
|
|
|
return pyrogram.types.ChosenInlineResult._parse(self.client, update, users), ChosenInlineResultHandler
|
2020-04-03 15:19:06 +00:00
|
|
|
|
2021-03-17 14:11:23 +00:00
|
|
|
async def chat_member_updated_parser(update, users, chats):
|
|
|
|
return pyrogram.types.ChatMemberUpdated._parse(self.client, update, users, chats), ChatMemberUpdatedHandler
|
|
|
|
|
2018-11-13 19:36:04 +00:00
|
|
|
self.update_parsers = {
|
2018-11-09 09:10:26 +00:00
|
|
|
Dispatcher.MESSAGE_UPDATES: message_parser,
|
2018-12-22 13:08:29 +00:00
|
|
|
Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser,
|
2018-11-09 09:10:26 +00:00
|
|
|
Dispatcher.CALLBACK_QUERY_UPDATES: callback_query_parser,
|
2019-09-07 14:01:31 +00:00
|
|
|
(UpdateUserStatus,): user_status_parser,
|
|
|
|
(UpdateBotInlineQuery,): inline_query_parser,
|
2020-04-03 15:19:06 +00:00
|
|
|
(UpdateMessagePoll,): poll_parser,
|
2021-03-17 14:11:23 +00:00
|
|
|
(UpdateBotInlineSend,): chosen_inline_result_parser,
|
|
|
|
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser
|
2018-10-19 09:54:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 19:31:53 +00:00
|
|
|
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2018-06-17 16:41:07 +00:00
|
|
|
async def start(self):
|
2020-08-22 06:05:05 +00:00
|
|
|
if not self.client.no_updates:
|
|
|
|
for i in range(self.client.workers):
|
|
|
|
self.locks_list.append(asyncio.Lock())
|
2019-06-21 22:45:49 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
self.handler_worker_tasks.append(
|
2020-08-22 12:05:54 +00:00
|
|
|
self.loop.create_task(self.handler_worker(self.locks_list[-1]))
|
2020-08-22 06:05:05 +00:00
|
|
|
)
|
2018-06-18 11:06:07 +00:00
|
|
|
|
2020-08-29 13:46:39 +00:00
|
|
|
log.info(f"Started {self.client.workers} HandlerTasks")
|
2018-04-13 17:09:00 +00:00
|
|
|
|
2018-06-17 16:41:07 +00:00
|
|
|
async def stop(self):
|
2020-08-22 06:05:05 +00:00
|
|
|
if not self.client.no_updates:
|
|
|
|
for i in range(self.client.workers):
|
|
|
|
self.updates_queue.put_nowait(None)
|
2018-06-18 11:06:07 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
for i in self.handler_worker_tasks:
|
|
|
|
await i
|
2018-06-18 11:06:07 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
self.handler_worker_tasks.clear()
|
|
|
|
self.groups.clear()
|
2018-06-18 11:06:07 +00:00
|
|
|
|
2020-08-29 13:46:39 +00:00
|
|
|
log.info(f"Stopped {self.client.workers} HandlerTasks")
|
2018-04-29 18:20:34 +00:00
|
|
|
|
2018-04-11 01:16:48 +00:00
|
|
|
def add_handler(self, handler, group: int):
|
2019-06-21 19:48:35 +00:00
|
|
|
async def fn():
|
2019-06-21 22:49:13 +00:00
|
|
|
for lock in self.locks_list:
|
|
|
|
await lock.acquire()
|
2019-06-21 22:45:49 +00:00
|
|
|
|
2019-06-21 22:49:13 +00:00
|
|
|
try:
|
2019-06-21 19:48:35 +00:00
|
|
|
if group not in self.groups:
|
|
|
|
self.groups[group] = []
|
|
|
|
self.groups = OrderedDict(sorted(self.groups.items()))
|
2018-04-10 12:52:31 +00:00
|
|
|
|
2019-06-21 19:48:35 +00:00
|
|
|
self.groups[group].append(handler)
|
2019-06-21 22:49:13 +00:00
|
|
|
finally:
|
|
|
|
for lock in self.locks_list:
|
|
|
|
lock.release()
|
2019-06-21 19:48:35 +00:00
|
|
|
|
2020-08-22 12:05:54 +00:00
|
|
|
self.loop.create_task(fn())
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-04-29 22:15:24 +00:00
|
|
|
def remove_handler(self, handler, group: int):
|
2019-06-21 19:48:35 +00:00
|
|
|
async def fn():
|
2019-06-21 22:49:13 +00:00
|
|
|
for lock in self.locks_list:
|
|
|
|
await lock.acquire()
|
2019-06-21 22:45:49 +00:00
|
|
|
|
2019-06-21 22:49:13 +00:00
|
|
|
try:
|
2019-06-21 19:48:35 +00:00
|
|
|
if group not in self.groups:
|
2020-08-22 06:05:05 +00:00
|
|
|
raise ValueError(f"Group {group} does not exist. Handler was not removed.")
|
2019-06-21 19:48:35 +00:00
|
|
|
|
|
|
|
self.groups[group].remove(handler)
|
2019-06-21 22:49:13 +00:00
|
|
|
finally:
|
|
|
|
for lock in self.locks_list:
|
|
|
|
lock.release()
|
2018-04-28 21:48:38 +00:00
|
|
|
|
2020-08-22 12:05:54 +00:00
|
|
|
self.loop.create_task(fn())
|
2018-06-17 16:41:07 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
async def handler_worker(self, lock):
|
2018-04-06 16:36:29 +00:00
|
|
|
while True:
|
2019-06-02 17:13:17 +00:00
|
|
|
packet = await self.updates_queue.get()
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2019-06-01 11:18:48 +00:00
|
|
|
if packet is None:
|
2018-04-06 16:36:29 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
2019-06-01 11:18:48 +00:00
|
|
|
update, users, chats = packet
|
2018-11-13 19:31:53 +00:00
|
|
|
parser = self.update_parsers.get(type(update), None)
|
2018-06-19 14:18:12 +00:00
|
|
|
|
2019-01-30 16:16:50 +00:00
|
|
|
parsed_update, handler_type = (
|
2019-02-04 11:59:20 +00:00
|
|
|
await parser(update, users, chats)
|
2019-02-04 10:46:57 +00:00
|
|
|
if parser is not None
|
|
|
|
else (None, type(None))
|
2019-01-30 16:16:50 +00:00
|
|
|
)
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2019-06-21 22:49:13 +00:00
|
|
|
async with lock:
|
2019-06-20 23:53:17 +00:00
|
|
|
for group in self.groups.values():
|
|
|
|
for handler in group:
|
|
|
|
args = None
|
|
|
|
|
|
|
|
if isinstance(handler, handler_type):
|
2019-07-29 11:31:07 +00:00
|
|
|
try:
|
2020-08-22 06:05:05 +00:00
|
|
|
if await handler.check(self.client, parsed_update):
|
2019-07-29 11:31:07 +00:00
|
|
|
args = (parsed_update,)
|
|
|
|
except Exception as e:
|
2019-09-08 17:24:06 +00:00
|
|
|
log.error(e, exc_info=True)
|
2019-07-29 11:31:07 +00:00
|
|
|
continue
|
|
|
|
|
2019-06-20 23:53:17 +00:00
|
|
|
elif isinstance(handler, RawUpdateHandler):
|
|
|
|
args = (update, users, chats)
|
|
|
|
|
|
|
|
if args is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2020-08-25 07:32:39 +00:00
|
|
|
if inspect.iscoroutinefunction(handler.callback):
|
2020-08-22 06:05:05 +00:00
|
|
|
await handler.callback(self.client, *args)
|
|
|
|
else:
|
|
|
|
await self.loop.run_in_executor(
|
2021-04-07 11:57:59 +00:00
|
|
|
self.client.handler_executor,
|
2020-08-22 06:05:05 +00:00
|
|
|
handler.callback,
|
|
|
|
self.client,
|
|
|
|
*args
|
|
|
|
)
|
2019-06-20 23:53:17 +00:00
|
|
|
except pyrogram.StopPropagation:
|
|
|
|
raise
|
|
|
|
except pyrogram.ContinuePropagation:
|
|
|
|
continue
|
|
|
|
except Exception as e:
|
2019-09-08 17:24:06 +00:00
|
|
|
log.error(e, exc_info=True)
|
2019-06-20 23:53:17 +00:00
|
|
|
|
|
|
|
break
|
2019-02-04 10:46:57 +00:00
|
|
|
except pyrogram.StopPropagation:
|
|
|
|
pass
|
2018-04-06 16:36:29 +00:00
|
|
|
except Exception as e:
|
2019-09-08 17:24:06 +00:00
|
|
|
log.error(e, exc_info=True)
|