From bd121be76d5d7d0fceecb26c7aa6ed32ee44f71c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 13:50:34 +0200 Subject: [PATCH 01/16] Update echo_bot.py --- examples/echo_bot.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index adda52c7..70e26f9d 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -20,9 +20,8 @@ from pyrogram import Client, Filters """This simple echo bot replies to every private text message. -It uses the @on_message decorator to register a MessageHandler -and applies two filters on it, Filters.text and Filters.private to make -sure it will only reply to private text messages. +It uses the @on_message decorator to register a MessageHandler and applies two filters on it: +Filters.text and Filters.private to make sure it will reply to private text messages only. """ app = Client("my_account") @@ -30,10 +29,7 @@ app = Client("my_account") @app.on_message(Filters.text & Filters.private) def echo(client, message): - client.send_message( - message.chat.id, message.text, - reply_to_message_id=message.message_id - ) + message.reply(message.text, quote=True) app.run() # Automatically start() and idle() From 7b76751e8f1037ee7c71de182ba751673cfd90fc Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 13:51:29 +0200 Subject: [PATCH 02/16] Update get_history.py --- examples/get_history.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/get_history.py b/examples/get_history.py index 433d127c..0466fc96 100644 --- a/examples/get_history.py +++ b/examples/get_history.py @@ -33,8 +33,7 @@ app.start() while True: try: m = app.get_history(target, offset_id=offset_id) - except FloodWait as e: - # For very large chats the method call can raise a FloodWait + except FloodWait as e: # For very large chats the method call can raise a FloodWait print("waiting {}".format(e.x)) time.sleep(e.x) # Sleep X seconds before continuing continue From 1be144288cf8bf498015ebe6e087bb2ab32f29f2 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:07:01 +0200 Subject: [PATCH 03/16] Add a proxy to the Client instance inside CallbackQuery objects --- pyrogram/client/dispatcher/dispatcher.py | 2 +- pyrogram/client/ext/utils.py | 8 +++++--- pyrogram/client/types/bots/callback_query.py | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pyrogram/client/dispatcher/dispatcher.py b/pyrogram/client/dispatcher/dispatcher.py index ea10a201..5506cbdd 100644 --- a/pyrogram/client/dispatcher/dispatcher.py +++ b/pyrogram/client/dispatcher/dispatcher.py @@ -205,7 +205,7 @@ class Dispatcher: self.dispatch( pyrogram.Update( callback_query=utils.parse_inline_callback_query( - update, users + self.client, update, users ) ) ) diff --git a/pyrogram/client/ext/utils.py b/pyrogram/client/ext/utils.py index b98a05a8..a497e3c9 100644 --- a/pyrogram/client/ext/utils.py +++ b/pyrogram/client/ext/utils.py @@ -865,11 +865,12 @@ def parse_callback_query(client, callback_query, users): message=client.get_messages(peer_id, callback_query.msg_id), chat_instance=str(callback_query.chat_instance), data=callback_query.data.decode(), - game_short_name=callback_query.game_short_name + game_short_name=callback_query.game_short_name, + client=client ) -def parse_inline_callback_query(callback_query, users): +def parse_inline_callback_query(client, callback_query, users): return pyrogram_types.CallbackQuery( id=str(callback_query.query_id), from_user=parse_user(users[callback_query.user_id]), @@ -883,7 +884,8 @@ def parse_inline_callback_query(callback_query, users): ), b"-_" ).decode().rstrip("="), - game_short_name=callback_query.game_short_name + game_short_name=callback_query.game_short_name, + client=client ) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index 63228f33..76f463ad 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -57,11 +57,13 @@ class CallbackQuery(Object): id: str, from_user, chat_instance: str, + client=None, message=None, inline_message_id: str = None, data: str = None, game_short_name: str = None ): + self._client = client self.id = id # string self.from_user = from_user # User self.message = message # flags.0?Message From acd525294fb3f95403c807e3f35500902083b165 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:08:01 +0200 Subject: [PATCH 04/16] Add CallbackQuery.answer bound method --- pyrogram/client/types/bots/callback_query.py | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pyrogram/client/types/bots/callback_query.py b/pyrogram/client/types/bots/callback_query.py index 76f463ad..083ab58a 100644 --- a/pyrogram/client/types/bots/callback_query.py +++ b/pyrogram/client/types/bots/callback_query.py @@ -71,3 +71,45 @@ class CallbackQuery(Object): self.chat_instance = chat_instance # string self.data = data # flags.2?string self.game_short_name = game_short_name # flags.3?string + + def answer(self, text: str = None, show_alert: bool = None, url: str = None, cache_time: int = 0): + """Use this method as a shortcut for: + + .. code-block:: python + + client.answer_callback_query( + callback_query.id, + text="Hello", + show_alert=True + ) + + Example: + .. code-block:: python + + callback_query.answer("Hello", show_alert=True) + + Args: + text (``str``): + Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters. + + show_alert (``bool``): + If true, an alert will be shown by the client instead of a notification at the top of the chat screen. + Defaults to False. + + url (``str``): + URL that will be opened by the user's client. + If you have created a Game and accepted the conditions via @Botfather, specify the URL that opens your + game – note that this will only work if the query comes from a callback_game button. + Otherwise, you may use links like t.me/your_bot?start=XXXX that open your bot with a parameter. + + cache_time (``int``): + The maximum amount of time in seconds that the result of the callback query may be cached client-side. + Telegram apps will support caching starting in version 3.14. Defaults to 0. + """ + return self._client.answer_callback_query( + callback_query_id=self.id, + text=text, + show_alert=show_alert, + url=url, + cache_time=cache_time + ) From b28f2ebbcb576efc248b6a68ea229dd5e0e3be27 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:08:14 +0200 Subject: [PATCH 05/16] Update callback_query_handler.py --- examples/callback_query_handler.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/callback_query_handler.py b/examples/callback_query_handler.py index 999c2686..38893c02 100644 --- a/examples/callback_query_handler.py +++ b/examples/callback_query_handler.py @@ -27,11 +27,7 @@ app = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") @app.on_callback_query() def answer(client, callback_query): - client.answer_callback_query( - callback_query.id, - text='Button contains: "{}"'.format(callback_query.data), - show_alert=True - ) + callback_query.answer('Button contains: "{}"'.format(callback_query.data), show_alert=True) app.run() # Automatically start() and idle() From aad72ed9b2fb29994ed62676a81bcb259486dc32 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:34:54 +0200 Subject: [PATCH 06/16] Add get_chat_members.py --- examples/get_chat_members.py | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/get_chat_members.py diff --git a/examples/get_chat_members.py b/examples/get_chat_members.py new file mode 100644 index 00000000..a9876ce4 --- /dev/null +++ b/examples/get_chat_members.py @@ -0,0 +1,45 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# 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 . + +import time + +from pyrogram import Client +from pyrogram.api.errors import FloodWait + +app = Client("my_account") + +target = "pyrogramchat" # Target channel/supergroup +members = [] # List that will contain all the members of the target chat +offset = 0 # Offset starts at 0 +limit = 200 # Amount of users to retrieve for each API call (max 200) + +with app: + while True: + try: + chunk = app.get_chat_members(target, offset) + except FloodWait as e: # Very large chats could trigger FloodWait + time.sleep(e.x) # When it happens, wait X seconds and try again + continue + + if not chunk.chat_members: + break # No more members left + + members.extend(chunk.chat_members) + offset += len(chunk.chat_members) + +# Now the "members" list contains all the members of the target chat From 184fb563aaa42083035f078d0a6d547fee659ab6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:45:51 +0200 Subject: [PATCH 07/16] Add short description --- examples/get_chat_members.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/get_chat_members.py b/examples/get_chat_members.py index a9876ce4..3b8837cd 100644 --- a/examples/get_chat_members.py +++ b/examples/get_chat_members.py @@ -21,6 +21,10 @@ import time from pyrogram import Client from pyrogram.api.errors import FloodWait +"""This example shows you how to get the first 10.000 members of a chat. +Refer to get_chat_members2.py for more than 10.000 members. +""" + app = Client("my_account") target = "pyrogramchat" # Target channel/supergroup From 95821f088bbe55aa83d04b61f24a90236f391961 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:47:47 +0200 Subject: [PATCH 08/16] Add get_chat_members2.py --- examples/get_chat_members2.py | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 examples/get_chat_members2.py diff --git a/examples/get_chat_members2.py b/examples/get_chat_members2.py new file mode 100644 index 00000000..563de5ce --- /dev/null +++ b/examples/get_chat_members2.py @@ -0,0 +1,68 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# 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 . + +import time +from string import ascii_lowercase + +from pyrogram import Client +from pyrogram.api.errors import FloodWait + +"""This is an improved version of get_chat_members.py + +Since Telegram will return at most 10.000 members for a single query, this script +repeats the search using numbers ("0" to "9") and all the available ascii letters ("a" to "z"). + +This can be further improved by also searching for non-ascii characters (e.g.: Japanese script), +as some user names may not contain ascii letters at all. +""" + +app = Client("my_account") + +target = "pyrogramchat" # Target channel/supergroup +members = {} # List that will contain all the members of the target chat +limit = 200 # Amount of users to retrieve for each API call (max 200) + +# "" + "0123456789" + "abcdefghijklmnopqrstuvwxyz" (as list) +queries = [""] + [str(i) for i in range(10)] + list(ascii_lowercase) + +with app: + for q in queries: + print('Searching for "{}"'.format(q)) + offset = 0 # For each query, offset restarts from 0 + + while True: + try: + chunk = app.get_chat_members(target, offset, query=q) + except FloodWait as e: # Very large chats could trigger FloodWait + print("Flood wait: {} seconds".format(e.x)) + time.sleep(e.x) # When it happens, wait X seconds and try again + continue + + if not chunk.chat_members: + print('Done searching for "{}"'.format(q)) + print() + break # No more members left + + members.update({i.user.id: i for i in chunk.chat_members}) + offset += len(chunk.chat_members) + + print("Total members: {}".format(len(members))) + + print("Grand total: {}".format(len(members))) + +# Now the "members" list contains all the members of the target chat From 5e8be0e6efe94c7faae01e08b821f875d6394fae Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:48:43 +0200 Subject: [PATCH 09/16] Make use of the "with" context manager --- examples/get_history.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/examples/get_history.py b/examples/get_history.py index 0466fc96..7289e0f2 100644 --- a/examples/get_history.py +++ b/examples/get_history.py @@ -28,25 +28,22 @@ target = "me" # "me" refers to your own chat (Saved Messages) messages = [] # List that will contain all the messages of the target chat offset_id = 0 # ID of the last message of the chunk -app.start() +with app: + while True: + try: + m = app.get_history(target, offset_id=offset_id) + except FloodWait as e: # For very large chats the method call can raise a FloodWait + print("waiting {}".format(e.x)) + time.sleep(e.x) # Sleep X seconds before continuing + continue -while True: - try: - m = app.get_history(target, offset_id=offset_id) - except FloodWait as e: # For very large chats the method call can raise a FloodWait - print("waiting {}".format(e.x)) - time.sleep(e.x) # Sleep X seconds before continuing - continue + if not m.messages: + break - if not m.messages: - break + messages += m.messages + offset_id = m.messages[-1].message_id - messages += m.messages - offset_id = m.messages[-1].message_id - - print("Messages: {}".format(len(messages))) - -app.stop() + print("Messages: {}".format(len(messages))) # Now the "messages" list contains all the messages sorted by date in # descending order (from the most recent to the oldest one) From eb276cbbd5f3795c451f4eeaa61af7d7beabea51 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:49:12 +0200 Subject: [PATCH 10/16] Remove get_participants.py and get_participants2.py --- examples/get_participants.py | 63 ---------------------------- examples/get_participants2.py | 79 ----------------------------------- 2 files changed, 142 deletions(-) delete mode 100644 examples/get_participants.py delete mode 100644 examples/get_participants2.py diff --git a/examples/get_participants.py b/examples/get_participants.py deleted file mode 100644 index fd5257fb..00000000 --- a/examples/get_participants.py +++ /dev/null @@ -1,63 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 . - -import time - -from pyrogram import Client -from pyrogram.api import functions, types -from pyrogram.api.errors import FloodWait - -"""This simple GetParticipants method usage shows you how to get the first 10.000 users of a chat. - -Refer to get_participants2.py for more than 10.000 users. -""" - -app = Client("my_account") -target = "pyrogramchat" # Target channel/supergroup -users = [] # List that will contain all the users of the target chat -limit = 200 # Amount of users to retrieve for each API call -offset = 0 # Offset starts at 0 - -app.start() - -while True: - try: - participants = app.send( - functions.channels.GetParticipants( - channel=app.resolve_peer(target), - filter=types.ChannelParticipantsSearch(""), # Filter by empty string (search for all) - offset=offset, - limit=limit, - hash=0 - ) - ) - except FloodWait as e: - # Very large channels will trigger FloodWait. - # When happens, wait X seconds before continuing - time.sleep(e.x) - continue - - if not participants.participants: - break # No more participants left - - users.extend(participants.users) - offset += limit - -app.stop() - -# Now the "users" list contains all the members of the target chat diff --git a/examples/get_participants2.py b/examples/get_participants2.py deleted file mode 100644 index a70afb74..00000000 --- a/examples/get_participants2.py +++ /dev/null @@ -1,79 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 . - -import time -from string import ascii_lowercase - -from pyrogram import Client -from pyrogram.api import functions, types -from pyrogram.api.errors import FloodWait - -"""This is an improved version of get_participants.py - -Since Telegram will return at most 10.000 users for a single query, this script -repeats the search using numbers ("0" to "9") and all the available ascii letters ("a" to "z"). - -This can be further improved by also searching for non-ascii characters (e.g.: Japanese script), -as some user names may not contain ascii letters at all. -""" - -app = Client("my_account") -target = "pyrogramchat" # Target channel/supergroup username or id -users = {} # To ensure uniqueness, users will be stored in a dictionary with user_id as key -limit = 200 # Amount of users to retrieve for each API call (200 is the maximum) -# "" + "0123456789" + "abcdefghijklmnopqrstuvwxyz" (as list) -queries = [""] + [str(i) for i in range(10)] + list(ascii_lowercase) - -app.start() - -for q in queries: - print("Searching for '{}'".format(q)) - offset = 0 # For each query, offset restarts from 0 - - while True: - try: - participants = app.send( - functions.channels.GetParticipants( - channel=app.resolve_peer(target), - filter=types.ChannelParticipantsSearch(q), - offset=offset, - limit=limit, - hash=0 - ) - ) - except FloodWait as e: - # Very large chats could trigger FloodWait. - # When happens, wait X seconds before continuing - print("Flood wait: {} seconds".format(e.x)) - time.sleep(e.x) - continue - - if not participants.participants: - print("Done searching for '{}'".format(q)) - print() - break # No more participants left - - # User information are stored in the participants.users list. - # Add those users to the dictionary - users.update({i.id: i for i in participants.users}) - - offset += len(participants.participants) - - print("Total users: {}".format(len(users))) - -app.stop() From 65fb30ce1cd1920abb1bb733749abb844ba96a95 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:49:42 +0200 Subject: [PATCH 11/16] Update examples' README.md --- examples/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index 0d8759a0..5d10033a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,8 +11,8 @@ Example | Description [**echo_bot**](echo_bot.py) | Echo bot that replies to every private text message [**welcome_bot**](welcome_bot.py) | The Welcome Bot source code in [@PyrogramChat](https://t.me/pyrogramchat) [**get_history**](get_history.py) | How to retrieve the full message history of a chat -[**get_participants**](get_participants.py) | How to get the first 10.000 members of a supergroup/channel -[**get_participants2**](get_participants2.py) | Improved version to get more than 10.000 users +[**get_chat_members**](get_chat_members.py) | How to get the first 10.000 members of a supergroup/channel +[**get_chat_members2**](get_chat_members2.py) | Improved version to get more than 10.000 members [**query_inline_bots**](query_inline_bots.py) | How to query an inline bot and send a result to a chat [**send_bot_keyboards**](send_bot_keyboards.py) | How to send normal and inline keyboards using regular bots [**callback_query_handler**](callback_query_handler.py) | How to handle queries coming from inline button presses From 133682ca48e887ec1d05103fa7e65c47cc61a7f6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 15:07:12 +0200 Subject: [PATCH 12/16] Add CC0 1.0 Universal License --- examples/LICENSE | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 examples/LICENSE diff --git a/examples/LICENSE b/examples/LICENSE new file mode 100644 index 00000000..1625c179 --- /dev/null +++ b/examples/LICENSE @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. \ No newline at end of file From 9409d9cd8f48ce9f3df928c78ba4c88432d39604 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 15:17:15 +0200 Subject: [PATCH 13/16] Hint about examples' license in README.md --- examples/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/README.md b/examples/README.md index 5d10033a..763db699 100644 --- a/examples/README.md +++ b/examples/README.md @@ -5,6 +5,9 @@ This folder contains example scripts to show you how **Pyrogram** looks like. Every script is working right away (provided you correctly set up your credentials), meaning you can simply copy-paste and run. The only things you have to change are session names and target chats. +All the examples listed in this directory are licensed under the terms of the [CC0 1.0 Universal](LICENSE) license and +can be freely used as basic building blocks for your own applications without worrying about copyrights. + Example | Description ---: | :--- [**hello_world**](hello_world.py) | Demonstration of basic API usages From fd306c383d7b792220d9622ba5831a11b8323e2e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 15:21:36 +0200 Subject: [PATCH 14/16] Remove LGPLv3 license notices from example files Examples are licensed under the terms of CC0 1.0 Universal License --- examples/callback_query_handler.py | 18 ------------------ examples/echo_bot.py | 18 ------------------ examples/get_chat_members.py | 18 ------------------ examples/get_chat_members2.py | 18 ------------------ examples/get_history.py | 18 ------------------ examples/hello_world.py | 18 ------------------ examples/query_inline_bots.py | 18 ------------------ examples/raw_update_handler.py | 18 ------------------ examples/send_bot_keyboards.py | 18 ------------------ examples/welcome_bot.py | 18 ------------------ 10 files changed, 180 deletions(-) diff --git a/examples/callback_query_handler.py b/examples/callback_query_handler.py index 38893c02..a9672f0e 100644 --- a/examples/callback_query_handler.py +++ b/examples/callback_query_handler.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client """This example shows how to handle callback queries, i.e.: queries coming from inline button presses. diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 70e26f9d..a560ed51 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client, Filters """This simple echo bot replies to every private text message. diff --git a/examples/get_chat_members.py b/examples/get_chat_members.py index 3b8837cd..db325f78 100644 --- a/examples/get_chat_members.py +++ b/examples/get_chat_members.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 . - import time from pyrogram import Client diff --git a/examples/get_chat_members2.py b/examples/get_chat_members2.py index 563de5ce..5a92645b 100644 --- a/examples/get_chat_members2.py +++ b/examples/get_chat_members2.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 . - import time from string import ascii_lowercase diff --git a/examples/get_history.py b/examples/get_history.py index 7289e0f2..eb9b2dcd 100644 --- a/examples/get_history.py +++ b/examples/get_history.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 . - import time from pyrogram import Client diff --git a/examples/hello_world.py b/examples/hello_world.py index 8dba8417..c4ff23d0 100644 --- a/examples/hello_world.py +++ b/examples/hello_world.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client """This example demonstrates a basic API usage""" diff --git a/examples/query_inline_bots.py b/examples/query_inline_bots.py index 0d5f7560..0d75b9ff 100644 --- a/examples/query_inline_bots.py +++ b/examples/query_inline_bots.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client """This example shows how to query an inline bot""" diff --git a/examples/raw_update_handler.py b/examples/raw_update_handler.py index c7195761..0f678cec 100644 --- a/examples/raw_update_handler.py +++ b/examples/raw_update_handler.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client """This example shows how to handle raw updates""" diff --git a/examples/send_bot_keyboards.py b/examples/send_bot_keyboards.py index 517b0696..21c1a60e 100644 --- a/examples/send_bot_keyboards.py +++ b/examples/send_bot_keyboards.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton """This example will show you how to send normal and inline keyboards. diff --git a/examples/welcome_bot.py b/examples/welcome_bot.py index 5fd93293..71a00c1c 100644 --- a/examples/welcome_bot.py +++ b/examples/welcome_bot.py @@ -1,21 +1,3 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-2018 Dan Tès -# -# 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 pyrogram import Client, Emoji, Filters """This is the Welcome Bot in @PyrogramChat. From be7194f89e9219368dd2447706ad61322aef13b3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 15:23:40 +0200 Subject: [PATCH 15/16] Put example descriptions at the very top of the files --- examples/callback_query_handler.py | 7 ++++--- examples/echo_bot.py | 4 ++-- examples/get_chat_members.py | 8 ++++---- examples/get_chat_members2.py | 12 ++++++------ examples/get_history.py | 4 ++-- examples/hello_world.py | 4 ++-- examples/query_inline_bots.py | 4 ++-- examples/raw_update_handler.py | 4 ++-- examples/send_bot_keyboards.py | 4 ++-- examples/welcome_bot.py | 6 +++--- 10 files changed, 29 insertions(+), 28 deletions(-) diff --git a/examples/callback_query_handler.py b/examples/callback_query_handler.py index a9672f0e..71538eae 100644 --- a/examples/callback_query_handler.py +++ b/examples/callback_query_handler.py @@ -1,8 +1,9 @@ -from pyrogram import Client - """This example shows how to handle callback queries, i.e.: queries coming from inline button presses. -It uses the @on_callback_query decorator to register a CallbackQueryHandler.""" +It uses the @on_callback_query decorator to register a CallbackQueryHandler. +""" + +from pyrogram import Client app = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") diff --git a/examples/echo_bot.py b/examples/echo_bot.py index a560ed51..7a2b0aa7 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -1,11 +1,11 @@ -from pyrogram import Client, Filters - """This simple echo bot replies to every private text message. It uses the @on_message decorator to register a MessageHandler and applies two filters on it: Filters.text and Filters.private to make sure it will reply to private text messages only. """ +from pyrogram import Client, Filters + app = Client("my_account") diff --git a/examples/get_chat_members.py b/examples/get_chat_members.py index db325f78..e0f8c3fa 100644 --- a/examples/get_chat_members.py +++ b/examples/get_chat_members.py @@ -1,12 +1,12 @@ +"""This example shows you how to get the first 10.000 members of a chat. +Refer to get_chat_members2.py for more than 10.000 members. +""" + import time from pyrogram import Client from pyrogram.api.errors import FloodWait -"""This example shows you how to get the first 10.000 members of a chat. -Refer to get_chat_members2.py for more than 10.000 members. -""" - app = Client("my_account") target = "pyrogramchat" # Target channel/supergroup diff --git a/examples/get_chat_members2.py b/examples/get_chat_members2.py index 5a92645b..a4fa9daa 100644 --- a/examples/get_chat_members2.py +++ b/examples/get_chat_members2.py @@ -1,9 +1,3 @@ -import time -from string import ascii_lowercase - -from pyrogram import Client -from pyrogram.api.errors import FloodWait - """This is an improved version of get_chat_members.py Since Telegram will return at most 10.000 members for a single query, this script @@ -13,6 +7,12 @@ This can be further improved by also searching for non-ascii characters (e.g.: J as some user names may not contain ascii letters at all. """ +import time +from string import ascii_lowercase + +from pyrogram import Client +from pyrogram.api.errors import FloodWait + app = Client("my_account") target = "pyrogramchat" # Target channel/supergroup diff --git a/examples/get_history.py b/examples/get_history.py index eb9b2dcd..628b5692 100644 --- a/examples/get_history.py +++ b/examples/get_history.py @@ -1,10 +1,10 @@ +"""This example shows how to retrieve the full message history of a chat""" + import time from pyrogram import Client from pyrogram.api.errors import FloodWait -"""This example shows how to retrieve the full message history of a chat""" - app = Client("my_account") target = "me" # "me" refers to your own chat (Saved Messages) messages = [] # List that will contain all the messages of the target chat diff --git a/examples/hello_world.py b/examples/hello_world.py index c4ff23d0..010725ef 100644 --- a/examples/hello_world.py +++ b/examples/hello_world.py @@ -1,7 +1,7 @@ -from pyrogram import Client - """This example demonstrates a basic API usage""" +from pyrogram import Client + # Create a new Client instance app = Client("my_account") diff --git a/examples/query_inline_bots.py b/examples/query_inline_bots.py index 0d75b9ff..c3b48874 100644 --- a/examples/query_inline_bots.py +++ b/examples/query_inline_bots.py @@ -1,7 +1,7 @@ -from pyrogram import Client - """This example shows how to query an inline bot""" +from pyrogram import Client + # Create a new Client app = Client("my_account") diff --git a/examples/raw_update_handler.py b/examples/raw_update_handler.py index 0f678cec..27d87eb3 100644 --- a/examples/raw_update_handler.py +++ b/examples/raw_update_handler.py @@ -1,7 +1,7 @@ -from pyrogram import Client - """This example shows how to handle raw updates""" +from pyrogram import Client + app = Client("my_account") diff --git a/examples/send_bot_keyboards.py b/examples/send_bot_keyboards.py index 21c1a60e..3a15a23a 100644 --- a/examples/send_bot_keyboards.py +++ b/examples/send_bot_keyboards.py @@ -1,5 +1,3 @@ -from pyrogram import Client, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton - """This example will show you how to send normal and inline keyboards. You must log-in as a regular bot in order to send keyboards (use the token from @BotFather). @@ -9,6 +7,8 @@ send_message() is used as example, but a keyboard can be sent with any other sen like send_audio(), send_document(), send_location(), etc... """ +from pyrogram import Client, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton + # Create a client using your bot token app = Client("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11") app.start() diff --git a/examples/welcome_bot.py b/examples/welcome_bot.py index 71a00c1c..396fe870 100644 --- a/examples/welcome_bot.py +++ b/examples/welcome_bot.py @@ -1,11 +1,11 @@ -from pyrogram import Client, Emoji, Filters - """This is the Welcome Bot in @PyrogramChat. It uses the Emoji module to easily add emojis in your text messages and Filters -to make it only work for specific messages in a specific chat +to make it only work for specific messages in a specific chat """ +from pyrogram import Client, Emoji, Filters + app = Client("my_account") From c2da2a61ece6711bd7ed51d68249995843ff5dec Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 9 Oct 2018 16:37:53 +0200 Subject: [PATCH 16/16] Update welcome_bot.py --- examples/welcome_bot.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/examples/welcome_bot.py b/examples/welcome_bot.py index 396fe870..5dbb44fb 100644 --- a/examples/welcome_bot.py +++ b/examples/welcome_bot.py @@ -1,34 +1,27 @@ """This is the Welcome Bot in @PyrogramChat. It uses the Emoji module to easily add emojis in your text messages and Filters -to make it only work for specific messages in a specific chat +to make it only work for specific messages in a specific chat. """ from pyrogram import Client, Emoji, Filters +MENTION = "[{}](tg://user?id={})" +MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {}!" + app = Client("my_account") @app.on_message(Filters.chat("PyrogramChat") & Filters.new_chat_members) def welcome(client, message): # Build the new members list (with mentions) by using their first_name - new_members = ", ".join([ - "[{}](tg://user?id={})".format(i.first_name, i.id) - for i in message.new_chat_members - ]) + new_members = [MENTION.format(i.first_name, i.id) for i in message.new_chat_members] # Build the welcome message by using an emoji and the list we built above - text = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {}!".format( - Emoji.SPARKLES, - new_members - ) + text = MESSAGE.format(Emoji.SPARKLES, ", ".join(new_members)) # Send the welcome message - client.send_message( - message.chat.id, text, - reply_to_message_id=message.message_id, - disable_web_page_preview=True - ) + message.reply(text, disable_web_page_preview=True) app.run() # Automatically start() and idle()