2018-04-06 16:36:29 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
2019-01-01 11:36:16 +00:00
|
|
|
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
|
2018-04-06 16:36:29 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
from collections import OrderedDict
|
|
|
|
from queue import Queue
|
2019-06-20 23:53:17 +00:00
|
|
|
from threading import Thread, Lock
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-12-17 15:40:06 +00:00
|
|
|
import pyrogram
|
2019-09-07 13:56:37 +00:00
|
|
|
from pyrogram.api.types import (
|
|
|
|
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
|
|
|
|
UpdateEditMessage, UpdateEditChannelMessage,
|
|
|
|
UpdateDeleteMessages, UpdateDeleteChannelMessages,
|
|
|
|
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
|
|
|
|
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll
|
|
|
|
)
|
2019-06-08 13:28:03 +00:00
|
|
|
from . import utils
|
2018-11-09 12:08:28 +00:00
|
|
|
from ..handlers import (
|
|
|
|
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
|
2019-04-14 18:50:13 +00:00
|
|
|
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
|
2018-11-09 12:08:28 +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
|
|
|
)
|
|
|
|
|
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-10-18 19:18:22 +00:00
|
|
|
|
2018-04-13 17:09:00 +00:00
|
|
|
self.workers_list = []
|
2019-06-21 22:45:49 +00:00
|
|
|
self.locks_list = []
|
|
|
|
|
2018-11-13 19:31:53 +00:00
|
|
|
self.updates_queue = Queue()
|
2018-04-10 12:52:31 +00:00
|
|
|
self.groups = OrderedDict()
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-11-13 19:31:53 +00:00
|
|
|
self.update_parsers = {
|
2018-10-19 09:54:27 +00:00
|
|
|
Dispatcher.MESSAGE_UPDATES:
|
2019-09-07 13:56:37 +00:00
|
|
|
lambda upd, usr, cht: (
|
|
|
|
pyrogram.Message._parse(
|
|
|
|
self.client,
|
|
|
|
upd.message,
|
|
|
|
usr,
|
|
|
|
cht,
|
|
|
|
isinstance(upd, UpdateNewScheduledMessage)
|
|
|
|
),
|
|
|
|
MessageHandler
|
|
|
|
),
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2018-12-17 15:40:06 +00:00
|
|
|
Dispatcher.DELETE_MESSAGES_UPDATES:
|
2019-06-08 13:13:52 +00:00
|
|
|
lambda upd, usr, cht: (utils.parse_deleted_messages(self.client, upd), DeletedMessagesHandler),
|
2018-10-19 09:54:27 +00:00
|
|
|
|
|
|
|
Dispatcher.CALLBACK_QUERY_UPDATES:
|
2018-12-19 12:00:33 +00:00
|
|
|
lambda upd, usr, cht: (pyrogram.CallbackQuery._parse(self.client, upd, usr), CallbackQueryHandler),
|
2018-10-19 09:54:27 +00:00
|
|
|
|
2019-09-07 13:56:37 +00:00
|
|
|
(UpdateUserStatus,):
|
2019-07-10 23:35:02 +00:00
|
|
|
lambda upd, usr, cht: (pyrogram.User._parse_user_status(self.client, upd), UserStatusHandler),
|
2018-11-09 12:08:28 +00:00
|
|
|
|
2019-09-07 13:56:37 +00:00
|
|
|
(UpdateBotInlineQuery,):
|
2019-04-14 18:50:13 +00:00
|
|
|
lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler),
|
|
|
|
|
2019-09-07 13:56:37 +00:00
|
|
|
(UpdateMessagePoll,):
|
2019-05-05 13:44:53 +00:00
|
|
|
lambda upd, usr, cht: (pyrogram.Poll._parse_update(self.client, upd), PollHandler)
|
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-04-06 16:36:29 +00:00
|
|
|
def start(self):
|
|
|
|
for i in range(self.workers):
|
2019-06-21 22:45:49 +00:00
|
|
|
self.locks_list.append(Lock())
|
|
|
|
|
2018-04-13 17:09:00 +00:00
|
|
|
self.workers_list.append(
|
|
|
|
Thread(
|
|
|
|
target=self.update_worker,
|
2019-06-21 22:45:49 +00:00
|
|
|
name="UpdateWorker#{}".format(i + 1),
|
|
|
|
args=(self.locks_list[-1],)
|
2018-04-13 17:09:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.workers_list[-1].start()
|
2018-04-06 16:36:29 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
for _ in range(self.workers):
|
2018-11-13 19:31:53 +00:00
|
|
|
self.updates_queue.put(None)
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2018-10-18 19:18:22 +00:00
|
|
|
for worker in self.workers_list:
|
|
|
|
worker.join()
|
2018-04-13 17:09:00 +00:00
|
|
|
|
2018-04-29 18:20:34 +00:00
|
|
|
self.workers_list.clear()
|
2019-06-21 22:45:49 +00:00
|
|
|
self.locks_list.clear()
|
2019-06-11 16:31:38 +00:00
|
|
|
self.groups.clear()
|
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 22:45:49 +00:00
|
|
|
for lock in self.locks_list:
|
|
|
|
lock.acquire()
|
|
|
|
|
|
|
|
try:
|
2019-06-20 23:53: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
|
|
|
|
2019-06-20 23:53:17 +00:00
|
|
|
self.groups[group].append(handler)
|
2019-06-21 22:45:49 +00:00
|
|
|
finally:
|
|
|
|
for lock in self.locks_list:
|
|
|
|
lock.release()
|
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 22:45:49 +00:00
|
|
|
for lock in self.locks_list:
|
|
|
|
lock.acquire()
|
|
|
|
|
|
|
|
try:
|
2019-06-20 23:53:17 +00:00
|
|
|
if group not in self.groups:
|
|
|
|
raise ValueError("Group {} does not exist. Handler was not removed.".format(group))
|
2018-10-18 19:18:22 +00:00
|
|
|
|
2019-06-20 23:53:17 +00:00
|
|
|
self.groups[group].remove(handler)
|
2019-06-21 22:45:49 +00:00
|
|
|
finally:
|
|
|
|
for lock in self.locks_list:
|
|
|
|
lock.release()
|
2018-04-09 21:35:51 +00:00
|
|
|
|
2019-06-21 22:45:49 +00:00
|
|
|
def update_worker(self, lock):
|
2018-04-06 16:36:29 +00:00
|
|
|
name = threading.current_thread().name
|
2019-09-08 17:24:06 +00:00
|
|
|
log.debug("{} started".format(name))
|
2018-04-06 16:36:29 +00:00
|
|
|
|
|
|
|
while True:
|
2019-06-01 11:18:48 +00:00
|
|
|
packet = 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-10-17 18:37:53 +00:00
|
|
|
|
2019-01-30 16:16:50 +00:00
|
|
|
parsed_update, handler_type = (
|
|
|
|
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:45:49 +00:00
|
|
|
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:
|
|
|
|
if handler.check(parsed_update):
|
|
|
|
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:
|
|
|
|
handler.callback(self.client, *args)
|
|
|
|
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)
|
2018-04-06 16:36:29 +00:00
|
|
|
|
2019-09-08 17:24:06 +00:00
|
|
|
log.debug("{} stopped".format(name))
|