From 70b63fddc17771411dfba5aa4d4e68c8e20fa37c Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Jun 2015 01:53:07 +0800 Subject: [PATCH 1/4] send file methods done. --- README.md | 10 +++++----- telebot/__init__.py | 17 ++++++++++++++++- telebot/apihelper.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dae46a2..5863c8e 100644 --- a/README.md +++ b/README.md @@ -65,11 +65,11 @@ while True: - [x] getMe - [x] sendMessage - [x] forwardMessage -- [ ] sendPhoto -- [ ] sendAudio -- [ ] sendDocument -- [ ] sendSticker -- [ ] sendVideo +- [x] sendPhoto +- [x] sendAudio +- [x] sendDocument +- [x] sendSticker +- [x] sendVideo - [ ] sendLocation - [ ] sendChatAction - [ ] getUserProfilePhotos diff --git a/telebot/__init__.py b/telebot/__init__.py index cb2c141..33f2f4a 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -112,10 +112,25 @@ class TeleBot: def forward_message(self, chat_id, from_chat_id, message_id): """ - + Use this method to forward messages of any kind. :param chat_id: which chat to forward :param from_chat_id: which chat message from :param message_id: message id :return: """ return apihelper.forward_message(self.token, chat_id, from_chat_id, message_id) + + def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None): + return apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup) + + def send_audio(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + return apihelper.send_data(self.token, chat_id, data, 'audio', reply_to_message_id, reply_markup) + + def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + return apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup) + + def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + return apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup) + + def send_video(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + return apihelper.send_data(self.token, chat_id, data, 'video', reply_to_message_id, reply_markup) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 93720f9..0ced9ac 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -52,3 +52,42 @@ def forward_message(token, chat_id, from_chat_id, message_id): payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id} req = requests.get(request_url, params=payload) return req.json() + + +def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None): + api_url = telebot.API_URL + method_url = r'sendPhoto' + request_url = api_url + 'bot' + token + '/' + method_url + payload = {'chat_id': chat_id} + files = {'photo': photo} + if caption: + payload['caption'] = caption + if reply_to_message_id: + payload['reply_to_message_id'] = reply_to_message_id + if reply_markup: + payload['reply_markup'] = reply_markup + req = requests.post(request_url, params=payload, files=files) + return req.json() + +def send_data(token, chat_id, data, type, reply_to_message_id=None, reply_markup=None): + api_url = telebot.API_URL + method_url = get_method_by_type(type) + request_url = api_url + 'bot' + token + '/' + method_url + payload = {'chat_id': chat_id} + files = {type: data} + if reply_to_message_id: + payload['reply_to_message_id'] = reply_to_message_id + if reply_markup: + payload['reply_markup'] = reply_markup + req = requests.post(request_url, params=payload, files=files) + return req.json() + +def get_method_by_type(type): + if type == 'audio': + return 'sendAudio' + if type == 'document': + return 'sendDocument' + if type == 'sticker': + return 'sendSticker' + if type == 'video': + return 'sendVideo' From a62139fafc4b1ac1e54eacfc4559700af8f0850c Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Jun 2015 02:14:45 +0800 Subject: [PATCH 2/4] Add some comment --- telebot/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ telebot/apihelper.py | 16 ++++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 33f2f4a..8d06b8c 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -121,16 +121,58 @@ class TeleBot: return apihelper.forward_message(self.token, chat_id, from_chat_id, message_id) def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None): + """ + Use this method to send photos. + :param chat_id: + :param photo: + :param caption: + :param reply_to_message_id: + :param reply_markup: + :return: + """ return apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup) def send_audio(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + """ + Use this method to send audio files, if you want Telegram clients to display the file as a playable + voice message. For this to work, your audio must be in an .ogg file encoded with OPUS + :param chat_id: + :param data: + :param reply_to_message_id: + :param reply_markup: + :return: + """ return apihelper.send_data(self.token, chat_id, data, 'audio', reply_to_message_id, reply_markup) def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + """ + Use this method to send general files. + :param chat_id: + :param data: + :param reply_to_message_id: + :param reply_markup: + :return: + """ return apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup) def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + """ + Use this method to send .webp stickers. + :param chat_id: + :param data: + :param reply_to_message_id: + :param reply_markup: + :return: + """ return apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup) def send_video(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + """ + Use this method to send video files, Telegram clients support mp4 videos. + :param chat_id: + :param data: + :param reply_to_message_id: + :param reply_markup: + :return: + """ return apihelper.send_data(self.token, chat_id, data, 'video', reply_to_message_id, reply_markup) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 0ced9ac..65fa75e 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -69,12 +69,12 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re req = requests.post(request_url, params=payload, files=files) return req.json() -def send_data(token, chat_id, data, type, reply_to_message_id=None, reply_markup=None): +def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None): api_url = telebot.API_URL - method_url = get_method_by_type(type) + method_url = get_method_by_type(data_type) request_url = api_url + 'bot' + token + '/' + method_url payload = {'chat_id': chat_id} - files = {type: data} + files = {data_type: data} if reply_to_message_id: payload['reply_to_message_id'] = reply_to_message_id if reply_markup: @@ -82,12 +82,12 @@ def send_data(token, chat_id, data, type, reply_to_message_id=None, reply_markup req = requests.post(request_url, params=payload, files=files) return req.json() -def get_method_by_type(type): - if type == 'audio': +def get_method_by_type(data_type): + if data_type == 'audio': return 'sendAudio' - if type == 'document': + if data_type == 'document': return 'sendDocument' - if type == 'sticker': + if data_type == 'sticker': return 'sendSticker' - if type == 'video': + if data_type == 'video': return 'sendVideo' From cc4ff121035ffa605bc7aab02e409b755ed822f1 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Jun 2015 10:03:02 +0800 Subject: [PATCH 3/4] Change telebot polling thread to daemon. fix #2 --- telebot/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/telebot/__init__.py b/telebot/__init__.py index 8d06b8c..2c304a2 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -83,6 +83,7 @@ class TeleBot: time.sleep(interval + 1) self.__stop_polling = False self.polling_thread = threading.Thread(target=self.__polling, args=()) + self.polling_thread.daemon = True self.polling_thread.start() def __polling(self): From b3c5f402ef90e50092a9d7e4b0f6f3645bc991b4 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Jun 2015 10:25:22 +0800 Subject: [PATCH 4/4] Add API usage. --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5863c8e..6338613 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,52 @@ while True: time.sleep(20) ``` +## TeleBot API usage + +***NOTICE*** : Message type only support text now. + +```python +import telebot +import time + +TOKEN = '' +tb = telebot.TeleBot(TOKEN) #create new Telegram Bot object + +# getMe +user = tb.get_me() + +# sendMessage +tb.send_message(chatid, text) + +# forwardMessage +# tb.forward_message(10894,926,3) +tb.forward_message(to_chat_id, from_chat_id, message_id) + +# sendPhoto +photo = open('/tmp/photo.png', 'rb') +tb.send_photo(chat_id, photo) + +# sendAudio +audio = open('/tmp/audio.ogg', 'rb') +tb.send_audio(chat_id, audio) + +# sendDocument +doc = open('/tmp/file.txt', 'rb') +tb.send_document(chat_id, doc) + +# sendSticker +sti = open('/tmp/sti.webp', 'rb') +tb.send_sticker(chat_id, sti) + +# sendVideo +video = open('/tmp/video.mp4', 'rb') +tb.send_video(chat_id, video) + +``` + ## TODO +- [ ] Let message not only support text. - [x] getMe - [x] sendMessage - [x] forwardMessage @@ -73,4 +117,4 @@ while True: - [ ] sendLocation - [ ] sendChatAction - [ ] getUserProfilePhotos -- [x] getUpdates +- [ ] getUpdates (Only text message support now.)