X Tutup
Skip to content

Commit d7e226e

Browse files
committed
Add new Bot methods and ChatMember class python-telegram-bot#302
1 parent 7c84516 commit d7e226e

File tree

3 files changed

+132
-19
lines changed

3 files changed

+132
-19
lines changed

telegram/__init__.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .base import TelegramObject
2424
from .user import User
2525
from .chat import Chat
26+
from .chatmember import ChatMember
2627
from .photosize import PhotoSize
2728
from .audio import Audio
2829
from .voice import Voice
@@ -82,22 +83,23 @@
8283

8384
__author__ = 'devs@python-telegram-bot.org'
8485
__version__ = '4.1.2'
85-
__all__ = ['Audio', 'Bot', 'Chat', 'ChatAction', 'ChosenInlineResult', 'CallbackQuery', 'Contact',
86-
'Document', 'Emoji', 'File', 'ForceReply', 'InlineKeyboardButton',
87-
'InlineKeyboardMarkup', 'InlineQuery', 'InlineQueryResult', 'InlineQueryResult',
88-
'InlineQueryResultArticle', 'InlineQueryResultAudio', 'InlineQueryResultCachedAudio',
89-
'InlineQueryResultCachedDocument', 'InlineQueryResultCachedGif',
90-
'InlineQueryResultCachedMpeg4Gif', 'InlineQueryResultCachedPhoto',
91-
'InlineQueryResultCachedSticker', 'InlineQueryResultCachedVideo',
92-
'InlineQueryResultCachedVoice', 'InlineQueryResultContact', 'InlineQueryResultDocument',
93-
'InlineQueryResultGif', 'InlineQueryResultLocation', 'InlineQueryResultMpeg4Gif',
94-
'InlineQueryResultPhoto', 'InlineQueryResultVenue', 'InlineQueryResultVideo',
95-
'InlineQueryResultVoice', 'InputContactMessageContent', 'InputFile',
96-
'InputLocationMessageContent', 'InputMessageContent', 'InputTextMessageContent',
97-
'InputVenueMessageContent', 'KeyboardButton', 'Location', 'Message', 'MessageEntity',
98-
'NullHandler', 'ParseMode', 'PhotoSize', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup',
99-
'ReplyMarkup', 'Sticker', 'TelegramError', 'TelegramObject', 'Update', 'User',
100-
'UserProfilePhotos', 'Venue', 'Video', 'Voice']
86+
__all__ = ['Audio', 'Bot', 'Chat', 'ChatMember', 'ChatAction', 'ChosenInlineResult',
87+
'CallbackQuery', 'Contact', 'Document', 'Emoji', 'File', 'ForceReply',
88+
'InlineKeyboardButton', 'InlineKeyboardMarkup', 'InlineQuery', 'InlineQueryResult',
89+
'InlineQueryResult', 'InlineQueryResultArticle', 'InlineQueryResultAudio',
90+
'InlineQueryResultCachedAudio', 'InlineQueryResultCachedDocument',
91+
'InlineQueryResultCachedGif', 'InlineQueryResultCachedMpeg4Gif',
92+
'InlineQueryResultCachedPhoto', 'InlineQueryResultCachedSticker',
93+
'InlineQueryResultCachedVideo', 'InlineQueryResultCachedVoice',
94+
'InlineQueryResultContact', 'InlineQueryResultDocument', 'InlineQueryResultGif',
95+
'InlineQueryResultLocation', 'InlineQueryResultMpeg4Gif', 'InlineQueryResultPhoto',
96+
'InlineQueryResultVenue', 'InlineQueryResultVideo', 'InlineQueryResultVoice',
97+
'InputContactMessageContent', 'InputFile', 'InputLocationMessageContent',
98+
'InputMessageContent', 'InputTextMessageContent', 'InputVenueMessageContent',
99+
'KeyboardButton', 'Location', 'Message', 'MessageEntity', 'NullHandler', 'ParseMode',
100+
'PhotoSize', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'ReplyMarkup', 'Sticker',
101+
'TelegramError', 'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue',
102+
'Video', 'Voice']
101103

102104
if version_info < (2, 7):
103105
from warnings import warn

