From 702763edd6ab5f09701d3b4e282d1c8c4ae86caa Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Thu, 20 Oct 2016 10:52:38 +0300 Subject: [PATCH] Get webhook info https://core.telegram.org/bots/api#getwebhookinfo --- telebot/__init__.py | 4 ++++ telebot/apihelper.py | 6 ++++++ telebot/types.py | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/telebot/__init__.py b/telebot/__init__.py index f234c6a..220750e 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -78,6 +78,10 @@ class TeleBot: def set_webhook(self, url=None, certificate=None): return apihelper.set_webhook(self.token, url, certificate) + def get_webhook_info(self): + result = apihelper.get_webhook_info(self.token) + return types.WebhookInfo.de_json(result) + def remove_webhook(self): return self.set_webhook() # No params resets webhook diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 693387d..0110b33 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -127,6 +127,12 @@ def set_webhook(token, url=None, certificate=None): return _make_request(token, method_url, params=payload, files=files) +def get_webhook_info(token): + method_url = r'getWebhookInfo' + payload = {} + return _make_request(token, method_url, params=payload) + + def get_updates(token, offset=None, limit=None, timeout=None): method_url = r'getUpdates' payload = {} diff --git a/telebot/types.py b/telebot/types.py index 97a08c3..792cdcd 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -121,6 +121,29 @@ class Update(JsonDeserializable): self.callback_query = callback_query +class WebhookInfo(JsonDeserializable): + @classmethod + def de_json(cls, json_string): + obj = cls.check_json(json_string) + url = obj['url'] + has_custom_certificate = obj['has_custom_certificate'] + pending_update_count = obj['pending_update_count'] + last_error_date = None + last_error_message = None + if 'last_error_message' in obj: + last_error_date = obj['last_error_date'] + if 'last_error_message' in obj: + last_error_message = obj['last_error_message'] + return cls(url, has_custom_certificate, pending_update_count, last_error_date, last_error_message) + + def __init__(self, url, has_custom_certificate, pending_update_count, last_error_date, last_error_message): + self.url = url + self.has_custom_certificate = has_custom_certificate + self.pending_update_count = pending_update_count + self.last_error_date = last_error_date + self.last_error_message = last_error_message + + class User(JsonDeserializable): @classmethod def de_json(cls, json_string):