mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Added some documentation.
This commit is contained in:
parent
13133ac5c6
commit
86500fc9fa
@ -44,6 +44,11 @@ class TeleBot:
|
||||
self.commands = []
|
||||
|
||||
def get_update(self):
|
||||
"""
|
||||
Retrieves any updates from the Telegram API.
|
||||
Registered listeners and applicable message handlers will be notified when a new message arrives.
|
||||
:raises ApiException when a call has failed.
|
||||
"""
|
||||
updates = apihelper.get_updates(self.token, offset=(self.last_update_id + 1))
|
||||
new_messages = []
|
||||
for update in updates:
|
||||
|
@ -7,16 +7,25 @@ from telebot import types
|
||||
|
||||
|
||||
def _make_request(token, method_name, method='get', params=None, files=None):
|
||||
"""
|
||||
Makes a request to the Telegram API.
|
||||
:param token: The bot's API token. (Created with @BotFather)
|
||||
:param method_name: Name of the API method to be called. (E.g. 'getUpdates')
|
||||
:param method: HTTP method to be used. Defaults to 'get'.
|
||||
:param params: Optional parameters. Should be a dictionary with key-value pairs.
|
||||
:param files: Optional files.
|
||||
:return:
|
||||
"""
|
||||
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
|
||||
result = requests.request(method, request_url, params=params, files=files)
|
||||
if result.status_code != 200:
|
||||
raise ApiException(method_name + r' error.', result)
|
||||
raise ApiException(method_name, result)
|
||||
try:
|
||||
result_json = result.json()
|
||||
if not result_json['ok']:
|
||||
raise Exception()
|
||||
except:
|
||||
raise ApiException(method_name + r' error.', result)
|
||||
raise ApiException(method_name, result)
|
||||
return result_json['result']
|
||||
|
||||
|
||||
@ -43,7 +52,7 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m
|
||||
if reply_to_message_id:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
payload['reply_markup'] = convert_markup(reply_markup)
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
return _make_request(token, method_url, params=payload)
|
||||
|
||||
|
||||
@ -80,7 +89,7 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re
|
||||
if reply_to_message_id:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
payload['reply_markup'] = convert_markup(reply_markup)
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
return _make_request(token, method_url, params=payload, files=files, method='post')
|
||||
|
||||
|
||||
@ -90,7 +99,7 @@ def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None,
|
||||
if reply_to_message_id:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
payload['reply_markup'] = convert_markup(reply_markup)
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
return _make_request(token, method_url, params=payload)
|
||||
|
||||
|
||||
@ -107,7 +116,7 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m
|
||||
if reply_to_message_id:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
payload['reply_markup'] = convert_markup(reply_markup)
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
return _make_request(token, method_url, params=payload, files=files, method='post')
|
||||
|
||||
|
||||
@ -122,13 +131,17 @@ def get_method_by_type(data_type):
|
||||
return 'sendVideo'
|
||||
|
||||
|
||||
def convert_markup(markup):
|
||||
def _convert_markup(markup):
|
||||
if isinstance(markup, types.JsonSerializable):
|
||||
return markup.to_json()
|
||||
return markup
|
||||
|
||||
|
||||
class ApiException(Exception):
|
||||
def __init__(self, message, result):
|
||||
super(ApiException, self).__init__(message)
|
||||
"""
|
||||
This class represents an Exception thrown when a call to the Telegram API fails.
|
||||
"""
|
||||
def __init__(self, function_name, result):
|
||||
super(ApiException, self).__init__('{0} failed. Returned result: {1}'.format(function_name, result))
|
||||
self.function_name = function_name
|
||||
self.result = result
|
||||
|
Loading…
Reference in New Issue
Block a user