Skip to content

Commit 1b99caa

Browse files
committed
Merge remote-tracking branch 'origin/master' into entities-filter
2 parents e16c1da + e1242b3 commit 1b99caa

File tree

114 files changed

+868
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+868
-391
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ The following wonderful people contributed directly or indirectly to this projec
1111
- `Avanatiker <https://github.com/Avanatiker>`_
1212
- `Balduro <https://github.com/Balduro>`_
1313
- `bimmlerd <https://github.com/bimmlerd>`_
14+
- `Eli Gao <https://github.com/eligao>`_
1415
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
1516
- `franciscod <https://github.com/franciscod>`_
1617
- `Jacob Bom <https://github.com/bomjacob>`_
1718
- `JASON0916 <https://github.com/JASON0916>`_
1819
- `jh0ker <https://github.com/jh0ker>`_
1920
- `JRoot3D <https://github.com/JRoot3D>`_
2021
- `jlmadurga <https://github.com/jlmadurga>`_
22+
- `Li-aung Yip <https://github.com/LiaungYip>`_
2123
- `macrojames <https://github.com/macrojames>`_
2224
- `naveenvhegde <https://github.com/naveenvhegde>`_
2325
- `njittam <https://github.com/njittam>`_

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ You can install or upgrade python-telegram-bot with:
9696
9797
$ pip install python-telegram-bot --upgrade
9898
99+
Or you can install from source with:
100+
101+
.. code:: shell
102+
103+
$ git clone https://github.com/python-telegram-bot/python-telegram-bot
104+
$ cd python-telegram-bot
105+
$ python setup.py install
99106
===============
100107
Getting started
101108
===============

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The examples in this folder are small bots meant to show you how a bot that is w
44

55
All examples are licensed under the [CC0 License](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/LICENSE.txt) and are therefore fully dedicated to the public domain. You can use them as the base for your own bots without worrying about copyrights.
66

7-
### [`echobot2.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot2.py)
7+
### [`echobot2.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/v5.0/examples/echobot2.py)
88
This is probably the base for most of the bots made with `python-telegram-bot`. It simply replies to each text message with a message that contains the same text.
99

1010
### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py)

examples/echobot2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
# Define a few command handlers. These usually take the two arguments bot and
3030
# update. Error handlers also receive the raised TelegramError object in error.
3131
def start(bot, update):
32-
bot.sendMessage(update.message.chat_id, text='Hi!')
32+
update.message.reply_text('Hi!')
3333

3434

3535
def help(bot, update):
36-
bot.sendMessage(update.message.chat_id, text='Help!')
36+
update.message.reply_text('Help!')
3737

3838

3939
def echo(bot, update):
40-
bot.sendMessage(update.message.chat_id, text=update.message.text)
40+
update.message.reply_text(update.message.text)
4141

4242

4343
def error(bot, update, error):

telegram/audio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ def __init__(self, file_id, duration, **kwargs):
5555
self.file_size = int(kwargs.get('file_size', 0))
5656

