2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Merge develop -> asyncio

This commit is contained in:
Dan 2019-06-21 02:04:16 +02:00
commit 471a124d2c
3 changed files with 49 additions and 45 deletions

View File

@ -81,13 +81,13 @@ CallbackQuery
^^^^^^^^^^^^^ ^^^^^^^^^^^^^
.. hlist:: .. hlist::
:columns: 4 :columns: 3
- :meth:`~CallbackQuery.answer` - :meth:`~CallbackQuery.answer`
- :meth:`~CallbackQuery.edit_text` - :meth:`~CallbackQuery.edit_message_text`
- :meth:`~CallbackQuery.edit_caption` - :meth:`~CallbackQuery.edit_message_caption`
- :meth:`~CallbackQuery.edit_media` - :meth:`~CallbackQuery.edit_message_media`
- :meth:`~CallbackQuery.edit_reply_markup` - :meth:`~CallbackQuery.edit_message_reply_markup`
InlineQuery InlineQuery
^^^^^^^^^^^ ^^^^^^^^^^^
@ -141,10 +141,10 @@ Details
.. CallbackQuery .. CallbackQuery
.. automethod:: CallbackQuery.answer() .. automethod:: CallbackQuery.answer()
.. automethod:: CallbackQuery.edit_text() .. automethod:: CallbackQuery.edit_message_text()
.. automethod:: CallbackQuery.edit_caption() .. automethod:: CallbackQuery.edit_message_caption()
.. automethod:: CallbackQuery.edit_media() .. automethod:: CallbackQuery.edit_message_media()
.. automethod:: CallbackQuery.edit_reply_markup() .. automethod:: CallbackQuery.edit_message_reply_markup()
.. InlineQuery .. InlineQuery
.. automethod:: InlineQuery.answer() .. automethod:: InlineQuery.answer()

View File

@ -22,7 +22,6 @@ from collections import OrderedDict
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from . import utils from . import utils
from ..handlers import ( from ..handlers import (
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
@ -63,6 +62,8 @@ class Dispatcher:
self.updates_queue = asyncio.Queue() self.updates_queue = asyncio.Queue()
self.groups = OrderedDict() self.groups = OrderedDict()
self.lock = asyncio.Lock()
async def message_parser(update, users, chats): async def message_parser(update, users, chats):
return await pyrogram.Message._parse(self.client, update.message, users, chats), MessageHandler return await pyrogram.Message._parse(self.client, update.message, users, chats), MessageHandler
@ -113,17 +114,19 @@ class Dispatcher:
log.info("Stopped {} UpdateWorkerTasks".format(self.workers)) log.info("Stopped {} UpdateWorkerTasks".format(self.workers))
def add_handler(self, handler, group: int): def add_handler(self, handler, group: int):
if group not in self.groups: with self.lock:
self.groups[group] = [] if group not in self.groups:
self.groups = OrderedDict(sorted(self.groups.items())) self.groups[group] = []
self.groups = OrderedDict(sorted(self.groups.items()))
self.groups[group].append(handler) self.groups[group].append(handler)
def remove_handler(self, handler, group: int): def remove_handler(self, handler, group: int):
if group not in self.groups: with self.lock:
raise ValueError("Group {} does not exist. Handler was not removed.".format(group)) if group not in self.groups:
raise ValueError("Group {} does not exist. Handler was not removed.".format(group))
self.groups[group].remove(handler) self.groups[group].remove(handler)
async def update_worker(self): async def update_worker(self):
while True: while True:
@ -142,29 +145,30 @@ class Dispatcher:
else (None, type(None)) else (None, type(None))
) )
for group in self.groups.values(): with self.lock:
for handler in group: for group in self.groups.values():
args = None for handler in group:
args = None
if isinstance(handler, handler_type): if isinstance(handler, handler_type):
if handler.check(parsed_update): if handler.check(parsed_update):
args = (parsed_update,) args = (parsed_update,)
elif isinstance(handler, RawUpdateHandler): elif isinstance(handler, RawUpdateHandler):
args = (update, users, chats) args = (update, users, chats)
if args is None: if args is None:
continue continue
try: try:
await handler.callback(self.client, *args) await handler.callback(self.client, *args)
except pyrogram.StopPropagation: except pyrogram.StopPropagation:
raise raise
except pyrogram.ContinuePropagation: except pyrogram.ContinuePropagation:
continue continue
except Exception as e: except Exception as e:
log.error(e, exc_info=True) log.error(e, exc_info=True)
break break
except pyrogram.StopPropagation: except pyrogram.StopPropagation:
pass pass
except Exception as e: except Exception as e:

View File

@ -173,14 +173,14 @@ class CallbackQuery(Object, Update):
cache_time=cache_time cache_time=cache_time
) )
def edit_text( def edit_message_text(
self, self,
text: str, text: str,
parse_mode: str = "", parse_mode: str = "",
disable_web_page_preview: bool = None, disable_web_page_preview: bool = None,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> Union["pyrogram.Message", bool]: ) -> Union["pyrogram.Message", bool]:
"""Edit the text of messages attached to this callback query. """Edit the text of messages attached to callback queries.
Bound method *edit_message_text* of :obj:`CallbackQuery`. Bound method *edit_message_text* of :obj:`CallbackQuery`.
@ -223,13 +223,13 @@ class CallbackQuery(Object, Update):
reply_markup=reply_markup reply_markup=reply_markup
) )
def edit_caption( def edit_message_caption(
self, self,
caption: str, caption: str,
parse_mode: str = "", parse_mode: str = "",
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> Union["pyrogram.Message", bool]: ) -> Union["pyrogram.Message", bool]:
"""Edit the caption of media messages attached to this callback query. """Edit the caption of media messages attached to callback queries.
Bound method *edit_message_caption* of :obj:`CallbackQuery`. Bound method *edit_message_caption* of :obj:`CallbackQuery`.
@ -251,14 +251,14 @@ class CallbackQuery(Object, Update):
Raises: Raises:
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
""" """
return self.edit_text(caption, parse_mode, reply_markup) return self.edit_message_text(caption, parse_mode, reply_markup)
def edit_media( def edit_message_media(
self, self,
media: "pyrogram.InputMedia", media: "pyrogram.InputMedia",
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> Union["pyrogram.Message", bool]: ) -> Union["pyrogram.Message", bool]:
"""Edit animation, audio, document, photo or video messages attached to this callback query. """Edit animation, audio, document, photo or video messages attached to callback queries.
Bound method *edit_message_media* of :obj:`CallbackQuery`. Bound method *edit_message_media* of :obj:`CallbackQuery`.
@ -290,11 +290,11 @@ class CallbackQuery(Object, Update):
reply_markup=reply_markup reply_markup=reply_markup
) )
def edit_reply_markup( def edit_message_reply_markup(
self, self,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> Union["pyrogram.Message", bool]: ) -> Union["pyrogram.Message", bool]:
"""Edit only the reply markup of messages attached to this callback query. """Edit only the reply markup of messages attached to callback queries.
Bound method *edit_message_reply_markup* of :obj:`CallbackQuery`. Bound method *edit_message_reply_markup* of :obj:`CallbackQuery`.