diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 8c75d711..91bbad38 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -534,7 +534,6 @@ def pyrogram_api(): Chat.promote_member Chat.get_member Chat.get_members - Chat.iter_members Chat.add_members Chat.join Chat.leave diff --git a/docs/Makefile b/docs/Makefile index ceb7494c..8eacc12a 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -20,4 +20,6 @@ help: @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) lhtml: # live html - sphinx-autobuild --host $(shell ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2) -b html "$(SOURCEDIR)" "$(BUILDDIR)/html" $(SPHINXOPTS) + sphinx-autobuild --host $(shell ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2) \ + --watch ../pyrogram \ + -b html "$(SOURCEDIR)" "$(BUILDDIR)/html" $(SPHINXOPTS) diff --git a/docs/source/start/errors.rst b/docs/source/start/errors.rst index 5f59deef..01881074 100644 --- a/docs/source/start/errors.rst +++ b/docs/source/start/errors.rst @@ -89,7 +89,7 @@ Errors with Values Exception objects may also contain some informative values. For example, ``FloodWait`` holds the amount of seconds you have to wait before you can try again, some other errors contain the DC number on which the request must be repeated on. -The value is stored in the ``x`` attribute of the exception object: +The value is stored in the ``value`` attribute of the exception object: .. code-block:: python @@ -100,5 +100,5 @@ The value is stored in the ``x`` attribute of the exception object: try: ... # Your code except FloodWait as e: - await asyncio.sleep(e.x) # Wait "x" seconds before continuing + await asyncio.sleep(e.value) # Wait N seconds before continuing ... \ No newline at end of file diff --git a/docs/source/start/examples/bot_keyboards.rst b/docs/source/start/examples/bot_keyboards.rst index a3a549f4..b774a80e 100644 --- a/docs/source/start/examples/bot_keyboards.rst +++ b/docs/source/start/examples/bot_keyboards.rst @@ -12,51 +12,57 @@ like send_audio(), send_document(), send_location(), etc... .. code-block:: python from pyrogram import Client - from pyrogram.types import ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton + from pyrogram.types import (ReplyKeyboardMarkup, InlineKeyboardMarkup, + InlineKeyboardButton) # Create a client using your bot token app = Client("my_bot", bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") - with app: - app.send_message( - "me", # Edit this - "This is a ReplyKeyboardMarkup example", - reply_markup=ReplyKeyboardMarkup( - [ - ["A", "B", "C", "D"], # First row - ["E", "F", "G"], # Second row - ["H", "I"], # Third row - ["J"] # Fourth row - ], - resize_keyboard=True # Make the keyboard smaller - ) - ) - app.send_message( - "me", # Edit this - "This is a InlineKeyboardMarkup example", - reply_markup=InlineKeyboardMarkup( - [ - [ # First row - InlineKeyboardButton( # Generates a callback query when pressed - "Button", - callback_data="data" - ), - InlineKeyboardButton( # Opens a web URL - "URL", - url="https://docs.pyrogram.org" - ), + async def main(): + async with app: + await app.send_message( + "me", # Edit this + "This is a ReplyKeyboardMarkup example", + reply_markup=ReplyKeyboardMarkup( + [ + ["A", "B", "C", "D"], # First row + ["E", "F", "G"], # Second row + ["H", "I"], # Third row + ["J"] # Fourth row ], - [ # Second row - InlineKeyboardButton( # Opens the inline interface - "Choose chat", - switch_inline_query="pyrogram" - ), - InlineKeyboardButton( # Opens the inline interface in the current chat - "Inline here", - switch_inline_query_current_chat="pyrogram" - ) - ] - ] + resize_keyboard=True # Make the keyboard smaller + ) ) - ) + + await app.send_message( + "me", # Edit this + "This is a InlineKeyboardMarkup example", + reply_markup=InlineKeyboardMarkup( + [ + [ # First row + InlineKeyboardButton( # Generates a callback query when pressed + "Button", + callback_data="data" + ), + InlineKeyboardButton( # Opens a web URL + "URL", + url="https://docs.pyrogram.org" + ), + ], + [ # Second row + InlineKeyboardButton( # Opens the inline interface + "Choose chat", + switch_inline_query="pyrogram" + ), + InlineKeyboardButton( # Opens the inline interface in the current chat + "Inline here", + switch_inline_query_current_chat="pyrogram" + ) + ] + ] + ) + ) + + + app.run(main()) \ No newline at end of file diff --git a/docs/source/start/examples/callback_queries.rst b/docs/source/start/examples/callback_queries.rst index 73273058..64da57b3 100644 --- a/docs/source/start/examples/callback_queries.rst +++ b/docs/source/start/examples/callback_queries.rst @@ -12,8 +12,10 @@ It uses the @on_callback_query decorator to register a CallbackQueryHandler. @app.on_callback_query() - def answer(client, callback_query): - callback_query.answer(f"Button contains: '{callback_query.data}'", show_alert=True) + async def answer(client, callback_query): + await callback_query.answer( + f"Button contains: '{callback_query.data}'", + show_alert=True) app.run() # Automatically start() and idle() \ No newline at end of file diff --git a/docs/source/start/examples/echobot.rst b/docs/source/start/examples/echo_bot.rst similarity index 74% rename from docs/source/start/examples/echobot.rst rename to docs/source/start/examples/echo_bot.rst index 2ff578e9..de8288b5 100644 --- a/docs/source/start/examples/echobot.rst +++ b/docs/source/start/examples/echo_bot.rst @@ -1,5 +1,5 @@ -echobot -======= +echo_bot +======== This simple echo bot replies to every private text message. @@ -14,8 +14,8 @@ It uses the ``@on_message`` decorator to register a ``MessageHandler`` and appli @app.on_message(filters.text & filters.private) - def echo(client, message): - message.reply(message.text) + async def echo(client, message): + await message.reply(message.text) app.run() # Automatically start() and idle() \ No newline at end of file diff --git a/docs/source/start/examples/get_chat_history.rst b/docs/source/start/examples/get_chat_history.rst new file mode 100644 index 00000000..59939948 --- /dev/null +++ b/docs/source/start/examples/get_chat_history.rst @@ -0,0 +1,20 @@ +get_history +=========== + +This example shows how to get the full message history of a chat, starting from the latest message. + +.. code-block:: python + + from pyrogram import Client + + app = Client("my_account") + + + async def main(): + async with app: + # "me" refers to your own chat (Saved Messages) + async for message in app.get_chat_history("me"): + print(message) + + + app.run(main()) \ No newline at end of file diff --git a/docs/source/start/examples/get_chat_members.rst b/docs/source/start/examples/get_chat_members.rst index 8d477976..26636ca3 100644 --- a/docs/source/start/examples/get_chat_members.rst +++ b/docs/source/start/examples/get_chat_members.rst @@ -7,9 +7,16 @@ This example shows how to get all the members of a chat. from pyrogram import Client - app = Client("my_account") - target = "pyrogramchat" # Target channel/supergroup + # Target channel/supergroup + TARGET = -100123456789 - with app: - for member in app.iter_chat_members(target): - print(member.user.first_name) \ No newline at end of file + app = Client("my_account") + + + async def main(): + async with app: + async for member in app.get_chat_members(TARGET): + print(member) + + + app.run(main()) \ No newline at end of file diff --git a/docs/source/start/examples/get_dialogs.rst b/docs/source/start/examples/get_dialogs.rst index b1a48064..e5b10609 100644 --- a/docs/source/start/examples/get_dialogs.rst +++ b/docs/source/start/examples/get_dialogs.rst @@ -9,6 +9,11 @@ This example shows how to get the full dialogs list (as user). app = Client("my_account") - with app: - for dialog in app.iter_dialogs(): - print(dialog.chat.title or dialog.chat.first_name) \ No newline at end of file + + async def main(): + async with app: + async for dialog in app.get_dialogs(): + print(dialog.chat.title or dialog.chat.first_name) + + + app.run(main()) \ No newline at end of file diff --git a/docs/source/start/examples/get_history.rst b/docs/source/start/examples/get_history.rst deleted file mode 100644 index 01433d91..00000000 --- a/docs/source/start/examples/get_history.rst +++ /dev/null @@ -1,15 +0,0 @@ -get_history -=========== - -This example shows how to get the full message history of a chat, starting from the latest message. - -.. code-block:: python - - from pyrogram import Client - - app = Client("my_account") - target = "me" # "me" refers to your own chat (Saved Messages) - - with app: - for message in app.iter_history(target): - print(message.text) \ No newline at end of file diff --git a/docs/source/start/examples/hello_world.rst b/docs/source/start/examples/hello_world.rst index 997659e2..2902241e 100644 --- a/docs/source/start/examples/hello_world.rst +++ b/docs/source/start/examples/hello_world.rst @@ -10,6 +10,11 @@ This example demonstrates a basic API usage # Create a new Client instance app = Client("my_account") - with app: - # Send a message, Markdown is enabled by default - app.send_message("me", "Hi there! I'm using **Pyrogram**") + + async def main(): + async with app: + # Send a message, Markdown is enabled by default + await app.send_message("me", "Hi there! I'm using **Pyrogram**") + + + app.run(main()) diff --git a/docs/source/start/examples/index.rst b/docs/source/start/examples/index.rst index 7d8a69a4..d47b60e8 100644 --- a/docs/source/start/examples/index.rst +++ b/docs/source/start/examples/index.rst @@ -17,9 +17,9 @@ to give you a basic idea. :align: center :doc:`hello_world`, "Demonstration of basic API usage" - :doc:`echobot`, "Echo every private text message" - :doc:`welcomebot`, "The Welcome Bot in @PyrogramChat" - :doc:`get_history`, "Get the full message history of a chat" + :doc:`echo_bot`, "Echo every private text message" + :doc:`welcome_bot`, "The Welcome Bot in @PyrogramChat" + :doc:`get_chat_history`, "Get the full message history of a chat" :doc:`get_chat_members`, "Get all the members of a chat" :doc:`get_dialogs`, "Get all of your dialog chats" :doc:`callback_queries`, "Handle callback queries (as bot) coming from inline button presses" @@ -34,9 +34,9 @@ For more advanced examples, see https://snippets.pyrogram.org. :hidden: hello_world - echobot - welcomebot - get_history + echo_bot + welcome_bot + get_chat_history get_chat_members get_dialogs callback_queries diff --git a/docs/source/start/examples/inline_queries.rst b/docs/source/start/examples/inline_queries.rst index 09d226ef..b78c6e1c 100644 --- a/docs/source/start/examples/inline_queries.rst +++ b/docs/source/start/examples/inline_queries.rst @@ -16,8 +16,8 @@ It uses the @on_inline_query decorator to register an InlineQueryHandler. @app.on_inline_query() - def answer(client, inline_query): - inline_query.answer( + async def answer(client, inline_query): + await inline_query.answer( results=[ InlineQueryResultArticle( title="Installation", diff --git a/docs/source/start/examples/raw_updates.rst b/docs/source/start/examples/raw_updates.rst index 6086a968..463a45a8 100644 --- a/docs/source/start/examples/raw_updates.rst +++ b/docs/source/start/examples/raw_updates.rst @@ -11,7 +11,7 @@ This example shows how to handle raw updates. @app.on_raw_update() - def raw(client, update, users, chats): + async def raw(client, update, users, chats): print(update) diff --git a/docs/source/start/examples/use_inline_bots.rst b/docs/source/start/examples/use_inline_bots.rst index 63e46985..8a2a72ac 100644 --- a/docs/source/start/examples/use_inline_bots.rst +++ b/docs/source/start/examples/use_inline_bots.rst @@ -10,9 +10,16 @@ This example shows how to query an inline bot (as user). # Create a new Client app = Client("my_account") - with app: - # Get bot results for "hello" from the inline bot @vid - bot_results = app.get_inline_bot_results("vid", "hello") - # Send the first result (bot_results.results[0]) to your own chat (Saved Messages) - app.send_inline_bot_result("me", bot_results.query_id, bot_results.results[0].id) \ No newline at end of file + async def main(): + async with app: + # Get bot results for "hello" from the inline bot @vid + bot_results = await app.get_inline_bot_results("vid", "hello") + + # Send the first result to your own chat (Saved Messages) + await app.send_inline_bot_result( + "me", bot_results.query_id, + bot_results.results[0].id) + + + app.run(main()) \ No newline at end of file diff --git a/docs/source/start/examples/welcomebot.rst b/docs/source/start/examples/welcome_bot.rst similarity index 69% rename from docs/source/start/examples/welcomebot.rst rename to docs/source/start/examples/welcome_bot.rst index a7420590..4e30ea7f 100644 --- a/docs/source/start/examples/welcomebot.rst +++ b/docs/source/start/examples/welcome_bot.rst @@ -1,5 +1,5 @@ -welcomebot -========== +welcome_bot +=========== This example uses the ``emoji`` module to easily add emoji in your text messages and ``filters`` to make it only work for specific messages in a specific chat. @@ -8,24 +8,23 @@ to make it only work for specific messages in a specific chat. from pyrogram import Client, emoji, filters - TARGET = -100123456789 # Target chat. Can also be a list of multiple chat ids/usernames - MENTION = "[{}](tg://user?id={})" # User mention markup - MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.org/)'s group chat {}!" # Welcome message + # Target chat. Can also be a list of multiple chat ids/usernames + TARGET = -100123456789 + # Welcome message template + MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.org/)'s group chat {}!" app = Client("my_account") # Filter in only new_chat_members updates generated in TARGET chat @app.on_message(filters.chat(TARGET) & filters.new_chat_members) - def welcome(client, message): + async def welcome(client, message): # Build the new members list (with mentions) by using their first_name new_members = [u.mention for u in message.new_chat_members] - # Build the welcome message by using an emoji and the list we built above text = MESSAGE.format(emoji.SPARKLES, ", ".join(new_members)) - # Send the welcome message, without the web page preview - message.reply_text(text, disable_web_page_preview=True) + await message.reply_text(text, disable_web_page_preview=True) app.run() # Automatically start() and idle() \ No newline at end of file diff --git a/docs/source/start/invoking.rst b/docs/source/start/invoking.rst index de1e321e..ab91e730 100644 --- a/docs/source/start/invoking.rst +++ b/docs/source/start/invoking.rst @@ -1,5 +1,5 @@ -Calling Methods -=============== +Invoking Methods +================ At this point, we have successfully :doc:`installed Pyrogram <../intro/install>` and :doc:`authorized ` our account; we are now aiming towards the core of the framework. @@ -14,7 +14,7 @@ account; we are now aiming towards the core of the framework. Basic Usage ----------- -Making API method calls with Pyrogram is very simple. Here's a basic example we are going to examine step by step: +Making API calls with Pyrogram is very simple. Here's a basic example we are going to examine step by step: .. code-block:: python @@ -43,7 +43,7 @@ Step-by-step app = Client("my_account") -#. Async methods can't be executed at the top level, because they must be inside an async context. +#. Async methods must be invoked within an async context. Here we define an async function and put our code inside. Also notice the ``await`` keyword in front of the method call; this is required for all asynchronous methods. @@ -101,24 +101,4 @@ be instantiated inside the main function. async with app: await app.send_message("me", "Hi!") - asyncio.run(main()) - -Synchronous Calls ------------------- - -Pyrogram is an asynchronous framework, but it also provides a convenience way for calling methods without the need -of async/await keywords and the extra boilerplate. In case you want Pyrogram to run synchronously, simply use the -synchronous context manager: - -.. code-block:: python - - from pyrogram import Client - - app = Client("my_account") - - with app: - app.send_message("me", "Hi!") - -As you can see, the non-async example becomes less cluttered. Use Pyrogram in this non-asynchronous way only when you -want to write something without the boilerplate or in case you want to combine Pyrogram with other libraries that are -not async. \ No newline at end of file + asyncio.run(main()) \ No newline at end of file diff --git a/docs/source/start/updates.rst b/docs/source/start/updates.rst index dee5a115..0cdf7650 100644 --- a/docs/source/start/updates.rst +++ b/docs/source/start/updates.rst @@ -1,8 +1,8 @@ Handling Updates ================ -Calling :doc:`API methods ` sequentially is one way to use Pyrogram, but how to react when, for example, a -new message arrives? This page deals with updates and how to handle such events in Pyrogram. +:doc:`Invoking API methods ` sequentially is one way to use Pyrogram. This page deals with Telegram updates +and how to handle new incoming messages or other events in Pyrogram. .. contents:: Contents :backlinks: none @@ -14,7 +14,7 @@ new message arrives? This page deals with updates and how to handle such events Defining Updates ---------------- -As hinted already, updates are simply events that happen in your Telegram account (incoming messages, new members join, +As hinted already, updates are events that happen in your Telegram account (incoming messages, new members join, bot button presses, etc.), which are meant to notify you about a new specific state that has changed. These updates are handled by registering one or more callback functions in your app using :doc:`Handlers <../api/handlers>`. @@ -52,25 +52,6 @@ In the last line we see again the :meth:`~pyrogram.Client.run` method, this time Its purpose here is simply to automatically :meth:`~pyrogram.Client.start`, keep the Client online so that it can listen for updates and :meth:`~pyrogram.Client.stop` it once you hit ``CTRL+C``. -Synchronous handlers -^^^^^^^^^^^^^^^^^^^^^ - -You can also have synchronous handlers; you only need to define the callback function without using ``async def`` and -call API methods by not placing ``await`` in front of them: - -.. code-block:: python - - @app.on_message() - def my_handler(client, message): - message.forward("me") - -.. note:: - - You can mix ``def`` and ``async def`` handlers as much as you like, Pyrogram will still work concurrently and - efficiently regardless of what you choose. However, it is recommended to use Pyrogram in its native, asynchronous - form at all times, unless you want to write something without the boilerplate or in case you want to combine - Pyrogram with other libraries that are not async. - Using add_handler() ^^^^^^^^^^^^^^^^^^^ @@ -91,16 +72,3 @@ function and registers it in your Client. It is useful in case you want to progr app.add_handler(my_handler) app.run() - -The same about synchronous handlers applies for :meth:`~pyrogram.Client.add_handler`: - -.. code-block:: python - - def my_function(client, message): - message.forward("me") - -.. note:: - - From now on, you'll see examples using synchronous code (i.e.: without ``async`` and ``await``, unless when actually - relevant). This is done to keep snippets concise and more readable. Once you get the idea behind a feature, you can - easily turn examples asynchronous later on. diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 5e5ea69c..bcef9c1d 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -35,15 +35,8 @@ class ContinuePropagation(StopAsyncIteration): pass -import asyncio - from . import raw, types, filters, handlers, emoji, enums from .client import Client from .sync import idle -# Save the main thread loop for future references -main_event_loop = asyncio.get_event_loop() - -CRYPTO_EXECUTOR_SIZE_THRESHOLD = 512 - crypto_executor = ThreadPoolExecutor(1, thread_name_prefix="CryptoWorker") diff --git a/pyrogram/client.py b/pyrogram/client.py index ac753f88..525ecf4a 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -452,29 +452,26 @@ class Client(Methods): Example: .. code-block:: python - from pyrogram import Client, enums + from pyrogram import enums - app = Client("my_account") + # Default combined mode: Markdown + HTML + await app.send_message("me", "1. **markdown** and html") - with app: - # Default combined mode: Markdown + HTML - app.send_message("me", "1. **markdown** and html") + # Force Markdown-only, HTML is disabled + app.set_parse_mode(enums.ParseMode.MARKDOWN) + await app.send_message("me", "2. **markdown** and html") - # Force Markdown-only, HTML is disabled - app.set_parse_mode(enums.ParseMode.MARKDOWN) - app.send_message("me", "2. **markdown** and html") + # Force HTML-only, Markdown is disabled + app.set_parse_mode(enums.ParseMode.HTML) + await app.send_message("me", "3. **markdown** and html") - # Force HTML-only, Markdown is disabled - app.set_parse_mode(enums.ParseMode.HTML) - app.send_message("me", "3. **markdown** and html") + # Disable the parser completely + app.set_parse_mode(enums.ParseMode.DISABLED) + await app.send_message("me", "4. **markdown** and html") - # Disable the parser completely - app.set_parse_mode(enums.ParseMode.DISABLED) - app.send_message("me", "4. **markdown** and html") - - # Bring back the default combined mode - app.set_parse_mode(enums.ParseMode.DEFAULT) - app.send_message("me", "5. **markdown** and html") + # Bring back the default combined mode + app.set_parse_mode(enums.ParseMode.DEFAULT) + await app.send_message("me", "5. **markdown** and html") """ self.parse_mode = parse_mode diff --git a/pyrogram/methods/bots/answer_callback_query.py b/pyrogram/methods/bots/answer_callback_query.py index af2c8f72..95d47931 100644 --- a/pyrogram/methods/bots/answer_callback_query.py +++ b/pyrogram/methods/bots/answer_callback_query.py @@ -60,13 +60,13 @@ class AnswerCallbackQuery: .. code-block:: python # Answer only (remove the spinning circles) - app.answer_callback_query(query_id) + await app.answer_callback_query(query_id) # Answer without alert - app.answer_callback_query(query_id, text=text) + await app.answer_callback_query(query_id, text=text) # Answer with alert - app.answer_callback_query(query_id, text=text, show_alert=True) + await app.answer_callback_query(query_id, text=text, show_alert=True) """ return await self.invoke( raw.functions.messages.SetBotCallbackAnswer( diff --git a/pyrogram/methods/bots/answer_inline_query.py b/pyrogram/methods/bots/answer_inline_query.py index ebdfab23..e9acd937 100644 --- a/pyrogram/methods/bots/answer_inline_query.py +++ b/pyrogram/methods/bots/answer_inline_query.py @@ -86,7 +86,7 @@ class AnswerInlineQuery: from pyrogram.types import InlineQueryResultArticle, InputTextMessageContent - app.answer_inline_query( + await app.answer_inline_query( inline_query_id, results=[ InlineQueryResultArticle( diff --git a/pyrogram/methods/bots/delete_bot_commands.py b/pyrogram/methods/bots/delete_bot_commands.py index 98e6d396..2e638aed 100644 --- a/pyrogram/methods/bots/delete_bot_commands.py +++ b/pyrogram/methods/bots/delete_bot_commands.py @@ -49,7 +49,7 @@ class DeleteBotCommands: .. code-block:: python # Delete commands - app.delete_bot_commands() + await app.delete_bot_commands() """ return await self.invoke( diff --git a/pyrogram/methods/bots/get_bot_commands.py b/pyrogram/methods/bots/get_bot_commands.py index fe5be0f3..f62b6375 100644 --- a/pyrogram/methods/bots/get_bot_commands.py +++ b/pyrogram/methods/bots/get_bot_commands.py @@ -51,7 +51,7 @@ class GetBotCommands: .. code-block:: python # Get commands - commands = app.get_bot_commands() + commands = await app.get_bot_commands() print(commands) """ diff --git a/pyrogram/methods/bots/get_game_high_scores.py b/pyrogram/methods/bots/get_game_high_scores.py index 312cc632..555e9af5 100644 --- a/pyrogram/methods/bots/get_game_high_scores.py +++ b/pyrogram/methods/bots/get_game_high_scores.py @@ -54,7 +54,7 @@ class GetGameHighScores: Example: .. code-block:: python - scores = app.get_game_high_scores(user_id, chat_id, message_id) + scores = await app.get_game_high_scores(user_id, chat_id, message_id) print(scores) """ # TODO: inline_message_id diff --git a/pyrogram/methods/bots/get_inline_bot_results.py b/pyrogram/methods/bots/get_inline_bot_results.py index f145cc8c..3ef1d4de 100644 --- a/pyrogram/methods/bots/get_inline_bot_results.py +++ b/pyrogram/methods/bots/get_inline_bot_results.py @@ -64,7 +64,7 @@ class GetInlineBotResults: Example: .. code-block:: python - results = app.get_inline_bot_results("pyrogrambot") + results = await app.get_inline_bot_results("pyrogrambot") print(results) """ # TODO: Don't return the raw type diff --git a/pyrogram/methods/bots/request_callback_answer.py b/pyrogram/methods/bots/request_callback_answer.py index 908c8eca..9c0626a5 100644 --- a/pyrogram/methods/bots/request_callback_answer.py +++ b/pyrogram/methods/bots/request_callback_answer.py @@ -58,7 +58,7 @@ class RequestCallbackAnswer: Example: .. code-block:: python - app.request_callback_answer(chat_id, message_id, "callback_data") + await app.request_callback_answer(chat_id, message_id, "callback_data") """ # Telegram only wants bytes, but we are allowed to pass strings too. diff --git a/pyrogram/methods/bots/send_game.py b/pyrogram/methods/bots/send_game.py index c8eee66e..e4181166 100644 --- a/pyrogram/methods/bots/send_game.py +++ b/pyrogram/methods/bots/send_game.py @@ -69,7 +69,7 @@ class SendGame: Example: .. code-block:: python - app.send_game(chat_id, "gamename") + await app.send_game(chat_id, "gamename") """ r = await self.invoke( raw.functions.messages.SendMedia( diff --git a/pyrogram/methods/bots/send_inline_bot_result.py b/pyrogram/methods/bots/send_inline_bot_result.py index 00b42043..ebe5ca8d 100644 --- a/pyrogram/methods/bots/send_inline_bot_result.py +++ b/pyrogram/methods/bots/send_inline_bot_result.py @@ -59,7 +59,7 @@ class SendInlineBotResult: Example: .. code-block:: python - app.send_inline_bot_result(chat_id, query_id, result_id) + await app.send_inline_bot_result(chat_id, query_id, result_id) """ return await self.invoke( raw.functions.messages.SendInlineBotResult( diff --git a/pyrogram/methods/bots/set_bot_commands.py b/pyrogram/methods/bots/set_bot_commands.py index b59c0fac..7dfbf109 100644 --- a/pyrogram/methods/bots/set_bot_commands.py +++ b/pyrogram/methods/bots/set_bot_commands.py @@ -57,7 +57,7 @@ class SetBotCommands: from pyrogram.types import BotCommand # Set new commands - app.set_bot_commands([ + await app.set_bot_commands([ BotCommand("start", "Start the bot"), BotCommand("settings", "Bot settings")]) """ diff --git a/pyrogram/methods/bots/set_game_score.py b/pyrogram/methods/bots/set_game_score.py index 855e4a2e..f9008e44 100644 --- a/pyrogram/methods/bots/set_game_score.py +++ b/pyrogram/methods/bots/set_game_score.py @@ -70,10 +70,10 @@ class SetGameScore: .. code-block:: python # Set new score - app.set_game_score(user_id, 1000) + await app.set_game_score(user_id, 1000) # Force set new score - app.set_game_score(user_id, 25, force=True) + await app.set_game_score(user_id, 25, force=True) """ r = await self.invoke( raw.functions.messages.SetGameScore( diff --git a/pyrogram/methods/chats/__init__.py b/pyrogram/methods/chats/__init__.py index 6eedbd32..1b744a05 100644 --- a/pyrogram/methods/chats/__init__.py +++ b/pyrogram/methods/chats/__init__.py @@ -36,7 +36,6 @@ from .get_dialogs import GetDialogs from .get_dialogs_count import GetDialogsCount from .get_nearby_chats import GetNearbyChats from .get_send_as_chats import GetSendAsChats -from .iter_chat_members import IterChatMembers from .iter_dialogs import IterDialogs from .join_chat import JoinChat from .leave_chat import LeaveChat @@ -78,7 +77,6 @@ class Chats( GetDialogs, GetChatMembersCount, IterDialogs, - IterChatMembers, SetChatUsername, SetChatPermissions, GetDialogsCount, diff --git a/pyrogram/methods/chats/add_chat_members.py b/pyrogram/methods/chats/add_chat_members.py index 2d053c4e..2bf145f9 100644 --- a/pyrogram/methods/chats/add_chat_members.py +++ b/pyrogram/methods/chats/add_chat_members.py @@ -52,13 +52,13 @@ class AddChatMembers: .. code-block:: python # Add one member to a group or channel - app.add_chat_members(chat_id, user_id) + await app.add_chat_members(chat_id, user_id) # Add multiple members to a group or channel - app.add_chat_members(chat_id, [user_id1, user_id2, user_id3]) + await app.add_chat_members(chat_id, [user_id1, user_id2, user_id3]) # Change forward_limit (for basic groups only) - app.add_chat_members(chat_id, user_id, forward_limit=25) + await app.add_chat_members(chat_id, user_id, forward_limit=25) """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/archive_chats.py b/pyrogram/methods/chats/archive_chats.py index cba27371..40544e0f 100644 --- a/pyrogram/methods/chats/archive_chats.py +++ b/pyrogram/methods/chats/archive_chats.py @@ -41,10 +41,10 @@ class ArchiveChats: .. code-block:: python # Archive chat - app.archive_chats(chat_id) + await app.archive_chats(chat_id) # Archive multiple chats at once - app.archive_chats([chat_id1, chat_id2, chat_id3]) + await app.archive_chats([chat_id1, chat_id2, chat_id3]) """ if not isinstance(chat_ids, list): diff --git a/pyrogram/methods/chats/ban_chat_member.py b/pyrogram/methods/chats/ban_chat_member.py index d8a207ab..8e1bca1e 100644 --- a/pyrogram/methods/chats/ban_chat_member.py +++ b/pyrogram/methods/chats/ban_chat_member.py @@ -64,10 +64,10 @@ class BanChatMember: from datetime import datetime, timedelta # Ban chat member forever - app.ban_chat_member(chat_id, user_id) + await app.ban_chat_member(chat_id, user_id) # Ban chat member and automatically unban after 24h - app.ban_chat_member(chat_id, user_id, datetime.now() + timedelta(days=1)) + await app.ban_chat_member(chat_id, user_id, datetime.now() + timedelta(days=1)) """ chat_peer = await self.resolve_peer(chat_id) user_peer = await self.resolve_peer(user_id) diff --git a/pyrogram/methods/chats/create_channel.py b/pyrogram/methods/chats/create_channel.py index 1b054b6e..2a149778 100644 --- a/pyrogram/methods/chats/create_channel.py +++ b/pyrogram/methods/chats/create_channel.py @@ -41,7 +41,7 @@ class CreateChannel: Example: .. code-block:: python - app.create_channel("Channel Title", "Channel Description") + await app.create_channel("Channel Title", "Channel Description") """ r = await self.invoke( raw.functions.channels.CreateChannel( diff --git a/pyrogram/methods/chats/create_group.py b/pyrogram/methods/chats/create_group.py index 78240f9f..a2ffa8de 100644 --- a/pyrogram/methods/chats/create_group.py +++ b/pyrogram/methods/chats/create_group.py @@ -50,7 +50,7 @@ class CreateGroup: Example: .. code-block:: python - app.create_group("Group Title", user_id) + await app.create_group("Group Title", user_id) """ if not isinstance(users, list): users = [users] diff --git a/pyrogram/methods/chats/create_supergroup.py b/pyrogram/methods/chats/create_supergroup.py index eb922c32..04da148a 100644 --- a/pyrogram/methods/chats/create_supergroup.py +++ b/pyrogram/methods/chats/create_supergroup.py @@ -45,7 +45,7 @@ class CreateSupergroup: Example: .. code-block:: python - app.create_supergroup("Supergroup Title", "Supergroup Description") + await app.create_supergroup("Supergroup Title", "Supergroup Description") """ r = await self.invoke( raw.functions.channels.CreateChannel( diff --git a/pyrogram/methods/chats/delete_channel.py b/pyrogram/methods/chats/delete_channel.py index 210c81f1..7aad47d0 100644 --- a/pyrogram/methods/chats/delete_channel.py +++ b/pyrogram/methods/chats/delete_channel.py @@ -39,7 +39,7 @@ class DeleteChannel: Example: .. code-block:: python - app.delete_channel(channel_id) + await app.delete_channel(channel_id) """ await self.invoke( raw.functions.channels.DeleteChannel( diff --git a/pyrogram/methods/chats/delete_chat_photo.py b/pyrogram/methods/chats/delete_chat_photo.py index ac485603..058b7fdb 100644 --- a/pyrogram/methods/chats/delete_chat_photo.py +++ b/pyrogram/methods/chats/delete_chat_photo.py @@ -44,7 +44,7 @@ class DeleteChatPhoto: Example: .. code-block:: python - app.delete_chat_photo(chat_id) + await app.delete_chat_photo(chat_id) """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/delete_supergroup.py b/pyrogram/methods/chats/delete_supergroup.py index 8fb069ff..39d5a07d 100644 --- a/pyrogram/methods/chats/delete_supergroup.py +++ b/pyrogram/methods/chats/delete_supergroup.py @@ -39,7 +39,7 @@ class DeleteSupergroup: Example: .. code-block:: python - app.delete_supergroup(supergroup_id) + await app.delete_supergroup(supergroup_id) """ await self.invoke( raw.functions.channels.DeleteChannel( diff --git a/pyrogram/methods/chats/get_chat.py b/pyrogram/methods/chats/get_chat.py index fc198019..d3246447 100644 --- a/pyrogram/methods/chats/get_chat.py +++ b/pyrogram/methods/chats/get_chat.py @@ -50,7 +50,7 @@ class GetChat: Example: .. code-block:: python - chat = app.get_chat("pyrogram") + chat = await app.get_chat("pyrogram") print(chat) """ match = self.INVITE_LINK_RE.match(str(chat_id)) diff --git a/pyrogram/methods/chats/get_chat_event_log.py b/pyrogram/methods/chats/get_chat_event_log.py index 2325bad9..622fe0bc 100644 --- a/pyrogram/methods/chats/get_chat_event_log.py +++ b/pyrogram/methods/chats/get_chat_event_log.py @@ -64,6 +64,12 @@ class GetChatEventLog: Yields: :obj:`~pyrogram.types.ChatEvent` objects. + + Example: + .. code-block:: python + + async for event in app.get_chat_event_log(chat_id): + print(event) """ current = 0 total = abs(limit) or (1 << 31) diff --git a/pyrogram/methods/chats/get_chat_member.py b/pyrogram/methods/chats/get_chat_member.py index 1b24d213..526cf744 100644 --- a/pyrogram/methods/chats/get_chat_member.py +++ b/pyrogram/methods/chats/get_chat_member.py @@ -47,7 +47,7 @@ class GetChatMember: Example: .. code-block:: python - member = app.get_chat_member(chat_id, "me") + member = await app.get_chat_member(chat_id, "me") print(member) """ chat = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/get_chat_members.py b/pyrogram/methods/chats/get_chat_members.py index c2516a70..065e262c 100644 --- a/pyrogram/methods/chats/get_chat_members.py +++ b/pyrogram/methods/chats/get_chat_members.py @@ -97,16 +97,16 @@ class GetChatMembers: from pyrogram import enums # Get members - for member in app.get_chat_members(chat_id): + async for member in app.get_chat_members(chat_id): print(member) # Get administrators - administrators = list(app.get_chat_members( + administrators = list(await app.get_chat_members( chat_id, filter=enums.ChatMembersFilter.ADMINISTRATORS)) # Get bots - bots = list(app.get_chat_members( + bots = list(await app.get_chat_members( chat_id, filter=enums.ChatMembersFilter.BOTS)) """ diff --git a/pyrogram/methods/chats/get_chat_members_count.py b/pyrogram/methods/chats/get_chat_members_count.py index 9c57a11e..e2f7fa8b 100644 --- a/pyrogram/methods/chats/get_chat_members_count.py +++ b/pyrogram/methods/chats/get_chat_members_count.py @@ -42,7 +42,7 @@ class GetChatMembersCount: Example: .. code-block:: python - count = app.get_chat_members_count(chat_id) + count = await app.get_chat_members_count(chat_id) print(count) """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/get_chat_online_count.py b/pyrogram/methods/chats/get_chat_online_count.py index 19924542..45c60d97 100644 --- a/pyrogram/methods/chats/get_chat_online_count.py +++ b/pyrogram/methods/chats/get_chat_online_count.py @@ -39,7 +39,7 @@ class GetChatOnlineCount: Example: .. code-block:: python - online = app.get_chat_online_count(chat_id) + online = await app.get_chat_online_count(chat_id) print(online) """ return (await self.invoke( diff --git a/pyrogram/methods/chats/get_dialogs.py b/pyrogram/methods/chats/get_dialogs.py index 4aaa95ff..f888c3ef 100644 --- a/pyrogram/methods/chats/get_dialogs.py +++ b/pyrogram/methods/chats/get_dialogs.py @@ -60,10 +60,10 @@ class GetDialogs: .. code-block:: python # Get first 100 dialogs - app.get_dialogs() + await app.get_dialogs() # Get pinned dialogs - app.get_dialogs(pinned_only=True) + await app.get_dialogs(pinned_only=True) """ if pinned_only: diff --git a/pyrogram/methods/chats/get_dialogs_count.py b/pyrogram/methods/chats/get_dialogs_count.py index 8ca237cf..3d96732c 100644 --- a/pyrogram/methods/chats/get_dialogs_count.py +++ b/pyrogram/methods/chats/get_dialogs_count.py @@ -37,7 +37,7 @@ class GetDialogsCount: Example: .. code-block:: python - count = app.get_dialogs_count() + count = await app.get_dialogs_count() print(count) """ diff --git a/pyrogram/methods/chats/get_nearby_chats.py b/pyrogram/methods/chats/get_nearby_chats.py index c7c36cc6..2ebd9306 100644 --- a/pyrogram/methods/chats/get_nearby_chats.py +++ b/pyrogram/methods/chats/get_nearby_chats.py @@ -45,7 +45,7 @@ class GetNearbyChats: Example: .. code-block:: python - chats = app.get_nearby_chats(51.500729, -0.124583) + chats = await app.get_nearby_chats(latitude, longitude) print(chats) """ diff --git a/pyrogram/methods/chats/get_send_as_chats.py b/pyrogram/methods/chats/get_send_as_chats.py index 2db4d5f0..2ad3b8b5 100644 --- a/pyrogram/methods/chats/get_send_as_chats.py +++ b/pyrogram/methods/chats/get_send_as_chats.py @@ -40,7 +40,7 @@ class GetSendAsChats: Example: .. code-block:: python - chats = app.get_send_as_chats(chat_id) + chats = await app.get_send_as_chats(chat_id) print(chats) """ r = await self.invoke( diff --git a/pyrogram/methods/chats/iter_chat_members.py b/pyrogram/methods/chats/iter_chat_members.py deleted file mode 100644 index f3ccf06c..00000000 --- a/pyrogram/methods/chats/iter_chat_members.py +++ /dev/null @@ -1,127 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-present Dan -# -# 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 . - -from typing import Union, AsyncGenerator, Optional - -import pyrogram -from pyrogram import raw -from pyrogram import types - - -class Filters: - ALL = "all" - BANNED = "banned" - RESTRICTED = "restricted" - BOTS = "bots" - RECENT = "recent" - ADMINISTRATORS = "administrators" - - -class IterChatMembers: - async def iter_chat_members( - self: "pyrogram.Client", - chat_id: Union[int, str], - limit: int = 0, - query: str = "", - filter: str = Filters.RECENT - ) -> Optional[AsyncGenerator["types.ChatMember", None]]: - """Iterate through the members of a chat sequentially. - - This convenience method does the same as repeatedly calling :meth:`~pyrogram.Client.get_chat_members` in a loop, - thus saving you from the hassle of setting up boilerplate code. It is useful for getting the whole members list - of a chat with a single call. - - Parameters: - chat_id (``int`` | ``str``): - Unique identifier (int) or username (str) of the target chat. - - limit (``int``, *optional*): - Limits the number of members to be retrieved. - - query (``str``, *optional*): - Query string to filter members based on their display names and usernames. - Defaults to "" (empty string). - A query string is applicable only for *"all"*, *"banned"* and *"restricted"* filters only. - - filter (``str``, *optional*): - Filter used to select the kind of members you want to retrieve. Only applicable for supergroups - and channels. It can be any of the followings: - *"all"* - all kind of members, - *"banned"* - banned members only, - *"restricted"* - restricted members only, - *"bots"* - bots only, - *"recent"* - recent members only, - *"administrators"* - chat administrators only. - Defaults to *"recent"*. - - Returns: - ``Generator``: A generator yielding :obj:`~pyrogram.types.ChatMember` objects. - - Example: - .. code-block:: python - - # Iterate though all chat members - for member in app.iter_chat_members(chat_id): - print(member.user.first_name) - - # Iterate though all administrators - for member in app.iter_chat_members(chat_id, filter="administrators"): - print(member.user.first_name) - - # Iterate though all bots - for member in app.iter_chat_members(chat_id, filter="bots"): - print(member.user.first_name) - """ - current = 0 - yielded = set() - total = limit or (1 << 31) - 1 - limit = min(200, total) - resolved_chat_id = await self.resolve_peer(chat_id) - offset = 0 - - while True: - chat_members = await self.get_chat_members( - chat_id=chat_id, - offset=offset, - limit=limit, - query=query, - filter=filter - ) - - if not chat_members: - break - - if isinstance(resolved_chat_id, raw.types.InputPeerChat): - total = len(chat_members) - - offset += len(chat_members) - - for chat_member in chat_members: - peer_id = chat_member.user.id if chat_member.user else chat_member.chat.id - - if peer_id in yielded: - continue - - yield chat_member - - yielded.add(peer_id) - - current += 1 - - if current >= total: - return diff --git a/pyrogram/methods/chats/iter_dialogs.py b/pyrogram/methods/chats/iter_dialogs.py index 72a396f7..d0f037be 100644 --- a/pyrogram/methods/chats/iter_dialogs.py +++ b/pyrogram/methods/chats/iter_dialogs.py @@ -45,7 +45,7 @@ class IterDialogs: .. code-block:: python # Iterate through all dialogs - for dialog in app.iter_dialogs(): + async for dialog in app.iter_dialogs(): print(dialog.chat.first_name or dialog.chat.title) """ current = 0 diff --git a/pyrogram/methods/chats/join_chat.py b/pyrogram/methods/chats/join_chat.py index 2534442a..07d7b2a7 100644 --- a/pyrogram/methods/chats/join_chat.py +++ b/pyrogram/methods/chats/join_chat.py @@ -41,14 +41,14 @@ class JoinChat: Example: .. code-block:: python - # Join chat via username - app.join_chat("pyrogram") - # Join chat via invite link - app.join_chat("https://t.me/joinchat/AAAAAE0QmSW3IUmm3UFR7A") + await app.join_chat("https://t.me/+AbCdEf0123456789") + + # Join chat via username + await app.join_chat("pyrogram") # Join a linked chat - app.join_chat(app.get_chat("pyrogram").linked_chat.id) + await app.join_chat(app.get_chat("pyrogram").linked_chat.id) """ match = self.INVITE_LINK_RE.match(str(chat_id)) diff --git a/pyrogram/methods/chats/leave_chat.py b/pyrogram/methods/chats/leave_chat.py index 7a6eb85d..942173f4 100644 --- a/pyrogram/methods/chats/leave_chat.py +++ b/pyrogram/methods/chats/leave_chat.py @@ -43,10 +43,10 @@ class LeaveChat: .. code-block:: python # Leave chat or channel - app.leave_chat(chat_id) + await app.leave_chat(chat_id) # Leave basic chat and also delete the dialog - app.leave_chat(chat_id, delete=True) + await app.leave_chat(chat_id, delete=True) """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/pin_chat_message.py b/pyrogram/methods/chats/pin_chat_message.py index 1f5ac912..0eaa1525 100644 --- a/pyrogram/methods/chats/pin_chat_message.py +++ b/pyrogram/methods/chats/pin_chat_message.py @@ -56,10 +56,10 @@ class PinChatMessage: .. code-block:: python # Pin with notification - app.pin_chat_message(chat_id, message_id) + await app.pin_chat_message(chat_id, message_id) # Pin without notification - app.pin_chat_message(chat_id, message_id, disable_notification=True) + await app.pin_chat_message(chat_id, message_id, disable_notification=True) """ r = await self.invoke( raw.functions.messages.UpdatePinnedMessage( diff --git a/pyrogram/methods/chats/promote_chat_member.py b/pyrogram/methods/chats/promote_chat_member.py index b64a1799..e6f2171e 100644 --- a/pyrogram/methods/chats/promote_chat_member.py +++ b/pyrogram/methods/chats/promote_chat_member.py @@ -27,7 +27,7 @@ class PromoteChatMember: self: "pyrogram.Client", chat_id: Union[int, str], user_id: Union[int, str], - privileges: "types.ChatPrivileges" = types.ChatPrivileges(), + privileges: "types.ChatPrivileges" = None, ) -> bool: """Promote or demote a user in a supergroup or a channel. @@ -52,11 +52,15 @@ class PromoteChatMember: .. code-block:: python # Promote chat member to admin - app.promote_chat_member(chat_id, user_id) + await app.promote_chat_member(chat_id, user_id) """ chat_id = await self.resolve_peer(chat_id) user_id = await self.resolve_peer(user_id) + # See Chat.promote_member for the reason of this (instead of setting types.ChatPrivileges() as default arg). + if privileges is None: + privileges = types.ChatPrivileges() + raw_chat_member = (await self.invoke( raw.functions.channels.GetParticipant( channel=chat_id, diff --git a/pyrogram/methods/chats/restrict_chat_member.py b/pyrogram/methods/chats/restrict_chat_member.py index 52b264e8..4ebb5f18 100644 --- a/pyrogram/methods/chats/restrict_chat_member.py +++ b/pyrogram/methods/chats/restrict_chat_member.py @@ -60,17 +60,18 @@ class RestrictChatMember: .. code-block:: python from datetime import datetime, timedelta - from pyrogram.types import ChatPermissions # Completely restrict chat member (mute) forever - app.restrict_chat_member(chat_id, user_id, ChatPermissions()) + await app.restrict_chat_member(chat_id, user_id, ChatPermissions()) # Chat member muted for 24h - app.restrict_chat_member(chat_id, user_id, ChatPermissions(), datetime.now() + timedelta(days=1)) + await app.restrict_chat_member(chat_id, user_id, ChatPermissions(), + datetime.now() + timedelta(days=1)) # Chat member can only send text messages - app.restrict_chat_member(chat_id, user_id, ChatPermissions(can_send_messages=True)) + await app.restrict_chat_member(chat_id, user_id, + ChatPermissions(can_send_messages=True)) """ r = await self.invoke( raw.functions.channels.EditBanned( diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py index 2ea0dccf..15f9dea5 100644 --- a/pyrogram/methods/chats/set_administrator_title.py +++ b/pyrogram/methods/chats/set_administrator_title.py @@ -52,7 +52,7 @@ class SetAdministratorTitle: Example: .. code-block:: python - app.set_administrator_title(chat_id, user_id, "Admin Title") + await app.set_administrator_title(chat_id, user_id, "Admin Title") """ chat_id = await self.resolve_peer(chat_id) user_id = await self.resolve_peer(user_id) diff --git a/pyrogram/methods/chats/set_chat_description.py b/pyrogram/methods/chats/set_chat_description.py index 4a93530b..3eee92ca 100644 --- a/pyrogram/methods/chats/set_chat_description.py +++ b/pyrogram/methods/chats/set_chat_description.py @@ -47,7 +47,7 @@ class SetChatDescription: Example: .. code-block:: python - app.set_chat_description(chat_id, "New Description") + await app.set_chat_description(chat_id, "New Description") """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/set_chat_permissions.py b/pyrogram/methods/chats/set_chat_permissions.py index 51d74fee..d1280ea7 100644 --- a/pyrogram/methods/chats/set_chat_permissions.py +++ b/pyrogram/methods/chats/set_chat_permissions.py @@ -50,10 +50,10 @@ class SetChatPermissions: from pyrogram.types import ChatPermissions # Completely restrict chat - app.set_chat_permissions(chat_id, ChatPermissions()) + await app.set_chat_permissions(chat_id, ChatPermissions()) # Chat members can only send text messages and media messages - app.set_chat_permissions( + await app.set_chat_permissions( chat_id, ChatPermissions( can_send_messages=True, diff --git a/pyrogram/methods/chats/set_chat_photo.py b/pyrogram/methods/chats/set_chat_photo.py index 6da4330b..dd44bb87 100644 --- a/pyrogram/methods/chats/set_chat_photo.py +++ b/pyrogram/methods/chats/set_chat_photo.py @@ -68,17 +68,17 @@ class SetChatPhoto: .. code-block:: python # Set chat photo using a local file - app.set_chat_photo(chat_id, photo="photo.jpg") + await app.set_chat_photo(chat_id, photo="photo.jpg") # Set chat photo using an exiting Photo file_id - app.set_chat_photo(chat_id, photo=photo.file_id) + await app.set_chat_photo(chat_id, photo=photo.file_id) # Set chat video using a local file - app.set_chat_photo(chat_id, video="video.mp4") + await app.set_chat_photo(chat_id, video="video.mp4") # Set chat photo using an exiting Video file_id - app.set_chat_photo(chat_id, video=video.file_id) + await app.set_chat_photo(chat_id, video=video.file_id) """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/set_chat_title.py b/pyrogram/methods/chats/set_chat_title.py index edebf176..e2c270e6 100644 --- a/pyrogram/methods/chats/set_chat_title.py +++ b/pyrogram/methods/chats/set_chat_title.py @@ -52,7 +52,7 @@ class SetChatTitle: Example: .. code-block:: python - app.set_chat_title(chat_id, "New Title") + await app.set_chat_title(chat_id, "New Title") """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/set_chat_username.py b/pyrogram/methods/chats/set_chat_username.py index c1f6d17d..2d4bc08a 100644 --- a/pyrogram/methods/chats/set_chat_username.py +++ b/pyrogram/methods/chats/set_chat_username.py @@ -48,7 +48,7 @@ class SetChatUsername: Example: .. code-block:: python - app.set_chat_username(chat_id, "new_username") + await app.set_chat_username(chat_id, "new_username") """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/chats/set_send_as_chat.py b/pyrogram/methods/chats/set_send_as_chat.py index bbe210a4..4cb50aed 100644 --- a/pyrogram/methods/chats/set_send_as_chat.py +++ b/pyrogram/methods/chats/set_send_as_chat.py @@ -45,7 +45,7 @@ class SetSendAsChat: Example: .. code-block:: python - app.set_send_as_chat(chat_id, send_as_chat_id) + await app.set_send_as_chat(chat_id, send_as_chat_id) """ return await self.invoke( raw.functions.messages.SaveDefaultSendAs( diff --git a/pyrogram/methods/chats/set_slow_mode.py b/pyrogram/methods/chats/set_slow_mode.py index e3237ad3..ca68c87f 100644 --- a/pyrogram/methods/chats/set_slow_mode.py +++ b/pyrogram/methods/chats/set_slow_mode.py @@ -45,10 +45,10 @@ class SetSlowMode: .. code-block:: python # Set slow mode to 60 seconds - app.set_slow_mode(chat_id, 60) + await app.set_slow_mode(chat_id, 60) # Disable slow mode - app.set_slow_mode(chat_id, None) + await app.set_slow_mode(chat_id, None) """ await self.invoke( diff --git a/pyrogram/methods/chats/unarchive_chats.py b/pyrogram/methods/chats/unarchive_chats.py index 726eb4a0..c518fe91 100644 --- a/pyrogram/methods/chats/unarchive_chats.py +++ b/pyrogram/methods/chats/unarchive_chats.py @@ -41,10 +41,10 @@ class UnarchiveChats: .. code-block:: python # Unarchive chat - app.unarchive_chats(chat_id) + await app.unarchive_chats(chat_id) # Unarchive multiple chats at once - app.unarchive_chats([chat_id1, chat_id2, chat_id3]) + await app.unarchive_chats([chat_id1, chat_id2, chat_id3]) """ if not isinstance(chat_ids, list): diff --git a/pyrogram/methods/chats/unban_chat_member.py b/pyrogram/methods/chats/unban_chat_member.py index c331e823..46c2241e 100644 --- a/pyrogram/methods/chats/unban_chat_member.py +++ b/pyrogram/methods/chats/unban_chat_member.py @@ -47,7 +47,7 @@ class UnbanChatMember: .. code-block:: python # Unban chat member right now - app.unban_chat_member(chat_id, user_id) + await app.unban_chat_member(chat_id, user_id) """ await self.invoke( raw.functions.channels.EditBanned( diff --git a/pyrogram/methods/chats/unpin_all_chat_messages.py b/pyrogram/methods/chats/unpin_all_chat_messages.py index 93b6eaec..866ca7c0 100644 --- a/pyrogram/methods/chats/unpin_all_chat_messages.py +++ b/pyrogram/methods/chats/unpin_all_chat_messages.py @@ -42,7 +42,7 @@ class UnpinAllChatMessages: .. code-block:: python # Unpin all chat messages - app.unpin_all_chat_messages(chat_id) + await app.unpin_all_chat_messages(chat_id) """ await self.invoke( raw.functions.messages.UnpinAllMessages( diff --git a/pyrogram/methods/chats/unpin_chat_message.py b/pyrogram/methods/chats/unpin_chat_message.py index 6723f5a2..5b3768c0 100644 --- a/pyrogram/methods/chats/unpin_chat_message.py +++ b/pyrogram/methods/chats/unpin_chat_message.py @@ -46,7 +46,7 @@ class UnpinChatMessage: Example: .. code-block:: python - app.unpin_chat_message(chat_id, message_id) + await app.unpin_chat_message(chat_id, message_id) """ await self.invoke( raw.functions.messages.UpdatePinnedMessage( diff --git a/pyrogram/methods/contacts/add_contact.py b/pyrogram/methods/contacts/add_contact.py index 433c4c90..ae7566b2 100644 --- a/pyrogram/methods/contacts/add_contact.py +++ b/pyrogram/methods/contacts/add_contact.py @@ -57,8 +57,11 @@ class AddContact: Example: .. code-block:: python - app.add_contact(12345678, "Foo") - app.add_contact("username", "Bar") + # Add contact by id + await app.add_contact(12345678, "Foo") + + # Add contact by username + await app.add_contact("username", "Bar") """ r = await self.invoke( raw.functions.contacts.AddContact( diff --git a/pyrogram/methods/contacts/delete_contacts.py b/pyrogram/methods/contacts/delete_contacts.py index 03238beb..31d2bcbe 100644 --- a/pyrogram/methods/contacts/delete_contacts.py +++ b/pyrogram/methods/contacts/delete_contacts.py @@ -42,8 +42,8 @@ class DeleteContacts: Example: .. code-block:: python - app.delete_contacts(user_id) - app.delete_contacts([user_id1, user_id2, user_id3]) + await app.delete_contacts(user_id) + await app.delete_contacts([user_id1, user_id2, user_id3]) """ is_user_ids_list = isinstance(user_ids, list) diff --git a/pyrogram/methods/contacts/get_contacts.py b/pyrogram/methods/contacts/get_contacts.py index ca888876..110d93a3 100644 --- a/pyrogram/methods/contacts/get_contacts.py +++ b/pyrogram/methods/contacts/get_contacts.py @@ -38,7 +38,7 @@ class GetContacts: Example: .. code-block:: python - contacts = app.get_contacts() + contacts = await app.get_contacts() print(contacts) """ contacts = await self.invoke(raw.functions.contacts.GetContacts(hash=0)) diff --git a/pyrogram/methods/contacts/get_contacts_count.py b/pyrogram/methods/contacts/get_contacts_count.py index d32ae050..a709b676 100644 --- a/pyrogram/methods/contacts/get_contacts_count.py +++ b/pyrogram/methods/contacts/get_contacts_count.py @@ -32,7 +32,7 @@ class GetContactsCount: Example: .. code-block:: python - count = app.get_contacts_count() + count = await app.get_contacts_count() print(count) """ diff --git a/pyrogram/methods/contacts/import_contacts.py b/pyrogram/methods/contacts/import_contacts.py index de802cff..11df315b 100644 --- a/pyrogram/methods/contacts/import_contacts.py +++ b/pyrogram/methods/contacts/import_contacts.py @@ -42,7 +42,7 @@ class ImportContacts: from pyrogram.types import InputPhoneContact - app.import_contacts([ + await app.import_contacts([ InputPhoneContact("+1-123-456-7890", "Foo"), InputPhoneContact("+1-456-789-0123", "Bar"), InputPhoneContact("+1-789-012-3456", "Baz")]) diff --git a/pyrogram/methods/invite_links/create_chat_invite_link.py b/pyrogram/methods/invite_links/create_chat_invite_link.py index 0e3e63a7..703d3d14 100644 --- a/pyrogram/methods/invite_links/create_chat_invite_link.py +++ b/pyrogram/methods/invite_links/create_chat_invite_link.py @@ -67,10 +67,10 @@ class CreateChatInviteLink: .. code-block:: python # Create a new link without limits - link = app.create_chat_invite_link(chat_id) + link = await app.create_chat_invite_link(chat_id) - # Create a new link for up to 7 new users - link = app.create_chat_invite_link(chat_id, member_limit=7) + # Create a new link for up to 3 new users + link = await app.create_chat_invite_link(chat_id, member_limit=3) """ r = await self.invoke( raw.functions.messages.ExportChatInvite( diff --git a/pyrogram/methods/invite_links/edit_chat_invite_link.py b/pyrogram/methods/invite_links/edit_chat_invite_link.py index 96dcf79d..29f658ec 100644 --- a/pyrogram/methods/invite_links/edit_chat_invite_link.py +++ b/pyrogram/methods/invite_links/edit_chat_invite_link.py @@ -69,10 +69,10 @@ class EditChatInviteLink: .. code-block:: python # Edit the member limit of a link - link = app.edit_chat_invite_link(chat_id, invite_link, member_limit=9) + link = await app.edit_chat_invite_link(chat_id, invite_link, member_limit=5) # Set no expiration date of a link - link = app.edit_chat_invite_link(chat_id, invite_link, expire_date=0) + link = await app.edit_chat_invite_link(chat_id, invite_link, expire_date=0) """ r = await self.invoke( raw.functions.messages.EditExportedChatInvite( diff --git a/pyrogram/methods/invite_links/export_chat_invite_link.py b/pyrogram/methods/invite_links/export_chat_invite_link.py index 66fb0227..ae40391f 100644 --- a/pyrogram/methods/invite_links/export_chat_invite_link.py +++ b/pyrogram/methods/invite_links/export_chat_invite_link.py @@ -51,7 +51,7 @@ class ExportChatInviteLink: .. code-block:: python # Generate a new primary link - link = app.export_chat_invite_link(chat_id) + link = await app.export_chat_invite_link(chat_id) """ r = await self.invoke( raw.functions.messages.ExportChatInvite( diff --git a/pyrogram/methods/messages/copy_media_group.py b/pyrogram/methods/messages/copy_media_group.py index b204999f..de47465d 100644 --- a/pyrogram/methods/messages/copy_media_group.py +++ b/pyrogram/methods/messages/copy_media_group.py @@ -76,9 +76,12 @@ class CopyMediaGroup: .. code-block:: python # Copy a media group - app.copy_media_group("me", source_chat, message_id) - app.copy_media_group("me", source_chat, message_id, captions="single caption") - app.copy_media_group("me", source_chat, message_id, captions=["caption 1", None, ""]) + await app.copy_media_group(to_chat, from_chat, 123) + + await app.copy_media_group(to_chat, from_chat, 123, captions="single caption") + + await app.copy_media_group(to_chat, from_chat, 123, + captions=["caption 1", None, ""]) """ media_group = await self.get_media_group(from_chat_id, message_id) diff --git a/pyrogram/methods/messages/copy_message.py b/pyrogram/methods/messages/copy_message.py index 94c11d4b..b114a9e0 100644 --- a/pyrogram/methods/messages/copy_message.py +++ b/pyrogram/methods/messages/copy_message.py @@ -101,7 +101,7 @@ class CopyMessage: .. code-block:: python # Copy a message - app.copy_message("me", "pyrogram", 20) + await app.copy_message(to_chat, from_chat, 123) """ message: types.Message = await self.get_messages(from_chat_id, message_id) diff --git a/pyrogram/methods/messages/delete_messages.py b/pyrogram/methods/messages/delete_messages.py index f8c3e7fe..b25c0ad4 100644 --- a/pyrogram/methods/messages/delete_messages.py +++ b/pyrogram/methods/messages/delete_messages.py @@ -54,13 +54,13 @@ class DeleteMessages: .. code-block:: python # Delete one message - app.delete_messages(chat_id, message_id) + await app.delete_messages(chat_id, message_id) # Delete multiple messages at once - app.delete_messages(chat_id, list_of_message_ids) + await app.delete_messages(chat_id, list_of_message_ids) # Delete messages only on your side (without revoking) - app.delete_messages(chat_id, message_id, revoke=False) + await app.delete_messages(chat_id, message_id, revoke=False) """ peer = await self.resolve_peer(chat_id) message_ids = list(message_ids) if not isinstance(message_ids, int) else [message_ids] diff --git a/pyrogram/methods/messages/download_media.py b/pyrogram/methods/messages/download_media.py index d75ba281..8b587d2f 100644 --- a/pyrogram/methods/messages/download_media.py +++ b/pyrogram/methods/messages/download_media.py @@ -89,16 +89,16 @@ class DownloadMedia: .. code-block:: python # Download from Message - app.download_media(message) + await app.download_media(message) # Download from file id - app.download_media(message.photo.file_id) + await app.download_media(message.photo.file_id) # Keep track of the progress while downloading - def progress(current, total): + async def progress(current, total): print(f"{current * 100 / total:.1f}%") - app.download_media(message, progress=progress) + await app.download_media(message, progress=progress) """ available_media = ("audio", "document", "photo", "sticker", "animation", "video", "voice", "video_note", "new_chat_photo") diff --git a/pyrogram/methods/messages/edit_inline_caption.py b/pyrogram/methods/messages/edit_inline_caption.py index 3068098e..0996d34e 100644 --- a/pyrogram/methods/messages/edit_inline_caption.py +++ b/pyrogram/methods/messages/edit_inline_caption.py @@ -53,7 +53,7 @@ class EditInlineCaption: .. code-block:: python # Bots only - app.edit_inline_caption(inline_message_id, "new media caption") + await app.edit_inline_caption(inline_message_id, "new media caption") """ return await self.edit_inline_text( inline_message_id=inline_message_id, diff --git a/pyrogram/methods/messages/edit_inline_media.py b/pyrogram/methods/messages/edit_inline_media.py index 1bf8d4d2..59a18aa7 100644 --- a/pyrogram/methods/messages/edit_inline_media.py +++ b/pyrogram/methods/messages/edit_inline_media.py @@ -61,13 +61,13 @@ class EditInlineMedia: # Bots only # Replace the current media with a local photo - app.edit_inline_media(inline_message_id, InputMediaPhoto("new_photo.jpg")) + await app.edit_inline_media(inline_message_id, InputMediaPhoto("new_photo.jpg")) # Replace the current media with a local video - app.edit_inline_media(inline_message_id, InputMediaVideo("new_video.mp4")) + await app.edit_inline_media(inline_message_id, InputMediaVideo("new_video.mp4")) # Replace the current media with a local audio - app.edit_inline_media(inline_message_id, InputMediaAudio("new_audio.mp3")) + await app.edit_inline_media(inline_message_id, InputMediaAudio("new_audio.mp3")) """ caption = media.caption parse_mode = media.parse_mode diff --git a/pyrogram/methods/messages/edit_inline_reply_markup.py b/pyrogram/methods/messages/edit_inline_reply_markup.py index 92c6851e..4e06e447 100644 --- a/pyrogram/methods/messages/edit_inline_reply_markup.py +++ b/pyrogram/methods/messages/edit_inline_reply_markup.py @@ -47,7 +47,7 @@ class EditInlineReplyMarkup: from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton # Bots only - app.edit_inline_reply_markup( + await app.edit_inline_reply_markup( inline_message_id, InlineKeyboardMarkup([[ InlineKeyboardButton("New button", callback_data="new_data")]])) diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py index 9a50d863..9caee55a 100644 --- a/pyrogram/methods/messages/edit_inline_text.py +++ b/pyrogram/methods/messages/edit_inline_text.py @@ -62,10 +62,10 @@ class EditInlineText: # Bots only # Simple edit text - app.edit_inline_text(inline_message_id, "new text") + await app.edit_inline_text(inline_message_id, "new text") # Take the same text message, remove the web page preview only - app.edit_inline_text( + await app.edit_inline_text( inline_message_id, message.text, disable_web_page_preview=True) """ diff --git a/pyrogram/methods/messages/edit_message_caption.py b/pyrogram/methods/messages/edit_message_caption.py index fc92b02f..d3b311db 100644 --- a/pyrogram/methods/messages/edit_message_caption.py +++ b/pyrogram/methods/messages/edit_message_caption.py @@ -62,7 +62,7 @@ class EditMessageCaption: Example: .. code-block:: python - app.edit_message_caption(chat_id, message_id, "new media caption") + await app.edit_message_caption(chat_id, message_id, "new media caption") """ return await self.edit_message_text( chat_id=chat_id, diff --git a/pyrogram/methods/messages/edit_message_media.py b/pyrogram/methods/messages/edit_message_media.py index 0e3f360c..3dce81ed 100644 --- a/pyrogram/methods/messages/edit_message_media.py +++ b/pyrogram/methods/messages/edit_message_media.py @@ -69,13 +69,16 @@ class EditMessageMedia: from pyrogram.types import InputMediaPhoto, InputMediaVideo, InputMediaAudio # Replace the current media with a local photo - app.edit_message_media(chat_id, message_id, InputMediaPhoto("new_photo.jpg")) + await app.edit_message_media(chat_id, message_id, + InputMediaPhoto("new_photo.jpg")) # Replace the current media with a local video - app.edit_message_media(chat_id, message_id, InputMediaVideo("new_video.mp4")) + await app.edit_message_media(chat_id, message_id, + InputMediaVideo("new_video.mp4")) # Replace the current media with a local audio - app.edit_message_media(chat_id, message_id, InputMediaAudio("new_audio.mp3")) + await app.edit_message_media(chat_id, message_id, + InputMediaAudio("new_audio.mp3")) """ caption = media.caption parse_mode = media.parse_mode diff --git a/pyrogram/methods/messages/edit_message_reply_markup.py b/pyrogram/methods/messages/edit_message_reply_markup.py index c164afbf..088e646d 100644 --- a/pyrogram/methods/messages/edit_message_reply_markup.py +++ b/pyrogram/methods/messages/edit_message_reply_markup.py @@ -53,7 +53,7 @@ class EditMessageReplyMarkup: from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton # Bots only - app.edit_message_reply_markup( + await app.edit_message_reply_markup( chat_id, message_id, InlineKeyboardMarkup([[ InlineKeyboardButton("New button", callback_data="new_data")]])) diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py index 551beaa1..94852de5 100644 --- a/pyrogram/methods/messages/edit_message_text.py +++ b/pyrogram/methods/messages/edit_message_text.py @@ -69,10 +69,10 @@ class EditMessageText: .. code-block:: python # Simple edit text - app.edit_message_text(chat_id, message_id, "new text") + await app.edit_message_text(chat_id, message_id, "new text") # Take the same text message, remove the web page preview only - app.edit_message_text( + await app.edit_message_text( chat_id, message_id, message.text, disable_web_page_preview=True) """ diff --git a/pyrogram/methods/messages/forward_messages.py b/pyrogram/methods/messages/forward_messages.py index dbc5534e..6ee84922 100644 --- a/pyrogram/methods/messages/forward_messages.py +++ b/pyrogram/methods/messages/forward_messages.py @@ -70,10 +70,10 @@ class ForwardMessages: .. code-block:: python # Forward a single message - app.forward_messages("me", "pyrogram", 20) + await app.forward_messages(to_chat, from_chat, 123) # Forward multiple messages at once - app.forward_messages("me", "pyrogram", [3, 20, 27]) + await app.forward_messages(to_chat, from_chat, [1, 2, 3]) """ is_iterable = not isinstance(message_ids, int) diff --git a/pyrogram/methods/messages/get_chat_history.py b/pyrogram/methods/messages/get_chat_history.py index bc2ed41f..f21a081f 100644 --- a/pyrogram/methods/messages/get_chat_history.py +++ b/pyrogram/methods/messages/get_chat_history.py @@ -88,7 +88,7 @@ class GetChatHistory: Example: .. code-block:: python - for message in app.get_chat_history(chat_id): + async for message in app.get_chat_history(chat_id): print(message.text) """ current = 0 diff --git a/pyrogram/methods/messages/get_chat_history_count.py b/pyrogram/methods/messages/get_chat_history_count.py index fbe6bfca..46f61eb0 100644 --- a/pyrogram/methods/messages/get_chat_history_count.py +++ b/pyrogram/methods/messages/get_chat_history_count.py @@ -48,7 +48,7 @@ class GetChatHistoryCount: Example: .. code-block:: python - app.get_history_count(chat_id) + await app.get_history_count(chat_id) """ r = await self.invoke( diff --git a/pyrogram/methods/messages/get_discussion_message.py b/pyrogram/methods/messages/get_discussion_message.py index cbcc7bfe..93510e21 100644 --- a/pyrogram/methods/messages/get_discussion_message.py +++ b/pyrogram/methods/messages/get_discussion_message.py @@ -45,10 +45,10 @@ class GetDiscussionMessage: .. code-block:: python # Get the discussion message - m = app.get_discussion_message(channel_id, message_id) + m = await app.get_discussion_message(channel_id, message_id) # Comment to the post by replying - m.reply("comment") + await m.reply("comment") """ r = await self.invoke( raw.functions.messages.GetDiscussionMessage( diff --git a/pyrogram/methods/messages/get_discussion_replies.py b/pyrogram/methods/messages/get_discussion_replies.py index b1279de5..467cca8d 100644 --- a/pyrogram/methods/messages/get_discussion_replies.py +++ b/pyrogram/methods/messages/get_discussion_replies.py @@ -45,8 +45,8 @@ class GetDiscussionReplies: Example: .. code-block:: python - for m in app.get_discussion_replies(chat_id, message_id): - print(m) + async for message in app.get_discussion_replies(chat_id, message_id): + print(message) """ current = 0 diff --git a/pyrogram/methods/messages/get_discussion_replies_count.py b/pyrogram/methods/messages/get_discussion_replies_count.py index cb5c1e8b..260be802 100644 --- a/pyrogram/methods/messages/get_discussion_replies_count.py +++ b/pyrogram/methods/messages/get_discussion_replies_count.py @@ -40,7 +40,7 @@ class GetDiscussionRepliesCount: Example: .. code-block:: python - count = app.get_discussion_replies_count(chat_id, message_id) + count = await app.get_discussion_replies_count(chat_id, message_id) """ r = await self.invoke( diff --git a/pyrogram/methods/messages/get_messages.py b/pyrogram/methods/messages/get_messages.py index a6a361af..8db24dd9 100644 --- a/pyrogram/methods/messages/get_messages.py +++ b/pyrogram/methods/messages/get_messages.py @@ -71,19 +71,19 @@ class GetMessages: .. code-block:: python # Get one message - app.get_messages(chat_id, 12345) + await app.get_messages(chat_id, 12345) # Get more than one message (list of messages) - app.get_messages(chat_id, [12345, 12346]) + await app.get_messages(chat_id, [12345, 12346]) # Get message by ignoring any replied-to message - app.get_messages(chat_id, message_id, replies=0) + await app.get_messages(chat_id, message_id, replies=0) # Get message with all chained replied-to messages - app.get_messages(chat_id, message_id, replies=-1) + await app.get_messages(chat_id, message_id, replies=-1) # Get the replied-to message of a message - app.get_messages(chat_id, reply_to_message_ids=message_id) + await app.get_messages(chat_id, reply_to_message_ids=message_id) Raises: ValueError: In case of invalid arguments. diff --git a/pyrogram/methods/messages/read_chat_history.py b/pyrogram/methods/messages/read_chat_history.py index caa228fd..8a58fa26 100644 --- a/pyrogram/methods/messages/read_chat_history.py +++ b/pyrogram/methods/messages/read_chat_history.py @@ -47,10 +47,10 @@ class ReadChatHistory: .. code-block:: python # Mark the whole chat as read - app.read_history(chat_id) + await app.read_history(chat_id) # Mark messages as read only up to the given message id - app.read_history(chat_id, 123456) + await app.read_history(chat_id, 12345) """ peer = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/messages/retract_vote.py b/pyrogram/methods/messages/retract_vote.py index 8aab69f0..8420a4a3 100644 --- a/pyrogram/methods/messages/retract_vote.py +++ b/pyrogram/methods/messages/retract_vote.py @@ -46,7 +46,7 @@ class RetractVote: Example: .. code-block:: python - app.retract_vote(chat_id, message_id) + await app.retract_vote(chat_id, message_id) """ r = await self.invoke( raw.functions.messages.SendVote( diff --git a/pyrogram/methods/messages/search_global.py b/pyrogram/methods/messages/search_global.py index b53dbf32..1a65702e 100644 --- a/pyrogram/methods/messages/search_global.py +++ b/pyrogram/methods/messages/search_global.py @@ -59,12 +59,14 @@ class SearchGlobal: Example: .. code-block:: python + from pyrogram import enums + # Search for "pyrogram". Get the first 50 results - for message in app.search_global("pyrogram", limit=50): + async for message in app.search_global("pyrogram", limit=50): print(message.text) # Search for recent photos from Global. Get the first 20 results - for message in app.search_global(filter="photo", limit=20): + async for message in app.search_global(filter=enums.MessagesFilter.PHOTO, limit=20): print(message.photo) """ current = 0 diff --git a/pyrogram/methods/messages/search_messages.py b/pyrogram/methods/messages/search_messages.py index 0e31871c..1f9fa3c7 100644 --- a/pyrogram/methods/messages/search_messages.py +++ b/pyrogram/methods/messages/search_messages.py @@ -108,15 +108,15 @@ class SearchMessages: from pyrogram import enums # Search for text messages in chat. Get the last 120 results - for message in app.search_messages(chat_id, query="hello", limit=120): + async for message in app.search_messages(chat_id, query="hello", limit=120): print(message.text) # Search for pinned messages in chat - for message in app.search_messages(chat_id, filter=enums.MessagesFilter.PINNED): + async for message in app.search_messages(chat_id, filter=enums.MessagesFilter.PINNED): print(message.text) # Search for messages containing "hello" sent by yourself in chat - for message in app.search_messages(chat, "hello", from_user="me"): + async for message in app.search_messages(chat, "hello", from_user="me"): print(message.text) """ diff --git a/pyrogram/methods/messages/send_animation.py b/pyrogram/methods/messages/send_animation.py index e45a0714..efa9cb16 100644 --- a/pyrogram/methods/messages/send_animation.py +++ b/pyrogram/methods/messages/send_animation.py @@ -153,19 +153,19 @@ class SendAnimation: .. code-block:: python # Send animation by uploading from local file - app.send_animation("me", "animation.gif") + await app.send_animation("me", "animation.gif") # Add caption to the animation - app.send_animation("me", "animation.gif", caption="animation caption") + await app.send_animation("me", "animation.gif", caption="animation caption") # Unsave the animation once is sent - app.send_animation("me", "animation.gif", unsave=True) + await app.send_animation("me", "animation.gif", unsave=True) # Keep track of the progress while uploading - def progress(current, total): + async def progress(current, total): print(f"{current * 100 / total:.1f}%") - app.send_animation("me", "animation.gif", progress=progress) + await app.send_animation("me", "animation.gif", progress=progress) """ file = None diff --git a/pyrogram/methods/messages/send_audio.py b/pyrogram/methods/messages/send_audio.py index 8c2624a8..f4552832 100644 --- a/pyrogram/methods/messages/send_audio.py +++ b/pyrogram/methods/messages/send_audio.py @@ -149,21 +149,21 @@ class SendAudio: .. code-block:: python # Send audio file by uploading from file - app.send_audio("me", "audio.mp3") + await app.send_audio("me", "audio.mp3") # Add caption to the audio - app.send_audio("me", "audio.mp3", caption="audio caption") + await app.send_audio("me", "audio.mp3", caption="audio caption") # Set audio metadata - app.send_audio( + await app.send_audio( "me", "audio.mp3", title="Title", performer="Performer", duration=234) # Keep track of the progress while uploading - def progress(current, total): + async def progress(current, total): print(f"{current * 100 / total:.1f}%") - app.send_audio("me", "audio.mp3", progress=progress) + await app.send_audio("me", "audio.mp3", progress=progress) """ file = None diff --git a/pyrogram/methods/messages/send_cached_media.py b/pyrogram/methods/messages/send_cached_media.py index 88a5f309..f0e04d72 100644 --- a/pyrogram/methods/messages/send_cached_media.py +++ b/pyrogram/methods/messages/send_cached_media.py @@ -93,7 +93,7 @@ class SendCachedMedia: Example: .. code-block:: python - app.send_cached_media("me", file_id) + await app.send_cached_media("me", file_id) """ r = await self.invoke( diff --git a/pyrogram/methods/messages/send_chat_action.py b/pyrogram/methods/messages/send_chat_action.py index 39f8d85f..15b5ac08 100644 --- a/pyrogram/methods/messages/send_chat_action.py +++ b/pyrogram/methods/messages/send_chat_action.py @@ -19,14 +19,14 @@ from typing import Union import pyrogram -from pyrogram import raw +from pyrogram import raw, enums class SendChatAction: async def send_chat_action( self: "pyrogram.Client", chat_id: Union[int, str], - action: "pyrogram.enums.ChatAction" + action: "enums.ChatAction" ) -> bool: """Tell the other party that something is happening on your side. @@ -51,16 +51,16 @@ class SendChatAction: from pyrogram import enums # Send "typing" chat action - app.send_chat_action(chat_id, enums.ChatAction.TYPING) + await app.send_chat_action(chat_id, enums.ChatAction.TYPING) # Send "upload_video" chat action - app.send_chat_action(chat_id, enums.ChatAction.UPLOAD_VIDEO) + await app.send_chat_action(chat_id, enums.ChatAction.UPLOAD_VIDEO) # Send "playing" chat action - app.send_chat_action(chat_id, enums.ChatAction.PLAYING) + await app.send_chat_action(chat_id, enums.ChatAction.PLAYING) # Cancel any current chat action - app.send_chat_action(chat_id, enums.ChatAction.CANCEL) + await app.send_chat_action(chat_id, enums.ChatAction.CANCEL) """ action_name = action.name.lower() diff --git a/pyrogram/methods/messages/send_contact.py b/pyrogram/methods/messages/send_contact.py index b4683338..cedcd0b2 100644 --- a/pyrogram/methods/messages/send_contact.py +++ b/pyrogram/methods/messages/send_contact.py @@ -86,7 +86,7 @@ class SendContact: Example: .. code-block:: python - app.send_contact("me", "+1-123-456-7890", "Name") + await app.send_contact("me", "+1-123-456-7890", "Name") """ r = await self.invoke( raw.functions.messages.SendMedia( diff --git a/pyrogram/methods/messages/send_dice.py b/pyrogram/methods/messages/send_dice.py index b15cfe2d..942f681f 100644 --- a/pyrogram/methods/messages/send_dice.py +++ b/pyrogram/methods/messages/send_dice.py @@ -79,13 +79,13 @@ class SendDice: .. code-block:: python # Send a dice - app.send_dice(chat_id) + await app.send_dice(chat_id) # Send a dart - app.send_dice(chat_id, "🎯") + await app.send_dice(chat_id, "🎯") # Send a basketball - app.send_dice(chat_id, "🏀") + await app.send_dice(chat_id, "🏀") """ r = await self.invoke( diff --git a/pyrogram/methods/messages/send_document.py b/pyrogram/methods/messages/send_document.py index c1179cb0..99c90fbb 100644 --- a/pyrogram/methods/messages/send_document.py +++ b/pyrogram/methods/messages/send_document.py @@ -141,16 +141,16 @@ class SendDocument: .. code-block:: python # Send document by uploading from local file - app.send_document("me", "document.zip") + await app.send_document("me", "document.zip") # Add caption to the document file - app.send_document("me", "document.zip", caption="document caption") + await app.send_document("me", "document.zip", caption="document caption") # Keep track of the progress while uploading - def progress(current, total): + async def progress(current, total): print(f"{current * 100 / total:.1f}%") - app.send_document("me", "document.zip", progress=progress) + await app.send_document("me", "document.zip", progress=progress) """ file = None diff --git a/pyrogram/methods/messages/send_location.py b/pyrogram/methods/messages/send_location.py index 1364543b..fe7d1ed1 100644 --- a/pyrogram/methods/messages/send_location.py +++ b/pyrogram/methods/messages/send_location.py @@ -78,7 +78,7 @@ class SendLocation: Example: .. code-block:: python - app.send_location("me", 51.500729, -0.124583) + app.send_location("me", latitude, longitude) """ r = await self.invoke( raw.functions.messages.SendMedia( diff --git a/pyrogram/methods/messages/send_media_group.py b/pyrogram/methods/messages/send_media_group.py index 382b2a92..43eb9d7c 100644 --- a/pyrogram/methods/messages/send_media_group.py +++ b/pyrogram/methods/messages/send_media_group.py @@ -79,7 +79,7 @@ class SendMediaGroup: from pyrogram.types import InputMediaPhoto, InputMediaVideo - app.send_media_group( + await app.send_media_group( "me", [ InputMediaPhoto("photo1.jpg"), diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py index 414f7447..0a7ab6d1 100644 --- a/pyrogram/methods/messages/send_message.py +++ b/pyrogram/methods/messages/send_message.py @@ -88,31 +88,29 @@ class SendMessage: .. code-block:: python # Simple example - app.send_message("me", "Message sent with **Pyrogram**!") + await app.send_message("me", "Message sent with **Pyrogram**!") # Disable web page previews - app.send_message("me", "https://docs.pyrogram.org", disable_web_page_preview=True) + await app.send_message("me", "https://docs.pyrogram.org", + disable_web_page_preview=True) # Reply to a message using its id - app.send_message("me", "this is a reply", reply_to_message_id=12345) + await app.send_message("me", "this is a reply", reply_to_message_id=123) - # Force HTML-only styles for this request only - app.send_message("me", "**not bold**, italic", parse_mode="html") + .. code-block:: python - ## # For bots only, send messages with keyboards attached - ## from pyrogram.types import ( ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton) # Send a normal keyboard - app.send_message( + await app.send_message( chat_id, "Look at that button!", reply_markup=ReplyKeyboardMarkup([["Nice!"]])) # Send an inline keyboard - app.send_message( + await app.send_message( chat_id, "These are inline buttons", reply_markup=InlineKeyboardMarkup( [ diff --git a/pyrogram/methods/messages/send_photo.py b/pyrogram/methods/messages/send_photo.py index 5391b2f6..85d6fe4b 100644 --- a/pyrogram/methods/messages/send_photo.py +++ b/pyrogram/methods/messages/send_photo.py @@ -128,16 +128,16 @@ class SendPhoto: .. code-block:: python # Send photo by uploading from local file - app.send_photo("me", "photo.jpg") + await app.send_photo("me", "photo.jpg") # Send photo by uploading from URL - app.send_photo("me", "https://i.imgur.com/BQBTP7d.png") + await app.send_photo("me", "https://example.com/example.jpg) # Add caption to a photo - app.send_photo("me", "photo.jpg", caption="Holidays!") + await app.send_photo("me", "photo.jpg", caption="Caption") # Send self-destructing photo - app.send_photo("me", "photo.jpg", ttl_seconds=10) + await app.send_photo("me", "photo.jpg", ttl_seconds=10) """ file = None diff --git a/pyrogram/methods/messages/send_poll.py b/pyrogram/methods/messages/send_poll.py index 0dd877b9..1305d2d6 100644 --- a/pyrogram/methods/messages/send_poll.py +++ b/pyrogram/methods/messages/send_poll.py @@ -98,7 +98,7 @@ class SendPoll: Example: .. code-block:: python - app.send_poll(chat_id, "Is this a poll question?", ["Yes", "No", "Maybe"]) + await app.send_poll(chat_id, "Is this a poll question?", ["Yes", "No", "Maybe"]) """ r = await self.invoke( raw.functions.messages.SendMedia( diff --git a/pyrogram/methods/messages/send_reaction.py b/pyrogram/methods/messages/send_reaction.py index 34c2d92c..7f597cb6 100644 --- a/pyrogram/methods/messages/send_reaction.py +++ b/pyrogram/methods/messages/send_reaction.py @@ -49,10 +49,10 @@ class SendReaction: .. code-block:: python # Send a reaction - app.send_reaction(chat_id, message_id, "🔥") + await app.send_reaction(chat_id, message_id, "🔥") # Retract a reaction - app.send_reaction(chat_id, message_id) + await app.send_reaction(chat_id, message_id) """ await self.invoke( raw.functions.messages.SendReaction( diff --git a/pyrogram/methods/messages/send_sticker.py b/pyrogram/methods/messages/send_sticker.py index d0b66fb1..2fc4565c 100644 --- a/pyrogram/methods/messages/send_sticker.py +++ b/pyrogram/methods/messages/send_sticker.py @@ -111,10 +111,10 @@ class SendSticker: .. code-block:: python # Send sticker by uploading from local file - app.send_sticker("me", "sticker.webp") + await app.send_sticker("me", "sticker.webp") # Send sticker using file_id - app.send_sticker("me", file_id) + await app.send_sticker("me", file_id) """ file = None diff --git a/pyrogram/methods/messages/send_venue.py b/pyrogram/methods/messages/send_venue.py index 7ddb98ef..e8cc760b 100644 --- a/pyrogram/methods/messages/send_venue.py +++ b/pyrogram/methods/messages/send_venue.py @@ -96,8 +96,8 @@ class SendVenue: .. code-block:: python app.send_venue( - "me", 51.500729, -0.124583, - "Elizabeth Tower", "Westminster, London SW1A 0AA, UK") + "me", latitude, longitude, + "Venue title", "Venue address") """ r = await self.invoke( raw.functions.messages.SendMedia( diff --git a/pyrogram/methods/messages/send_video.py b/pyrogram/methods/messages/send_video.py index 49674076..299f1b47 100644 --- a/pyrogram/methods/messages/send_video.py +++ b/pyrogram/methods/messages/send_video.py @@ -158,19 +158,19 @@ class SendVideo: .. code-block:: python # Send video by uploading from local file - app.send_video("me", "video.mp4") + await app.send_video("me", "video.mp4") # Add caption to the video - app.send_video("me", "video.mp4", caption="video caption") + await app.send_video("me", "video.mp4", caption="video caption") # Send self-destructing video - app.send_video("me", "video.mp4", ttl_seconds=10) + await app.send_video("me", "video.mp4", ttl_seconds=10) # Keep track of the progress while uploading - def progress(current, total): + async def progress(current, total): print(f"{current * 100 / total:.1f}%") - app.send_video("me", "video.mp4", progress=progress) + await app.send_video("me", "video.mp4", progress=progress) """ file = None diff --git a/pyrogram/methods/messages/send_video_note.py b/pyrogram/methods/messages/send_video_note.py index ce6240d9..bf33cdb7 100644 --- a/pyrogram/methods/messages/send_video_note.py +++ b/pyrogram/methods/messages/send_video_note.py @@ -125,10 +125,10 @@ class SendVideoNote: .. code-block:: python # Send video note by uploading from local file - app.send_video_note("me", "video_note.mp4") + await app.send_video_note("me", "video_note.mp4") # Set video note length - app.send_video_note("me", "video_note.mp4", length=25) + await app.send_video_note("me", "video_note.mp4", length=25) """ file = None diff --git a/pyrogram/methods/messages/send_voice.py b/pyrogram/methods/messages/send_voice.py index 9bc64565..5947ecc2 100644 --- a/pyrogram/methods/messages/send_voice.py +++ b/pyrogram/methods/messages/send_voice.py @@ -127,13 +127,13 @@ class SendVoice: .. code-block:: python # Send voice note by uploading from local file - app.send_voice("me", "voice.ogg") + await app.send_voice("me", "voice.ogg") # Add caption to the voice note - app.send_voice("me", "voice.ogg", caption="voice caption") + await app.send_voice("me", "voice.ogg", caption="voice caption") # Set voice note duration - app.send_voice("me", "voice.ogg", duration=20) + await app.send_voice("me", "voice.ogg", duration=20) """ file = None diff --git a/pyrogram/methods/messages/stop_poll.py b/pyrogram/methods/messages/stop_poll.py index e642e1b6..6104403f 100644 --- a/pyrogram/methods/messages/stop_poll.py +++ b/pyrogram/methods/messages/stop_poll.py @@ -52,7 +52,7 @@ class StopPoll: Example: .. code-block:: python - app.stop_poll(chat_id, message_id) + await app.stop_poll(chat_id, message_id) """ poll = (await self.get_messages(chat_id, message_id)).poll diff --git a/pyrogram/methods/messages/vote_poll.py b/pyrogram/methods/messages/vote_poll.py index 3fea2e22..72a912de 100644 --- a/pyrogram/methods/messages/vote_poll.py +++ b/pyrogram/methods/messages/vote_poll.py @@ -50,7 +50,7 @@ class VotePoll: Example: .. code-block:: python - app.vote_poll(chat_id, message_id, 6) + await app.vote_poll(chat_id, message_id, 6) """ poll = (await self.get_messages(chat_id, message_id)).poll diff --git a/pyrogram/methods/password/change_cloud_password.py b/pyrogram/methods/password/change_cloud_password.py index 3f7dee00..c5e8bd27 100644 --- a/pyrogram/methods/password/change_cloud_password.py +++ b/pyrogram/methods/password/change_cloud_password.py @@ -52,10 +52,10 @@ class ChangeCloudPassword: .. code-block:: python # Change password only - app.change_cloud_password("current_password", "new_password") + await app.change_cloud_password("current_password", "new_password") # Change password and hint - app.change_cloud_password("current_password", "new_password", new_hint="hint") + await app.change_cloud_password("current_password", "new_password", new_hint="hint") """ r = await self.invoke(raw.functions.account.GetPassword()) diff --git a/pyrogram/methods/password/enable_cloud_password.py b/pyrogram/methods/password/enable_cloud_password.py index fd8b3dbf..a6533dbf 100644 --- a/pyrogram/methods/password/enable_cloud_password.py +++ b/pyrogram/methods/password/enable_cloud_password.py @@ -54,13 +54,13 @@ class EnableCloudPassword: .. code-block:: python # Enable password without hint and email - app.enable_cloud_password("password") + await app.enable_cloud_password("password") # Enable password with hint and without email - app.enable_cloud_password("password", hint="hint") + await app.enable_cloud_password("password", hint="hint") # Enable password with hint and email - app.enable_cloud_password("password", hint="hint", email="user@email.com") + await app.enable_cloud_password("password", hint="hint", email="user@email.com") """ r = await self.invoke(raw.functions.account.GetPassword()) diff --git a/pyrogram/methods/password/remove_cloud_password.py b/pyrogram/methods/password/remove_cloud_password.py index 845547d6..dab37d86 100644 --- a/pyrogram/methods/password/remove_cloud_password.py +++ b/pyrogram/methods/password/remove_cloud_password.py @@ -41,7 +41,7 @@ class RemoveCloudPassword: Example: .. code-block:: python - app.remove_cloud_password("password") + await app.remove_cloud_password("password") """ r = await self.invoke(raw.functions.account.GetPassword()) diff --git a/pyrogram/methods/users/block_user.py b/pyrogram/methods/users/block_user.py index 3298e601..f1447955 100644 --- a/pyrogram/methods/users/block_user.py +++ b/pyrogram/methods/users/block_user.py @@ -41,7 +41,7 @@ class BlockUser: Example: .. code-block:: python - app.block_user(user_id) + await app.block_user(user_id) """ return bool( await self.invoke( diff --git a/pyrogram/methods/users/delete_profile_photos.py b/pyrogram/methods/users/delete_profile_photos.py index 107f11a6..e7205661 100644 --- a/pyrogram/methods/users/delete_profile_photos.py +++ b/pyrogram/methods/users/delete_profile_photos.py @@ -43,13 +43,13 @@ class DeleteProfilePhotos: .. code-block:: python # Get the photos to be deleted - photos = app.get_profile_photos("me") + photos = await app.get_profile_photos("me") # Delete one photo - app.delete_profile_photos(photos[0].file_id) + await app.delete_profile_photos(photos[0].file_id) # Delete the rest of the photos - app.delete_profile_photos([p.file_id for p in photos[1:]]) + await app.delete_profile_photos([p.file_id for p in photos[1:]]) """ photo_ids = photo_ids if isinstance(photo_ids, list) else [photo_ids] input_photos = [utils.get_input_media_from_file_id(i, FileType.PHOTO).id for i in photo_ids] diff --git a/pyrogram/methods/users/get_common_chats.py b/pyrogram/methods/users/get_common_chats.py index 6c7d5ce7..7269a81e 100644 --- a/pyrogram/methods/users/get_common_chats.py +++ b/pyrogram/methods/users/get_common_chats.py @@ -45,7 +45,7 @@ class GetCommonChats: Example: .. code-block:: python - common = app.get_common_chats(user_id) + common = await app.get_common_chats(user_id) print(common) """ diff --git a/pyrogram/methods/users/get_me.py b/pyrogram/methods/users/get_me.py index 2869f85c..610a11af 100644 --- a/pyrogram/methods/users/get_me.py +++ b/pyrogram/methods/users/get_me.py @@ -33,7 +33,7 @@ class GetMe: Example: .. code-block:: python - me = app.get_me() + me = await app.get_me() print(me) """ r = await self.invoke( diff --git a/pyrogram/methods/users/get_profile_photos.py b/pyrogram/methods/users/get_profile_photos.py index ec35aa9e..ba20c89c 100644 --- a/pyrogram/methods/users/get_profile_photos.py +++ b/pyrogram/methods/users/get_profile_photos.py @@ -54,13 +54,13 @@ class GetProfilePhotos: .. code-block:: python # Get the first 100 profile photos of a user - app.get_profile_photos("me") + await app.get_profile_photos("me") # Get only the first profile photo of a user - app.get_profile_photos("me", limit=1) + await app.get_profile_photos("me", limit=1) # Get 3 profile photos of a user, skip the first 5 - app.get_profile_photos("me", limit=3, offset=5) + await app.get_profile_photos("me", limit=3, offset=5) """ peer_id = await self.resolve_peer(chat_id) diff --git a/pyrogram/methods/users/get_profile_photos_count.py b/pyrogram/methods/users/get_profile_photos_count.py index 41e50f5e..4c0fe5d5 100644 --- a/pyrogram/methods/users/get_profile_photos_count.py +++ b/pyrogram/methods/users/get_profile_photos_count.py @@ -41,7 +41,7 @@ class GetProfilePhotosCount: Example: .. code-block:: python - count = app.get_profile_photos_count("me") + count = await app.get_profile_photos_count("me") print(count) """ diff --git a/pyrogram/methods/users/get_users.py b/pyrogram/methods/users/get_users.py index 6f085c4f..caef7966 100644 --- a/pyrogram/methods/users/get_users.py +++ b/pyrogram/methods/users/get_users.py @@ -47,10 +47,10 @@ class GetUsers: .. code-block:: python # Get information about one user - app.get_users("me") + await app.get_users("me") # Get information about multiple users at once - app.get_users([user1, user2, user3]) + await app.get_users([user_id1, user_id2, user_id3]) """ is_iterable = not isinstance(user_ids, (int, str)) user_ids = list(user_ids) if is_iterable else [user_ids] diff --git a/pyrogram/methods/users/iter_profile_photos.py b/pyrogram/methods/users/iter_profile_photos.py index 88c02c3a..6665bc9b 100644 --- a/pyrogram/methods/users/iter_profile_photos.py +++ b/pyrogram/methods/users/iter_profile_photos.py @@ -54,8 +54,8 @@ class IterProfilePhotos: Example: .. code-block:: python - for photo in app.iter_profile_photos("me"): - print(photo.file_id) + async for photo in app.iter_profile_photos("me"): + print(photo) """ current = 0 total = limit or (1 << 31) diff --git a/pyrogram/methods/users/set_profile_photo.py b/pyrogram/methods/users/set_profile_photo.py index a7d59092..86aaf31f 100644 --- a/pyrogram/methods/users/set_profile_photo.py +++ b/pyrogram/methods/users/set_profile_photo.py @@ -57,10 +57,10 @@ class SetProfilePhoto: .. code-block:: python # Set a new profile photo - app.set_profile_photo(photo="new_photo.jpg") + await app.set_profile_photo(photo="new_photo.jpg") # Set a new profile video - app.set_profile_photo(video="new_video.mp4") + await app.set_profile_photo(video="new_video.mp4") """ return bool( diff --git a/pyrogram/methods/users/set_username.py b/pyrogram/methods/users/set_username.py index 68e443f1..6f070bc5 100644 --- a/pyrogram/methods/users/set_username.py +++ b/pyrogram/methods/users/set_username.py @@ -43,7 +43,7 @@ class SetUsername: Example: .. code-block:: python - app.set_username("new_username") + await app.set_username("new_username") """ return bool( diff --git a/pyrogram/methods/users/unblock_user.py b/pyrogram/methods/users/unblock_user.py index 75930658..4809aa6b 100644 --- a/pyrogram/methods/users/unblock_user.py +++ b/pyrogram/methods/users/unblock_user.py @@ -41,7 +41,7 @@ class UnblockUser: Example: .. code-block:: python - app.unblock_user(user_id) + await app.unblock_user(user_id) """ return bool( await self.invoke( diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py index 779aa6cf..31f12508 100644 --- a/pyrogram/methods/users/update_profile.py +++ b/pyrogram/methods/users/update_profile.py @@ -50,13 +50,13 @@ class UpdateProfile: .. code-block:: python # Update your first name only - app.update_profile(first_name="Pyrogram") + await app.update_profile(first_name="Pyrogram") # Update first name and bio - app.update_profile(first_name="Pyrogram", bio="https://docs.pyrogram.org/") + await app.update_profile(first_name="Pyrogram", bio="https://docs.pyrogram.org/") # Remove the last name - app.update_profile(last_name="") + await app.update_profile(last_name="") """ return bool( diff --git a/pyrogram/methods/utilities/add_handler.py b/pyrogram/methods/utilities/add_handler.py index 12b41bfe..136647d9 100644 --- a/pyrogram/methods/utilities/add_handler.py +++ b/pyrogram/methods/utilities/add_handler.py @@ -50,12 +50,12 @@ class AddHandler: from pyrogram import Client from pyrogram.handlers import MessageHandler - def dump(client, message): + async def hello(client, message): print(message) app = Client("my_account") - app.add_handler(MessageHandler(dump)) + app.add_handler(MessageHandler(hello)) app.run() """ diff --git a/pyrogram/methods/utilities/export_session_string.py b/pyrogram/methods/utilities/export_session_string.py index 8177c456..6529484d 100644 --- a/pyrogram/methods/utilities/export_session_string.py +++ b/pyrogram/methods/utilities/export_session_string.py @@ -35,11 +35,6 @@ class ExportSessionString: Example: .. code-block:: python - from pyrogram import Client - - app = Client("my_account") - - with app: - print(app.export_session_string()) + s = await app.export_session_string() """ return await self.storage.export_session_string() diff --git a/pyrogram/methods/utilities/idle.py b/pyrogram/methods/utilities/idle.py index c708d6f1..bcd685e0 100644 --- a/pyrogram/methods/utilities/idle.py +++ b/pyrogram/methods/utilities/idle.py @@ -41,33 +41,35 @@ async def idle(): It is useful for event-driven application only, that are, applications which react upon incoming Telegram updates through handlers, rather than executing a set of methods sequentially. - The way Pyrogram works, it will keep your handlers in a pool of worker threads, which are executed concurrently - outside the main thread; calling idle() will ensure the client(s) will be kept alive by not letting the main - script to end, until you decide to quit. - Once a signal is received (e.g.: from CTRL+C) the function will terminate and your main script will continue. Don't forget to call :meth:`~pyrogram.Client.stop` for each running client before the script ends. Example: .. code-block:: python + import asyncio from pyrogram import Client, idle - app1 = Client("account1") - app2 = Client("account2") - app3 = Client("account3") - ... # Set handlers up + async def main(): + apps = [ + Client("account1"), + Client("account2"), + Client("account3") + ] - app1.start() - app2.start() - app3.start() + ... # Set up handlers - idle() + for app in apps: + await app.start() - app1.stop() - app2.stop() - app3.stop() + await idle() + + for app in apps: + await app.stop() + + + asyncio.run(main()) """ global is_idling diff --git a/pyrogram/methods/utilities/remove_handler.py b/pyrogram/methods/utilities/remove_handler.py index fca4a879..9f1c974e 100644 --- a/pyrogram/methods/utilities/remove_handler.py +++ b/pyrogram/methods/utilities/remove_handler.py @@ -45,12 +45,12 @@ class RemoveHandler: from pyrogram import Client from pyrogram.handlers import MessageHandler - def dump(client, message): + async def hello(client, message): print(message) app = Client("my_account") - handler = app.add_handler(MessageHandler(dump)) + handler = app.add_handler(MessageHandler(hello)) # Starred expression to unpack (handler, group) app.remove_handler(*handler) diff --git a/pyrogram/methods/utilities/restart.py b/pyrogram/methods/utilities/restart.py index 66c24608..44fd6745 100644 --- a/pyrogram/methods/utilities/restart.py +++ b/pyrogram/methods/utilities/restart.py @@ -32,7 +32,7 @@ class Restart: Parameters: block (``bool``, *optional*): Blocks the code execution until the client has been restarted. It is useful with ``block=False`` in case - you want to restart the own client *within* an handler in order not to cause a deadlock. + you want to restart the own client within an handler in order not to cause a deadlock. Defaults to True. Returns: @@ -47,15 +47,17 @@ class Restart: from pyrogram import Client app = Client("my_account") - app.start() - ... # Call API methods - app.restart() + async def main(): + await app.start() + ... # Invoke API methods + await app.restart() + ... # Invoke other API methods + await app.stop() - ... # Call other API methods - app.stop() + app.run(main()) """ async def do_it(): diff --git a/pyrogram/methods/utilities/run.py b/pyrogram/methods/utilities/run.py index 6247b936..e3890355 100644 --- a/pyrogram/methods/utilities/run.py +++ b/pyrogram/methods/utilities/run.py @@ -30,8 +30,17 @@ class Run: ): """Start the client, idle the main script and finally stop the client. - This is a convenience method that calls :meth:`~pyrogram.Client.start`, :meth:`~pyrogram.idle` and - :meth:`~pyrogram.Client.stop` in sequence. It makes running a single client less verbose. + When calling this method without any argument it acts as a convenience method that calls + :meth:`~pyrogram.Client.start`, :meth:`~pyrogram.idle` and :meth:`~pyrogram.Client.stop` in sequence. + It makes running a single client less verbose. + + In case a coroutine is passed as argument, runs the coroutine until it's completed and doesn't do any client + operation. This is almost the same as :py:obj:`asyncio.run` except for the fact that Pyrogram's ``run`` uses the + current event loop instead of a new one. + + Parameters: + coroutine (``Coroutine``, *optional*): + Pass a coroutine to run it until it completes. Raises: ConnectionError: In case you try to run an already started client. @@ -42,10 +51,22 @@ class Run: from pyrogram import Client app = Client("my_account") - ... # Set handlers up - app.run() + + .. code-block:: python + + from pyrogram import Client + + app = Client("my_account") + + + async def main(): + async with app: + print(await app.get_me()) + + + app.run(main()) """ loop = asyncio.get_event_loop() run = loop.run_until_complete diff --git a/pyrogram/methods/utilities/start.py b/pyrogram/methods/utilities/start.py index ab4aef82..bf5468fb 100644 --- a/pyrogram/methods/utilities/start.py +++ b/pyrogram/methods/utilities/start.py @@ -30,7 +30,7 @@ class Start: ): """Start the client. - This method connects the client to Telegram and, in case of new sessions, automatically manages the full + This method connects the client to Telegram and, in case of new sessions, automatically manages the authorization process using an interactive prompt. Returns: @@ -45,11 +45,15 @@ class Start: from pyrogram import Client app = Client("my_account") - app.start() - ... # Call API methods - app.stop() + async def main(): + await app.start() + ... # Invoke API methods + await app.stop() + + + app.run(main()) """ is_authorized = await self.connect() diff --git a/pyrogram/methods/utilities/stop.py b/pyrogram/methods/utilities/stop.py index 92b99fc5..1b28add0 100644 --- a/pyrogram/methods/utilities/stop.py +++ b/pyrogram/methods/utilities/stop.py @@ -46,11 +46,15 @@ class Stop: from pyrogram import Client app = Client("my_account") - app.start() - ... # Call API methods - app.stop() + async def main(): + await app.start() + ... # Invoke API methods + await app.stop() + + + app.run(main()) """ async def do_it(): diff --git a/pyrogram/methods/utilities/stop_transmission.py b/pyrogram/methods/utilities/stop_transmission.py index 0639eab8..da645695 100644 --- a/pyrogram/methods/utilities/stop_transmission.py +++ b/pyrogram/methods/utilities/stop_transmission.py @@ -29,17 +29,15 @@ class StopTransmission: Example: .. code-block:: python - from pyrogram import Client - - app = Client("my_account") - - # Example to stop transmission once the upload progress reaches 50% - # Useless in practice, but shows how to stop on command - def progress(current, total, client): + # Stop transmission once the upload progress reaches 50% + async def progress(current, total, client): if (current * 100 / total) > 50: client.stop_transmission() - with app: - app.send_document("me", "file.zip", progress=progress, progress_args=(app,)) + async with app: + await app.send_document( + "me", "file.zip", + progress=progress, + progress_args=(app,)) """ raise pyrogram.StopTransmission diff --git a/pyrogram/types/bots_and_keyboards/callback_query.py b/pyrogram/types/bots_and_keyboards/callback_query.py index 436b6076..8c96d6cc 100644 --- a/pyrogram/types/bots_and_keyboards/callback_query.py +++ b/pyrogram/types/bots_and_keyboards/callback_query.py @@ -131,7 +131,7 @@ class CallbackQuery(Object, Update): .. code-block:: python - client.answer_callback_query( + await client.answer_callback_query( callback_query.id, text="Hello", show_alert=True @@ -140,7 +140,7 @@ class CallbackQuery(Object, Update): Example: .. code-block:: python - callback_query.answer("Hello", show_alert=True) + await callback_query.answer("Hello", show_alert=True) Parameters: text (``str``, *optional*): diff --git a/pyrogram/types/inline_mode/inline_query.py b/pyrogram/types/inline_mode/inline_query.py index 6f671059..a5f0422e 100644 --- a/pyrogram/types/inline_mode/inline_query.py +++ b/pyrogram/types/inline_mode/inline_query.py @@ -122,7 +122,7 @@ class InlineQuery(Object, Update): .. code-block:: python - client.answer_inline_query( + await client.answer_inline_query( inline_query.id, results=[...] ) @@ -130,7 +130,7 @@ class InlineQuery(Object, Update): Example: .. code-block:: python - inline_query.answer([...]) + await inline_query.answer([...]) Parameters: results (List of :obj:`~pyrogram.types.InlineQueryResult`): diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index f7965e6a..0c639e35 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -834,7 +834,7 @@ class Message(Object, Update): .. code-block:: python - client.get_media_group( + await client.get_media_group( chat_id=message.chat.id, message_id=message.id ) @@ -842,7 +842,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.get_media_group() + await message.get_media_group() Returns: List of :obj:`~pyrogram.types.Message`: On success, a list of messages of the media group is returned. @@ -877,7 +877,7 @@ class Message(Object, Update): .. code-block:: python - client.send_message( + await client.send_message( chat_id=message.chat.id, text="hello", reply_to_message_id=message.id @@ -886,7 +886,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_text("hello", quote=True) + await message.reply_text("hello", quote=True) Parameters: text (``str``): @@ -979,7 +979,7 @@ class Message(Object, Update): .. code-block:: python - client.send_animation( + await client.send_animation( chat_id=message.chat.id, animation=animation ) @@ -987,7 +987,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_animation(animation) + await message.reply_animation(animation) Parameters: animation (``str``): @@ -1118,7 +1118,7 @@ class Message(Object, Update): .. code-block:: python - client.send_audio( + await client.send_audio( chat_id=message.chat.id, audio=audio ) @@ -1126,7 +1126,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_audio(audio) + await message.reply_audio(audio) Parameters: audio (``str``): @@ -1251,7 +1251,7 @@ class Message(Object, Update): .. code-block:: python - client.send_cached_media( + await client.send_cached_media( chat_id=message.chat.id, file_id=file_id ) @@ -1259,7 +1259,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_cached_media(file_id) + await message.reply_cached_media(file_id) Parameters: file_id (``str``): @@ -1315,31 +1315,30 @@ class Message(Object, Update): reply_markup=reply_markup ) - async def reply_chat_action(self, action: str) -> bool: + async def reply_chat_action(self, action: "enums.ChatAction") -> bool: """Bound method *reply_chat_action* of :obj:`~pyrogram.types.Message`. Use as a shortcut for: .. code-block:: python - client.send_chat_action( + from pyrogram import enums + + await client.send_chat_action( chat_id=message.chat.id, - action="typing" + action=enums.ChatAction.TYPING ) Example: .. code-block:: python - message.reply_chat_action("typing") + from pyrogram import enums + + await message.reply_chat_action(enums.ChatAction.TYPING) Parameters: - action (``str``): - Type of action to broadcast. Choose one, depending on what the user is about to receive: *"typing"* for - text messages, *"upload_photo"* for photos, *"record_video"* or *"upload_video"* for videos, - *"record_audio"* or *"upload_audio"* for audio files, *"upload_document"* for general files, - *"find_location"* for location data, *"record_video_note"* or *"upload_video_note"* for video notes, - *"choose_contact"* for contacts, *"playing"* for games or *"cancel"* to cancel any chat action currently - displayed. + action (:obj:`~pyrogram.enums.ChatAction`): + Type of action to broadcast. Returns: ``bool``: On success, True is returned. @@ -1375,7 +1374,7 @@ class Message(Object, Update): .. code-block:: python - client.send_contact( + await client.send_contact( chat_id=message.chat.id, phone_number=phone_number, first_name=first_name @@ -1384,7 +1383,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_contact("+1-123-456-7890", "Name") + await message.reply_contact("+1-123-456-7890", "Name") Parameters: phone_number (``str``): @@ -1466,7 +1465,7 @@ class Message(Object, Update): .. code-block:: python - client.send_document( + await client.send_document( chat_id=message.chat.id, document=document ) @@ -1474,7 +1473,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_document(document) + await message.reply_document(document) Parameters: document (``str``): @@ -1599,7 +1598,7 @@ class Message(Object, Update): .. code-block:: python - client.send_game( + await client.send_game( chat_id=message.chat.id, game_short_name="lumberjack" ) @@ -1607,7 +1606,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_game("lumberjack") + await message.reply_game("lumberjack") Parameters: game_short_name (``str``): @@ -1663,7 +1662,7 @@ class Message(Object, Update): .. code-block:: python - client.send_inline_bot_result( + await client.send_inline_bot_result( chat_id=message.chat.id, query_id=query_id, result_id=result_id @@ -1672,7 +1671,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_inline_bot_result(query_id, result_id) + await message.reply_inline_bot_result(query_id, result_id) Parameters: query_id (``int``): @@ -1733,16 +1732,16 @@ class Message(Object, Update): .. code-block:: python - client.send_location( + await client.send_location( chat_id=message.chat.id, - latitude=41.890251, - longitude=12.492373 + latitude=latitude, + longitude=longitude ) Example: .. code-block:: python - message.reply_location(41.890251, 12.492373) + await message.reply_location(latitude, longitude) Parameters: latitude (``float``): @@ -1801,7 +1800,7 @@ class Message(Object, Update): .. code-block:: python - client.send_media_group( + await client.send_media_group( chat_id=message.chat.id, media=list_of_media ) @@ -1809,7 +1808,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_media_group(list_of_media) + await message.reply_media_group(list_of_media) Parameters: media (``list``): @@ -1874,7 +1873,7 @@ class Message(Object, Update): .. code-block:: python - client.send_photo( + await client.send_photo( chat_id=message.chat.id, photo=photo ) @@ -1882,7 +1881,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_photo(photo) + await message.reply_photo(photo) Parameters: photo (``str``): @@ -1997,16 +1996,16 @@ class Message(Object, Update): .. code-block:: python - client.send_poll( + await client.send_poll( chat_id=message.chat.id, - question="Is Pyrogram the best?", - options=["Yes", "Yes"] + question="This is a poll", + options=["A", "B", "C] ) Example: .. code-block:: python - message.reply_poll("Is Pyrogram the best?", ["Yes", "Yes"]) + await message.reply_poll("This is a poll", ["A", "B", "C"]) Parameters: question (``str``): @@ -2097,7 +2096,7 @@ class Message(Object, Update): .. code-block:: python - client.send_sticker( + await client.send_sticker( chat_id=message.chat.id, sticker=sticker ) @@ -2105,7 +2104,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_sticker(sticker) + await message.reply_sticker(sticker) Parameters: sticker (``str``): @@ -2200,18 +2199,18 @@ class Message(Object, Update): .. code-block:: python - client.send_venue( + await client.send_venue( chat_id=message.chat.id, - latitude=41.890251, - longitude=12.492373, - title="Coliseum", - address="Piazza del Colosseo, 1, 00184 Roma RM" + latitude=latitude, + longitude=longitude, + title="Venue title", + address="Venue address" ) Example: .. code-block:: python - message.reply_venue(41.890251, 12.492373, "Coliseum", "Piazza del Colosseo, 1, 00184 Roma RM") + await message.reply_venue(latitude, longitude, "Venue title", "Venue address") Parameters: latitude (``float``): @@ -2304,7 +2303,7 @@ class Message(Object, Update): .. code-block:: python - client.send_video( + await client.send_video( chat_id=message.chat.id, video=video ) @@ -2312,7 +2311,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_video(video) + await message.reply_video(video) Parameters: video (``str``): @@ -2449,7 +2448,7 @@ class Message(Object, Update): .. code-block:: python - client.send_video_note( + await client.send_video_note( chat_id=message.chat.id, video_note=video_note ) @@ -2457,7 +2456,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_video_note(video_note) + await message.reply_video_note(video_note) Parameters: video_note (``str``): @@ -2568,7 +2567,7 @@ class Message(Object, Update): .. code-block:: python - client.send_voice( + await client.send_voice( chat_id=message.chat.id, voice=voice ) @@ -2576,7 +2575,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.reply_voice(voice) + await message.reply_voice(voice) Parameters: voice (``str``): @@ -2680,7 +2679,7 @@ class Message(Object, Update): .. code-block:: python - client.edit_message_text( + await client.edit_message_text( chat_id=message.chat.id, message_id=message.id, text="hello" @@ -2689,7 +2688,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.edit_text("hello") + await message.edit_text("hello") Parameters: text (``str``): @@ -2739,7 +2738,7 @@ class Message(Object, Update): .. code-block:: python - client.edit_message_caption( + await client.edit_message_caption( chat_id=message.chat.id, message_id=message.id, caption="hello" @@ -2748,7 +2747,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.edit_caption("hello") + await message.edit_caption("hello") Parameters: caption (``str``): @@ -2790,7 +2789,7 @@ class Message(Object, Update): .. code-block:: python - client.edit_message_media( + await client.edit_message_media( chat_id=message.chat.id, message_id=message.id, media=media @@ -2799,7 +2798,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.edit_media(media) + await message.edit_media(media) Parameters: media (:obj:`~pyrogram.types.InputMedia`): @@ -2828,7 +2827,7 @@ class Message(Object, Update): .. code-block:: python - client.edit_message_reply_markup( + await client.edit_message_reply_markup( chat_id=message.chat.id, message_id=message.id, reply_markup=inline_reply_markup @@ -2837,7 +2836,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.edit_reply_markup(inline_reply_markup) + await message.edit_reply_markup(inline_reply_markup) Parameters: reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`): @@ -2868,7 +2867,7 @@ class Message(Object, Update): .. code-block:: python - client.forward_messages( + await client.forward_messages( chat_id=chat_id, from_chat_id=message.chat.id, message_ids=message.id @@ -2877,7 +2876,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.forward(chat_id) + await message.forward(chat_id) Parameters: chat_id (``int`` | ``str``): @@ -2929,7 +2928,7 @@ class Message(Object, Update): .. code-block:: python - client.copy_message( + await client.copy_message( chat_id=chat_id, from_chat_id=message.chat.id, message_id=message.id @@ -2938,7 +2937,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.copy(chat_id) + await message.copy(chat_id) Parameters: chat_id (``int`` | ``str``): @@ -3100,7 +3099,7 @@ class Message(Object, Update): .. code-block:: python - client.delete_messages( + await client.delete_messages( chat_id=chat_id, message_ids=message.id ) @@ -3108,7 +3107,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.delete() + await message.delete() Parameters: revoke (``bool``, *optional*): @@ -3138,7 +3137,7 @@ class Message(Object, Update): .. code-block:: python - client.request_callback_answer( + await client.request_callback_answer( chat_id=message.chat.id, message_id=message.id, callback_data=message.reply_markup[i][j].callback_data @@ -3148,7 +3147,7 @@ class Message(Object, Update): .. code-block:: python - client.send_message( + await client.send_message( chat_id=message.chat.id, text=message.reply_markup[i][j].text ) @@ -3257,7 +3256,7 @@ class Message(Object, Update): .. code-block:: python - client.send_reaction( + await client.send_reaction( chat_id=chat_id, message_id=message.message_id, emoji="🔥" @@ -3266,7 +3265,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.react(emoji="🔥") + await message.react(emoji="🔥") Parameters: emoji (``str``, *optional*): @@ -3330,12 +3329,12 @@ class Message(Object, Update): .. code-block:: python - client.download_media(message) + await lient.download_media(message) Example: .. code-block:: python - message.download() + await message.download() Parameters: file_name (``str``, *optional*): @@ -3430,7 +3429,7 @@ class Message(Object, Update): .. code-block:: python - client.pin_chat_message( + await client.pin_chat_message( chat_id=message.chat.id, message_id=message_id ) @@ -3438,7 +3437,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.pin() + await message.pin() Parameters: disable_notification (``bool``): @@ -3469,7 +3468,7 @@ class Message(Object, Update): .. code-block:: python - client.unpin_chat_message( + await client.unpin_chat_message( chat_id=message.chat.id, message_id=message_id ) @@ -3477,7 +3476,7 @@ class Message(Object, Update): Example: .. code-block:: python - message.unpin() + await message.unpin() Returns: True on success. diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index f48f3c0f..e36107bf 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -17,7 +17,7 @@ # along with Pyrogram. If not, see . from datetime import datetime -from typing import Union, List, Generator, Optional +from typing import Union, List import pyrogram from pyrogram import raw, enums @@ -365,12 +365,12 @@ class Chat(Object): .. code-block:: python - client.archive_chats(-100123456789) + await client.archive_chats(-100123456789) Example: .. code-block:: python - chat.archive() + await chat.archive() Returns: True on success. @@ -388,12 +388,12 @@ class Chat(Object): .. code-block:: python - client.unarchive_chats(-100123456789) + await client.unarchive_chats(-100123456789) Example: .. code-block:: python - chat.unarchive() + await chat.unarchive() Returns: True on success. @@ -412,7 +412,7 @@ class Chat(Object): .. code-block:: python - client.set_chat_title( + await client.set_chat_title( chat_id=chat_id, title=title ) @@ -420,7 +420,7 @@ class Chat(Object): Example: .. code-block:: python - chat.set_title("Lounge") + await chat.set_title("Lounge") Note: In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" @@ -450,7 +450,7 @@ class Chat(Object): .. code-block:: python - client.set_chat_description( + await client.set_chat_description( chat_id=chat_id, description=description ) @@ -458,7 +458,7 @@ class Chat(Object): Example: .. code-block:: python - chat.set_chat_description("Don't spam!") + await chat.set_chat_description("Don't spam!") Parameters: description (``str``): @@ -484,7 +484,7 @@ class Chat(Object): .. code-block:: python - client.set_chat_photo( + await client.set_chat_photo( chat_id=chat_id, photo=photo ) @@ -492,7 +492,7 @@ class Chat(Object): Example: .. code-block:: python - chat.set_photo("photo.png") + await chat.set_photo("photo.png") Parameters: photo (``str``): @@ -522,7 +522,7 @@ class Chat(Object): .. code-block:: python - client.ban_chat_member( + await client.ban_chat_member( chat_id=chat_id, user_id=user_id ) @@ -530,7 +530,7 @@ class Chat(Object): Example: .. code-block:: python - chat.ban_member(123456789) + await chat.ban_member(123456789) Note: In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" setting is @@ -571,7 +571,7 @@ class Chat(Object): .. code-block:: python - client.unban_chat_member( + await client.unban_chat_member( chat_id=chat_id, user_id=user_id ) @@ -579,7 +579,7 @@ class Chat(Object): Example: .. code-block:: python - chat.unban_member(123456789) + await chat.unban_member(123456789) Parameters: user_id (``int`` | ``str``): @@ -610,7 +610,7 @@ class Chat(Object): .. code-block:: python - client.restrict_chat_member( + await client.restrict_chat_member( chat_id=chat_id, user_id=user_id, permissions=ChatPermissions() @@ -619,7 +619,7 @@ class Chat(Object): Example: .. code-block:: python - chat.restrict_member(user_id, ChatPermissions()) + await chat.restrict_member(user_id, ChatPermissions()) Parameters: user_id (``int`` | ``str``): @@ -648,19 +648,12 @@ class Chat(Object): until_date=until_date, ) + # Set None as privileges default due to issues with partially initialized module, because at the time Chat + # is being initialized, ChatPrivileges would be required here, but was not initialized yet. async def promote_member( self, user_id: Union[int, str], - can_manage_chat: bool = True, - can_change_info: bool = True, - can_post_messages: bool = False, - can_edit_messages: bool = False, - can_delete_messages: bool = True, - can_restrict_members: bool = True, - can_invite_users: bool = True, - can_pin_messages: bool = False, - can_promote_members: bool = False, - can_manage_voice_chats: bool = False + privileges: "types.ChatPrivileges" = None ) -> bool: """Bound method *promote_member* of :obj:`~pyrogram.types.Chat`. @@ -668,7 +661,7 @@ class Chat(Object): .. code-block:: python - client.promote_chat_member( + await client.promote_chat_member( chat_id=chat_id, user_id=user_id ) @@ -677,46 +670,15 @@ class Chat(Object): .. code-block:: python - chat.promote_member(123456789) + await chat.promote_member(123456789) Parameters: user_id (``int`` | ``str``): Unique identifier (int) or username (str) of the target user. For a contact that exists in your Telegram address book you can use his phone number (str). - can_manage_chat (``bool``, *optional*): - Pass True, if the administrator can access the chat event log, chat statistics, message statistics - in channels, see channel members, see anonymous administrators in supergroups and ignore slow mode. - Implied by any other administrator privilege. - - can_change_info (``bool``, *optional*): - Pass True, if the administrator can change chat title, photo and other settings. - - can_post_messages (``bool``, *optional*): - Pass True, if the administrator can create channel posts, channels only. - - can_edit_messages (``bool``, *optional*): - Pass True, if the administrator can edit messages of other users and can pin messages, channels only. - - can_delete_messages (``bool``, *optional*): - Pass True, if the administrator can delete messages of other users. - - can_restrict_members (``bool``, *optional*): - Pass True, if the administrator can restrict, ban or unban chat members. - - can_invite_users (``bool``, *optional*): - Pass True, if the administrator can invite new users to the chat. - - can_pin_messages (``bool``, *optional*): - Pass True, if the administrator can pin messages, supergroups only. - - can_promote_members (``bool``, *optional*): - Pass True, if the administrator can add new administrators with a subset of his own privileges or - demote administrators that he has promoted, directly or indirectly (promoted by administrators that - were appointed by him). - - can_manage_voice_chats (``bool``, *optional*): - Pass True, if the administration can manage voice chats (also called group calls). + privileges (:obj:`~pyrogram.types.ChatPrivileges`, *optional*): + New user privileges. Returns: ``bool``: True on success. @@ -728,16 +690,7 @@ class Chat(Object): return await self._client.promote_chat_member( chat_id=self.id, user_id=user_id, - can_manage_chat=can_manage_chat, - can_change_info=can_change_info, - can_post_messages=can_post_messages, - can_edit_messages=can_edit_messages, - can_delete_messages=can_delete_messages, - can_restrict_members=can_restrict_members, - can_invite_users=can_invite_users, - can_pin_messages=can_pin_messages, - can_promote_members=can_promote_members, - can_manage_voice_chats=can_manage_voice_chats + privileges=privileges ) async def join(self): @@ -747,12 +700,12 @@ class Chat(Object): .. code-block:: python - client.join_chat(123456789) + await client.join_chat(123456789) Example: .. code-block:: python - chat.join() + await chat.join() Note: This only works for public groups, channels that have set a username or linked chats. @@ -773,12 +726,12 @@ class Chat(Object): .. code-block:: python - client.leave_chat(123456789) + await client.leave_chat(123456789) Example: .. code-block:: python - chat.leave() + await chat.leave() Raises: RPCError: In case of a Telegram RPC error. @@ -819,7 +772,7 @@ class Chat(Object): .. code-block:: python - client.get_chat_member( + await client.get_chat_member( chat_id=chat_id, user_id=user_id ) @@ -827,7 +780,7 @@ class Chat(Object): Example: .. code-block:: python - chat.get_member(user_id) + await chat.get_member(user_id) Returns: :obj:`~pyrogram.types.ChatMember`: On success, a chat member is returned. @@ -840,9 +793,8 @@ class Chat(Object): async def get_members( self, - offset: int = 0, - limit: int = 200, query: str = "", + limit: int = 0, filter: "enums.ChatMembersFilter" = enums.ChatMembersFilter.SEARCH ) -> List["types.ChatMember"]: """Bound method *get_members* of :obj:`~pyrogram.types.Chat`. @@ -851,120 +803,38 @@ class Chat(Object): .. code-block:: python - client.get_chat_members(chat_id) - - - Parameters: - offset (``int``, *optional*): - Sequential number of the first member to be returned. - Only applicable to supergroups and channels. Defaults to 0 [1]_. - - limit (``int``, *optional*): - Limits the number of members to be retrieved. - Only applicable to supergroups and channels. - Defaults to 200, which is also the maximum server limit allowed per method call. - - query (``str``, *optional*): - Query string to filter members based on their display names and usernames. - Only applicable to supergroups and channels. Defaults to "" (empty string) [2]_. - - filter (``str``, *optional*): - Filter used to select the kind of members you want to retrieve. Only applicable for supergroups - and channels. It can be any of the followings: - *"all"* - all kind of members, - *"banned"* - banned members only, - *"restricted"* - restricted members only, - *"bots"* - bots only, - *"recent"* - recent members only, - *"administrators"* - chat administrators only. - Only applicable to supergroups and channels. - Defaults to *"recent"*. - - .. [1] Server limit: on supergroups, you can get up to 10,000 members for a single query and up to 200 members - on channels. - - .. [2] A query string is applicable only for *"all"*, *"banned"* and *"restricted"* filters only. + async for member in client.get_chat_members(chat_id): + print(member) Example: .. code-block:: python - # Get first 200 recent members - chat.get_members() - - # Get all administrators - chat.get_members(filter="administrators") - - # Get all bots - chat.get_members(filter="bots") - - Returns: - List of :obj:`~pyrogram.types.ChatMember`: On success, a list of chat members is returned. - """ - - return await self._client.get_chat_members( - self.id, - offset=offset, - limit=limit, - query=query, - filter=filter - ) - - def iter_members( - self, - limit: int = 0, - query: str = "", - filter: str = "all" - ) -> Optional[Generator["types.ChatMember", None, None]]: - """Bound method *iter_members* of :obj:`~pyrogram.types.Chat`. - - Use as a shortcut for: - - .. code-block:: python + async for member in chat.get_members(): + print(member) Parameters: - limit (``int``, *optional*): - Limits the number of members to be retrieved. - Only applicable to supergroups and channels. - Defaults to 200, which is also the maximum server limit allowed per method call [1]_. - query (``str``, *optional*): Query string to filter members based on their display names and usernames. - Only applicable to supergroups and channels. Defaults to "" (empty string) [2]_. + Only applicable to supergroups and channels. Defaults to "" (empty string). + A query string is applicable only for :obj:`~pyrogram.enums.ChatMembersFilter.SEARCH`, + :obj:`~pyrogram.enums.ChatMembersFilter.BANNED` and :obj:`~pyrogram.enums.ChatMembersFilter.RESTRICTED` + filters only. + + limit (``int``, *optional*): + Limits the number of members to be retrieved. filter (:obj:`~pyrogram.enums.ChatMembersFilter`, *optional*): Filter used to select the kind of members you want to retrieve. Only applicable for supergroups and channels. - .. [1] Server limit: on supergroups, you can get up to 10,000 members for a single query and up to 200 members - on channels. - - .. [2] A query string is applicable only for *"all"*, *"banned"* and *"restricted"* filters only. - - Example: - .. code-block:: python - - from pyrogram import enums - - # Get first 200 recent members - for member in chat.get_members(): - print(member.user.first_name) - - # Get all administrators - for member in chat.iter_members(filter=enums.ChatMembersFilter.ADMINISTRATORS): - print(member.user.first_name) - - # Get first 3 bots - for member in chat.iter_members(filter=enums.ChatMembersFilter.BOTS, limit=3): - print(member.user.first_name) - Returns: - ``Generator``: A generator yielding :obj:`~pyrogram.types.ChatMember` objects. + ``Generator``: On success, a generator yielding :obj:`~pyrogram.types.ChatMember` objects is returned. """ - return self._client.iter_chat_members( + return self._client.get_chat_members( self.id, - limit=limit, query=query, + limit=limit, filter=filter ) @@ -979,12 +849,12 @@ class Chat(Object): .. code-block:: python - client.add_chat_members(chat_id, user_id) + await client.add_chat_members(chat_id, user_id) Example: .. code-block:: python - chat.add_members(user_id) + await chat.add_members(user_id) Returns: ``bool``: On success, True is returned. @@ -1003,12 +873,12 @@ class Chat(Object): .. code-block:: python - client.mark_unread(chat_id) + await client.mark_unread(chat_id) Example: .. code-block:: python - chat.mark_unread() + await chat.mark_unread() Returns: ``bool``: On success, True is returned. @@ -1023,7 +893,7 @@ class Chat(Object): .. code-block:: python - client.set_chat_protected_content(chat_id, enabled) + await client.set_chat_protected_content(chat_id, enabled) Parameters: enabled (``bool``): @@ -1032,7 +902,7 @@ class Chat(Object): Example: .. code-block:: python - chat.set_protected_content(enabled) + await chat.set_protected_content(enabled) Returns: ``bool``: On success, True is returned. diff --git a/pyrogram/types/user_and_chats/chat_join_request.py b/pyrogram/types/user_and_chats/chat_join_request.py index ee9da3ef..b810640b 100644 --- a/pyrogram/types/user_and_chats/chat_join_request.py +++ b/pyrogram/types/user_and_chats/chat_join_request.py @@ -89,7 +89,7 @@ class ChatJoinRequest(Object, Update): .. code-block:: python - client.approve_chat_join_request( + await client.approve_chat_join_request( chat_id=request.chat.id, user_id=request.from_user.id ) @@ -97,7 +97,7 @@ class ChatJoinRequest(Object, Update): Example: .. code-block:: python - request.approve() + await request.approve() Returns: ``bool``: True on success. @@ -117,7 +117,7 @@ class ChatJoinRequest(Object, Update): .. code-block:: python - client.decline_chat_join_request( + await client.decline_chat_join_request( chat_id=request.chat.id, user_id=request.from_user.id ) @@ -125,7 +125,7 @@ class ChatJoinRequest(Object, Update): Example: .. code-block:: python - request.decline() + await request.decline() Returns: ``bool``: True on success. diff --git a/pyrogram/types/user_and_chats/user.py b/pyrogram/types/user_and_chats/user.py index 2ec09fb5..ff32e973 100644 --- a/pyrogram/types/user_and_chats/user.py +++ b/pyrogram/types/user_and_chats/user.py @@ -274,12 +274,12 @@ class User(Object, Update): .. code-block:: python - client.archive_chats(123456789) + await client.archive_chats(123456789) Example: .. code-block:: python - user.archive() + await user.archive() Returns: True on success. @@ -297,12 +297,12 @@ class User(Object, Update): .. code-block:: python - client.unarchive_chats(123456789) + await client.unarchive_chats(123456789) Example: .. code-block:: python - user.unarchive() + await user.unarchive() Returns: True on success. @@ -320,12 +320,12 @@ class User(Object, Update): .. code-block:: python - client.block_user(123456789) + await client.block_user(123456789) Example: .. code-block:: python - user.block() + await user.block() Returns: True on success.