5757
@staticmethod
58-
def de_json(data):
58+
def de_json(data, bot):
5959
"""
6060
Args:
61-
data (str):
61+
data (dict):
62+
bot (telegram.Bot):
6263
6364
Returns:
6465
telegram.Audio:

telegram/base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ def __getitem__(self, item):
3838
return self.__dict__[item]
3939

4040
@staticmethod
41-
def de_json(data):
41+
def de_json(data, bot):
4242
"""
4343
Args:
44-
data (str):
44+
data (dict):
45+
bot (telegram.Bot):
4546
4647
Returns:
47-
telegram.TelegramObject:
48+
dict:
4849
"""
4950
if not data:
5051
return None
@@ -68,6 +69,9 @@ def to_dict(self):
6869
data = dict()
6970

7071
for key in iter(self.__dict__):
72+
if key == 'bot':
73+
continue
74+
7175
value = self.__dict__[key]
7276
if value is not None:
7377
if hasattr(value, 'to_dict'):

telegram/bot.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def decorator(self, *args, **kwargs):
154154
if result is True:
155155
return result
156156

157-
return Message.de_json(result)
157+
return Message.de_json(result, self)
158158

159159
return decorator
160160

@@ -176,7 +176,7 @@ def getMe(self, **kwargs):
176176

177177
result = self._request.get(url)
178178

179-
self.bot = User.de_json(result)
179+
self.bot = User.de_json(result, self)
180180

181181
return self.bot
182182

@@ -860,7 +860,7 @@ def getUserProfilePhotos(self, user_id, offset=None, limit=100, **kwargs):
860860

861861
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
862862

863-
return UserProfilePhotos.de_json(result)
863+
return UserProfilePhotos.de_json(result, self)
864864

865865
@log
866866
def getFile(self, file_id, **kwargs):
@@ -894,7 +894,7 @@ def getFile(self, file_id, **kwargs):
894894
if result.get('file_path'):
895895
result['file_path'] = '%s/%s' % (self.base_file_url, result['file_path'])
896896

897-
return File.de_json(result, self._request)
897+
return File.de_json(result, self)
898898

899899
@log
900900
def kickChatMember(self, chat_id, user_id, **kwargs):
@@ -1225,7 +1225,7 @@ def getUpdates(self, offset=None, limit=100, timeout=0, network_delay=5., **kwar
12251225
else:
12261226
self.logger.debug('No new updates found.')
12271227

1228-
return [Update.de_json(x) for x in result]
1228+
return [Update.de_json(u, self) for u in result]
12291229

12301230
@log
12311231
def setWebhook(self, webhook_url=None, certificate=None, **kwargs):
@@ -1325,7 +1325,7 @@ def getChat(self, chat_id, **kwargs):
13251325

13261326
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
13271327

1328-
return Chat.de_json(result)
1328+
return Chat.de_json(result, self)
13291329

13301330
@log
13311331
def getChatAdministrators(self, chat_id, **kwargs):
@@ -1360,7 +1360,7 @@ def getChatAdministrators(self, chat_id, **kwargs):
13601360

13611361
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
13621362

1363-
return [ChatMember.de_json(x) for x in result]
1363+
return [ChatMember.de_json(x, self) for x in result]
13641364

13651365
@log
13661366
def getChatMembersCount(self, chat_id, **kwargs):
@@ -1423,11 +1423,11 @@ def getChatMember(self, chat_id, user_id, **kwargs):
14231423

14241424
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
14251425

1426-
return ChatMember.de_json(result)
1426+
return ChatMember.de_json(result, self)
14271427

14281428
@staticmethod
1429-
def de_json(data):
1430-
data = super(Bot, Bot).de_json(data)
1429+
def de_json(data, bot):
1430+
data = super(Bot, Bot).de_json(data, bot)
14311431

14321432
return Bot(**data)
14331433

telegram/callbackquery.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class CallbackQuery(TelegramObject):
2626
"""This object represents a Telegram CallbackQuery."""
2727

28-
def __init__(self, id, from_user, data, **kwargs):
28+
def __init__(self, id, from_user, data, bot=None, **kwargs):
2929
# Required
3030
self.id = id
3131
self.from_user = from_user
@@ -34,15 +34,26 @@ def __init__(self, id, from_user, data, **kwargs):
3434
self.message = kwargs.get('message')
3535
self.inline_message_id = kwargs.get('inline_message_id', '')
3636

37+
self.bot = bot
38+
3739
@staticmethod
38-
def de_json(data):
40+
def de_json(data, bot):
41+
"""
42+
Args:
43+
data (dict):
44+
bot (telegram.Bot):
45+
46+
Returns:
47+
telegram.CallbackQuery:
48+
"""
49+
3950
if not data:
4051
return None
4152

42-
data['from_user'] = User.de_json(data.get('from'))
43-
data['message'] = Message.de_json(data.get('message'))
53+
data['from_user'] = User.de_json(data.get('from'), bot)
54+
data['message'] = Message.de_json(data.get('message'), bot)
4455

45-
return CallbackQuery(**data)
56+
return CallbackQuery(bot=bot, **data)
4657

4758
def to_dict(self):
4859
"""
@@ -54,3 +65,7 @@ def to_dict(self):
5465
# Required
5566
data['from'] = data.pop('from_user', None)
5667
return data
68+
69+
def answer(self, *args, **kwargs):
70+
"""Shortcut for ``bot.answerCallbackQuery(update.callback_query.id, *args, **kwargs)``"""
71+
return self.bot.answerCallbackQuery(self.id, *args, **kwargs)