telegram/bot.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
# along with this program. If not, see [http://www.gnu.org/licenses/].
2020
"""This module contains a object that represents a Telegram Bot."""
2121

22-
import logging
2322
import functools
23+
import logging
2424

25-
from telegram import (User, Message, Update, UserProfilePhotos, File, ReplyMarkup, TelegramObject,
26-
NullHandler)
25+
from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File,
26+
ReplyMarkup, TelegramObject, NullHandler)
2727
from telegram.utils import request
2828
from telegram.utils.validate import validate_token
2929

@@ -1259,6 +1259,56 @@ def setWebhook(self, webhook_url=None, certificate=None, **kwargs):
12591259

12601260
return result
12611261

1262+
@log
1263+
def leaveChat(self, chat_id, **kwargs):
1264+
url = '{0}/leaveChat'.format(self.base_url)
1265+
1266+
data = {'chat_id': chat_id}
1267+
1268+
result = request.post(url, data, timeout=kwargs.get('timeout'))
1269+
1270+
return result
1271+
1272+
@log
1273+
def getChat(self, chat_id, **kwargs):
1274+
url = '{0}/getChat'.format(self.base_url)
1275+
1276+
data = {'chat_id': chat_id}
1277+
1278+
result = request.post(url, data, timeout=kwargs.get('timeout'))
1279+
1280+
return Chat.de_json(result)
1281+
1282+
@log
1283+
def getChatAdministrators(self, chat_id, **kwargs):
1284+
url = '{0}/getChatAdministrators'.format(self.base_url)
1285+
1286+
data = {'chat_id': chat_id}
1287+
1288+
result = request.post(url, data, timeout=kwargs.get('timeout'))
1289+
1290+
return [ChatMember.de_json(x) for x in result]
1291+
1292+
@log
1293+
def getChatMembersCount(self, chat_id, **kwargs):
1294+
url = '{0}/getChatMembersCount'.format(self.base_url)
1295+
1296+
data = {'chat_id': chat_id}
1297+
1298+
result = request.post(url, data, timeout=kwargs.get('timeout'))
1299+
1300+
return result
1301+
1302+
@log
1303+
def getChatMember(self, chat_id, user_id, **kwargs):
1304+
url = '{0}/getChatMember'.format(self.base_url)
1305+
1306+
data = {'chat_id': chat_id, 'user_id': user_id}
1307+
1308+
result = request.post(url, data, timeout=kwargs.get('timeout'))
1309+
1310+
return ChatMember.de_json(result)
1311+
12621312
@staticmethod
12631313
def de_json(data):
12641314
data = super(Bot, Bot).de_json(data)
@@ -1302,3 +1352,8 @@ def __reduce__(self):
13021352
edit_message_reply_markup = editMessageReplyMarkup
13031353
get_updates = getUpdates
13041354
set_webhook = setWebhook
1355+
leave_chat = leaveChat
1356+
get_chat = getChat
1357+
get_chat_administrators = getChatAdministrators
1358+
get_chat_member = getChatMember
1359+
get_chat_member_count = getChatMembersCount

telegram/chatmember.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2016
5+
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser Public License
18+
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
"""This module contains a object that represents a Telegram ChatMember."""
20+
21+
from telegram import User, TelegramObject
22+
23+
24+
class ChatMember(TelegramObject):
25+
"""This object represents a Telegram ChatMember.
26+
27+
Attributes:
28+
user (:class:`telegram.User`): Information about the user.
29+
status (str): The member's status in the chat. Can be 'creator', 'administrator', 'member',
30+
'left' or 'kicked'.
31+
32+
Args:
33+
user (:class:`telegram.User`):
34+
status (str):
35+
"""
36+
37+
def __init__(self, user, status, **kwargs):
38+
# Required
39+
self.user = user
40+
self.status = status
41+
42+
@staticmethod
43+
def de_json(data):
44+
"""
45+
Args:
46+
data (dict):
47+
48+
Returns:
49+
telegram.ChatMember:
50+
"""
51+
if not data:
52+
return None
53+
54+
data['user'] = User.de_json(data.get('user'))
55+
56+
return ChatMember(**data)

0 commit comments

Comments
 (0)
X Tutup