2018-04-06 16:36:29 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
|
|
|
|
#
|
|
|
|
# This file is part of Pyrogram.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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-06-17 16:41:07 +00:00
|
|
|
import asyncio
|
2018-04-06 16:36:29 +00:00
|
|
|
import logging
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from pyrogram.api import types
|
2018-05-07 12:30:55 +00:00
|
|
|
from ..ext import utils
|
2018-10-17 18:37:53 +00:00
|
|
|
from ..handlers import CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler
|
2018-04-06 16:36:29 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Dispatcher:
|
2018-04-28 21:48:38 +00:00
|
|
|
NEW_MESSAGE_UPDATES = (
|
2018-04-08 10:43:47 +00:00
|
|
|
types.UpdateNewMessage,
|
|
|
|
types.UpdateNewChannelMessage
|
|
|
|
)
|
|
|
|
|
2018-04-28 21:48:38 +00:00
|
|
|
EDIT_MESSAGE_UPDATES = (
|
2018-04-08 10:43:47 +00:00
|
|
|
types.UpdateEditMessage,
|
|
|
|
types.UpdateEditChannelMessage
|
|
|
|
)
|
|
|
|
|
2018-06-19 14:18:12 +00:00
|
|
|
DELETE_MESSAGE_UPDATES = (
|
|
|
|
types.UpdateDeleteMessages,
|
|
|
|
types.UpdateDeleteChannelMessages
|
|
|
|
)
|
|
|
|
|
2018-10-19 09:54:27 +00:00
|
|
|
CALLBACK_QUERY_UPDATES = (
|
|
|
|
types.UpdateBotCallbackQuery,
|
|
|
|
types.UpdateInlineBotCallbackQuery
|
|
|
|
)
|
|
|
|
|
2018-04-28 21:48:38 +00:00
|
|
|
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
|
2018-04-08 10:43:47 +00:00
|
|
|
|
2018-10-19 09:54:27 +00:00
|
|
|
def __init__(self, client, workers: int):
|
2018-04-06 16:36:29 +00:00
|
|
|
self.client = client
|
|
|
|
self.workers = workers
|
|
|
|
|
2018-06-18 11:06:07 +00:00
|
|
|
self.update_worker_tasks = []
|
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):
|
|
|
|
return await utils.parse_messages(self.client, update.message, users, chats), MessageHandler
|
|
|
|
|
|
|
|
async def deleted_messages_parser(update, users, chats):
|
|
|
|
return utils.parse_deleted_messages(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):
|
|
|
|
return await utils.parse_callback_query(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):
|
|
|
|
return utils.parse_user_status(update.status, update.user_id), UserStatusHandler
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2018-11-13 19:36:04 +00:00
|
|
|
self.update_parsers = {
|
2018-11-09 09:10:26 +00:00
|
|
|
Dispatcher.MESSAGE_UPDATES: message_parser,
|
|
|
|
Dispatcher.DELETE_MESSAGE_UPDATES: deleted_messages_parser,
|
|
|
|
Dispatcher.CALLBACK_QUERY_UPDATES: callback_query_parser,
|
|
|
|
(types.UpdateUserStatus,): user_status_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):
|
2018-06-18 11:06:07 +00:00
|
|
|
for i in range(self.workers):
|
|
|
|
self.update_worker_tasks.append(
|
|
|
|
asyncio.ensure_future(self.update_worker())
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info("Started {} UpdateWorkerTasks".format(self.workers))
|
2018-04-13 17:09:00 +00:00
|
|
|
|
2018-06-17 16:41:07 +00:00
|
|
|
async def stop(self):
|
2018-06-18 11:06:07 +00:00
|
|
|
for i in range(self.workers):
|
2018-11-13 19:36:04 +00:00
|
|
|
self.updates_queue.put_nowait(None)
|
2018-06-18 11:06:07 +00:00
|
|
|
|
|
|
|
for i in self.update_worker_tasks:
|
|
|
|
await i
|
|
|
|
|
|
|
|
self.update_worker_tasks.clear()
|
|
|
|
|
|
|
|
log.info("Stopped {} UpdateWorkerTasks".format(self.workers))
|
2018-04-29 18:20:34 +00:00
|
|
|
|
2018-04-11 01:16:48 +00:00
|
|
|
def add_handler(self, handler, group: int):
|
2018-05-26 16:04:17 +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
|
|
|
|
2018-05-26 16:04:17 +00:00
|
|
|
self.groups[group].append(handler)
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-04-29 22:15:24 +00:00
|
|
|
def remove_handler(self, handler, group: int):
|
2018-05-26 16:04:17 +00:00
|
|
|
if group not in self.groups:
|
2018-10-18 19:18:22 +00:00
|
|
|
raise ValueError("Group {} does not exist. Handler was not removed.".format(group))
|
2018-04-28 21:48:38 +00:00
|
|
|
|
2018-05-26 16:04:17 +00:00
|
|
|
self.groups[group].remove(handler)
|
2018-06-17 16:41:07 +00:00
|
|
|
|
2018-11-09 08:41:49 +00:00
|
|
|
async def update_worker(self):
|
2018-04-06 16:36:29 +00:00
|
|
|
while True:
|
2018-11-13 19:36:04 +00:00
|
|
|
update = await self.updates_queue.get()
|
2018-04-06 16:36:29 +00:00
|
|
|
|
|
|
|
if update is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
users = {i.id: i for i in update[1]}
|
|
|
|
chats = {i.id: i for i in update[2]}
|
|
|
|
update = update[0]
|
|
|
|
|
2018-11-13 19:31:53 +00:00
|
|
|
parser = self.update_parsers.get(type(update), None)
|
2018-06-19 14:18:12 +00:00
|
|
|
|
2018-10-19 09:54:27 +00:00
|
|
|
if parser is None:
|
2018-04-28 21:48:38 +00:00
|
|
|
continue
|
2018-10-17 18:37:53 +00:00
|
|
|
|
2018-11-09 09:10:26 +00:00
|
|
|
update, handler_type = await parser(update, users, chats)
|
2018-10-19 09:54:27 +00:00
|
|
|
|
|
|
|
for group in self.groups.values():
|
|
|
|
for handler in group:
|
|
|
|
args = None
|
|
|
|
|
|
|
|
if isinstance(handler, RawUpdateHandler):
|
|
|
|
args = (update, users, chats)
|
|
|
|
elif isinstance(handler, handler_type):
|
|
|
|
if handler.check(update):
|
|
|
|
args = (update,)
|
|
|
|
|
2018-11-09 08:21:01 +00:00
|
|
|
if args is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2018-11-09 09:10:26 +00:00
|
|
|
await handler.callback(self.client, *args)
|
2018-11-09 08:21:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.error(e, exc_info=True)
|
|
|
|
finally:
|
|
|
|
break
|
2018-04-06 16:36:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.error(e, exc_info=True)
|