From 94f1bbd40235e689190b6f8db553d1c3b420879e Mon Sep 17 00:00:00 2001 From: eternnoir Date: Tue, 5 Jan 2016 14:07:47 +0800 Subject: [PATCH] Almost done. --- telebot/__init__.py | 3 +++ telebot/apihelper.py | 31 +++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 5d28391..07590f3 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -415,6 +415,9 @@ class TeleBot: """ return self.send_message(message.chat.id, text, reply_to_message_id=message.message_id, **kwargs) + def answer_inline_query(self, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None): + return apihelper.answer_inline_query(self.token, inline_query_id, results, cache_time, is_personal, next_offset) + def register_for_reply(self, message, callback): """ Registers a callback function to be notified when a reply to `message` arrives. diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 411e9c5..7ae8556 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -42,19 +42,19 @@ def _check_result(method_name, result): :return: The result parsed to a JSON dictionary. """ if result.status_code != 200: - msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\ + msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]' \ .format(result.status_code, result.reason, result.text.encode('utf8')) raise ApiException(msg, method_name, result) try: result_json = result.json() except: - msg = 'The server returned an invalid JSON response. Response body:\n[{0}]'\ + msg = 'The server returned an invalid JSON response. Response body:\n[{0}]' \ .format(result.text.encode('utf8')) raise ApiException(msg, method_name, result) if not result_json['ok']: - msg = 'Error code: {0} Description: {1}'\ + msg = 'Error code: {0} Description: {1}' \ .format(result_json['error_code'], result_json['description']) raise ApiException(msg, method_name, result) return result_json @@ -74,7 +74,7 @@ def download_file(token, file_path): url = FILE_URL.format(token, file_path) result = requests.get(url) if result.status_code != 200: - msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\ + msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]' \ .format(result.status_code, result.reason, result.text) raise ApiException(msg, 'Download file', result) return result.content @@ -258,11 +258,34 @@ def get_method_by_type(data_type): return r'sendSticker' +def answer_inline_query(token, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None): + method_url = 'answerInlineQuery' + payload = {'inline_query_id': inline_query_id, 'results': _convert_inline_results(results)} + if cache_time: + payload['cache_time'] = cache_time + if is_personal: + payload['is_personal'] = is_personal + if next_offset: + payload['next_offset'] = next_offset + return _make_request(token, method_url, params=payload) + + +def _convert_inline_results(results): + ret = '' + for r in results: + if isinstance(r, types.JsonSerializable): + ret = ret + r.to_json() + ',' + if len(ret) > 0: + ret = ret[:-1] + return '[' + ret + ']' + + def _convert_markup(markup): if isinstance(markup, types.JsonSerializable): return markup.to_json() return markup + class ApiException(Exception): """ This class represents an Exception thrown when a call to the Telegram API fails.