telegram/chat.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ class Chat(TelegramObject):
4040
4141
Keyword Args:
4242
type (Optional[str]):
43+
bot (Optional[Bot]): The Bot to use for instance methods
4344
"""
4445

4546
PRIVATE = 'private'
4647
GROUP = 'group'
4748
SUPERGROUP = 'supergroup'
4849
CHANNEL = 'channel'
4950

50-
def __init__(self, id, type, **kwargs):
51+
def __init__(self, id, type, bot=None, **kwargs):
5152
# Required
5253
self.id = int(id)
5354
self.type = type
@@ -57,16 +58,47 @@ def __init__(self, id, type, **kwargs):
5758
self.first_name = kwargs.get('first_name', '')
5859
self.last_name = kwargs.get('last_name', '')
5960

61+
self.bot = bot
62+
6063
@staticmethod
61-
def de_json(data):
64+
def de_json(data, bot):
6265
"""
6366
Args:
6467
data (dict):
68+
bot (telegram.Bot):
6569
6670
Returns:
6771
telegram.Chat:
6872
"""
6973
if not data:
7074
return None
7175

72-
return Chat(**data)
76+
return Chat(bot=bot, **data)
77+
78+
def send_action(self, *args, **kwargs):
79+
"""Shortcut for ``bot.sendChatAction(update.message.chat.id, *args, **kwargs)``"""
80+
return self.bot.sendChatAction(self.id, *args, **kwargs)
81+
82+
def leave(self, *args, **kwargs):
83+
"""Shortcut for ``bot.leaveChat(update.message.chat.id, *args, **kwargs)``"""
84+
return self.bot.leaveChat(self.id, *args, **kwargs)
85+
86+
def get_administrators(self, *args, **kwargs):
87+
"""Shortcut for ``bot.getChatAdministrators(update.message.chat.id, *args, **kwargs)``"""
88+
return self.bot.getChatAdministrators(self.id, *args, **kwargs)
89+
90+
def get_members_count(self, *args, **kwargs):
91+
"""Shortcut for ``bot.getChatMembersCount(update.message.chat.id, *args, **kwargs)``"""
92+
return self.bot.getChatMembersCount(self.id, *args, **kwargs)
93+
94+
def get_member(self, *args, **kwargs):
95+
"""Shortcut for ``bot.getChatMember(update.message.chat.id, *args, **kwargs)``"""
96+
return self.bot.getChatMember(self.id, *args, **kwargs)
97+
98+
def kick_member(self, *args, **kwargs):
99+
"""Shortcut for ``bot.kickChatMember(update.message.chat.id, *args, **kwargs)``"""
100+
return self.bot.kickChatMember(self.id, *args, **kwargs)
101+
102+
def unban_member(self, *args, **kwargs):
103+
"""Shortcut for ``bot.unbanChatMember(update.message.chat.id, *args, **kwargs)``"""
104+
return self.bot.unbanChatMember(self.id, *args, **kwargs)

telegram/chatmember.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ def __init__(self, user, status, **kwargs):
4646
self.status = status
4747

4848
@staticmethod
49-
def de_json(data):
49+
def de_json(data, bot):
5050
"""
5151
Args:
5252
data (dict):
53+
bot (telegram.Bot):
5354
5455
Returns:
5556
telegram.ChatMember:
5657
"""
5758
if not data:
5859
return None
5960

60-
data['user'] = User.de_json(data.get('user'))
61+
data['user'] = User.de_json(data.get('user'), bot)
6162

6263
return ChatMember(**data)

0 commit comments

Comments
 (0)