Add WebhookInfo and getWebhookInfo

Still needs tests though
This commit is contained in:
Jacob Bom 2016-10-03 15:16:43 +02:00
parent f7ede4baea
commit 868d9217bc
3 changed files with 76 additions and 2 deletions

View file

@ -83,6 +83,7 @@ from .constants import (MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH, SUPPORTED_WEBHOO
MAX_FILESIZE_DOWNLOAD, MAX_FILESIZE_UPLOAD,
MAX_MESSAGES_PER_SECOND_PER_CHAT, MAX_MESSAGES_PER_SECOND,
MAX_MESSAGES_PER_MINUTE_PER_GROUP)
from .webhookinfo import WebhookInfo
from .version import __version__ # flake8: noqa
__author__ = 'devs@python-telegram-bot.org'
@ -105,4 +106,4 @@ __all__ = ['Audio', 'Bot', 'Chat', 'ChatMember', 'ChatAction', 'ChosenInlineResu
'TelegramObject', 'Update', 'User', 'UserProfilePhotos', 'Venue', 'Video', 'Voice',
'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP']
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP', 'WebhookInfo']

View file

@ -23,7 +23,7 @@ import functools
import logging
from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File,
ReplyMarkup, TelegramObject)
ReplyMarkup, TelegramObject, WebhookInfo)
from telegram.error import InvalidToken
from telegram.utils.request import Request
@ -1440,6 +1440,15 @@ class Bot(TelegramObject):
return ChatMember.de_json(result, self)
def getWebhookInfo(self, **kwargs):
url = '{0}/getWebhookInfo'.format(self.base_url)
data = {}
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
return WebhookInfo.de_json(result, self)
@staticmethod
def de_json(data, bot):
data = super(Bot, Bot).de_json(data, bot)
@ -1489,3 +1498,4 @@ class Bot(TelegramObject):
get_chat_administrators = getChatAdministrators
get_chat_member = getChatMember
get_chat_members_count = getChatMembersCount
get_webhook_info = getWebhookInfo

63
telegram/webhookinfo.py Normal file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains a object that represents a Telegram WebhookInfo."""
from telegram import TelegramObject
class WebhookInfo(TelegramObject):
"""This object represents a Telegram WebhookInfo.
Attributes:
url (str): Webhook URL, may be empty if webhook is not set up
has_custom_certificate (bool):
pending_update_count (int):
last_error_date (Optional[int]):
last_error_message (Optional[str]):
Args:
url (str): Webhook URL, may be empty if webhook is not set up
has_custom_certificate (bool):
pending_update_count (int):
last_error_date (Optional[int]):
last_error_message (Optional[str]):
"""
def __init__(self, url, has_custom_certificate, pending_update_count, **kwargs):
# Required
self.url = url
self.has_custom_certificate = has_custom_certificate
self.pending_update_count = pending_update_count
self.last_error_date = kwargs.get('last_error_date')
self.last_error_message = kwargs.get('last_error_message')
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.WebhookInfo:
"""
if not data:
return None
return WebhookInfo(**data)