From 292191038f92ec272660b5acf537316451c103c1 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Feb 2016 11:17:35 +0800 Subject: [PATCH 1/2] Add disable_notification --- telebot/__init__.py | 61 +++++++++++++++++++++++++++----------------- telebot/apihelper.py | 36 ++++++++++++++++++++------ 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 1d6d8124b..0fa69262e 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -11,7 +11,7 @@ logger = logging.getLogger('TeleBot') formatter = logging.Formatter( - '%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"' + '%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"' ) console_output_handler = logging.StreamHandler(sys.stderr) @@ -182,9 +182,9 @@ def __threaded_polling(self, none_stop=False, interval=0, timeout=3): polling_thread = util.WorkerThread(name="PollingThread") or_event = util.OrEvent( - polling_thread.done_event, - polling_thread.exception_event, - self.worker_pool.exception_event + polling_thread.done_event, + polling_thread.exception_event, + self.worker_pool.exception_event ) while not self.__stop_polling.wait(interval): @@ -277,7 +277,7 @@ def get_user_profile_photos(self, user_id, offset=None, limit=None): return types.UserProfilePhotos.de_json(result) def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None, - parse_mode=None): + parse_mode=None, disable_notification=None): """ Use this method to send text messages. @@ -290,23 +290,27 @@ def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_me :param reply_to_message_id: :param reply_markup: :param parse_mode: + :param disable_notification: Boolean, Optional. Sends the message silently. :return: API reply. """ return types.Message.de_json( - apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id, - reply_markup, parse_mode)) + apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id, + reply_markup, parse_mode, disable_notification)) - def forward_message(self, chat_id, from_chat_id, message_id): + def forward_message(self, chat_id, from_chat_id, message_id, disable_notification=None): """ Use this method to forward messages of any kind. + :param disable_notification: :param chat_id: which chat to forward :param from_chat_id: which chat message from :param message_id: message id :return: API reply. """ - return types.Message.de_json(apihelper.forward_message(self.token, chat_id, from_chat_id, message_id)) + return types.Message.de_json( + apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification)) - def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None): + def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None, + disable_notification=None): """ Use this method to send photos. :param chat_id: @@ -317,10 +321,11 @@ def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, rep :return: API reply. """ return types.Message.de_json( - apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup)) + apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup, + disable_notification)) def send_audio(self, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None, - reply_markup=None): + reply_markup=None, disable_notification=None): """ Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. :param chat_id:Unique identifier for the message recipient @@ -333,10 +338,11 @@ def send_audio(self, chat_id, audio, duration=None, performer=None, title=None, :return: Message """ return types.Message.de_json( - apihelper.send_audio(self.token, chat_id, audio, duration, performer, title, reply_to_message_id, - reply_markup)) + apihelper.send_audio(self.token, chat_id, audio, duration, performer, title, reply_to_message_id, + reply_markup, disable_notification)) - def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None): + def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None, + disable_notification=None): """ Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. :param chat_id:Unique identifier for the message recipient. @@ -347,9 +353,10 @@ def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, re :return: Message """ return types.Message.de_json( - apihelper.send_voice(self.token, chat_id, voice, duration, reply_to_message_id, reply_markup)) + apihelper.send_voice(self.token, chat_id, voice, duration, reply_to_message_id, reply_markup, + disable_notification)) - def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None): """ Use this method to send general files. :param chat_id: @@ -359,9 +366,10 @@ def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=No :return: API reply. """ return types.Message.de_json( - apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup)) + apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup, + disable_notification)) - def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None): + def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None): """ Use this method to send .webp stickers. :param chat_id: @@ -371,9 +379,11 @@ def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=Non :return: API reply. """ return types.Message.de_json( - apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup)) + apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup, + disable_notification)) - def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None): + def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None, + disable_notification=None): """ Use this method to send video files, Telegram clients support mp4 videos. :param chat_id: Integer : Unique identifier for the message recipient — User or GroupChat id @@ -385,9 +395,11 @@ def send_video(self, chat_id, data, duration=None, caption=None, reply_to_messag :return: """ return types.Message.de_json( - apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup)) + apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup, + disable_notification)) - def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None): + def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None, + disable_notification=None): """ Use this method to send point on the map. :param chat_id: @@ -398,7 +410,8 @@ def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, :return: API reply. """ return types.Message.de_json( - apihelper.send_location(self.token, chat_id, latitude, longitude, reply_to_message_id, reply_markup)) + apihelper.send_location(self.token, chat_id, latitude, longitude, reply_to_message_id, reply_markup, + disable_notification)) def send_chat_action(self, chat_id, action): """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 4c133692e..3053b59ed 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -81,7 +81,7 @@ def download_file(token, file_path): def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None, - parse_mode=None): + parse_mode=None, disable_notification=None): """ Use this method to send text messages. On success, the sent Message is returned. :param token: @@ -102,6 +102,8 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m payload['reply_markup'] = _convert_markup(reply_markup) if parse_mode: payload['parse_mode'] = parse_mode + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload, method='post') @@ -139,13 +141,16 @@ def get_user_profile_photos(token, user_id, offset=None, limit=None): return _make_request(token, method_url, params=payload) -def forward_message(token, chat_id, from_chat_id, message_id): +def forward_message(token, chat_id, from_chat_id, message_id, disable_notification=None): method_url = r'forwardMessage' payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id} + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload) -def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None): +def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None, + disable_notification=None): method_url = r'sendPhoto' payload = {'chat_id': chat_id} files = None @@ -159,16 +164,21 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re payload['reply_to_message_id'] = reply_to_message_id if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload, files=files, method='post') -def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None): +def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None, + disable_notification=None): method_url = r'sendLocation' payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude} if reply_to_message_id: payload['reply_to_message_id'] = reply_to_message_id if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload) @@ -178,7 +188,8 @@ def send_chat_action(token, chat_id, action): return _make_request(token, method_url, params=payload) -def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None): +def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None, + disable_notification=None): method_url = r'sendVideo' payload = {'chat_id': chat_id} files = None @@ -194,10 +205,13 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa payload['reply_to_message_id'] = reply_to_message_id if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload, files=files, method='post') -def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None): +def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None, + disable_notification=None): method_url = r'sendVoice' payload = {'chat_id': chat_id} files = None @@ -211,11 +225,13 @@ def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, r payload['reply_to_message_id'] = reply_to_message_id if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload, files=files, method='post') def send_audio(token, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None, - reply_markup=None): + reply_markup=None, disable_notification=None): method_url = r'sendAudio' payload = {'chat_id': chat_id} files = None @@ -233,10 +249,12 @@ def send_audio(token, chat_id, audio, duration=None, performer=None, title=None, payload['reply_to_message_id'] = reply_to_message_id if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload, files=files, method='post') -def send_data(token, chat_id, data, 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, disable_notification=None): method_url = get_method_by_type(data_type) payload = {'chat_id': chat_id} files = None @@ -248,6 +266,8 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m payload['reply_to_message_id'] = reply_to_message_id if reply_markup: payload['reply_markup'] = _convert_markup(reply_markup) + if disable_notification: + payload['disable_notification'] = disable_notification return _make_request(token, method_url, params=payload, files=files, method='post') From 9b9c0287eca519c926474071616560e9827c443b Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Feb 2016 11:36:53 +0800 Subject: [PATCH 2/2] Add test case. --- tests/test_telebot.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tests/test_telebot.py b/tests/test_telebot.py index dec456a81..e0e23ea09 100644 --- a/tests/test_telebot.py +++ b/tests/test_telebot.py @@ -101,6 +101,16 @@ def test_send_message_with_markdown(self): ret_msg = tb.send_message(CHAT_ID, markdown, parse_mode="Markdown") assert ret_msg.message_id + def test_send_message_with_disable_notification(self): + tb = telebot.TeleBot(TOKEN) + markdown = """ + *bold text* + _italic text_ + [text](URL) + """ + ret_msg = tb.send_message(CHAT_ID, markdown, parse_mode="Markdown", disable_notification=True) + assert ret_msg.message_id + def test_send_file(self): file_data = open('../examples/detailed_example/kitten.jpg', 'rb') tb = telebot.TeleBot(TOKEN) @@ -110,18 +120,39 @@ def test_send_file(self): ret_msg = tb.send_document(CHAT_ID, ret_msg.document.file_id) assert ret_msg.message_id + def test_send_file_dis_noti(self): + file_data = open('../examples/detailed_example/kitten.jpg', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_document(CHAT_ID, file_data, disable_notification=True) + assert ret_msg.message_id + + ret_msg = tb.send_document(CHAT_ID, ret_msg.document.file_id) + assert ret_msg.message_id + def test_send_video(self): file_data = open('./test_data/test_video.mp4', 'rb') tb = telebot.TeleBot(TOKEN) ret_msg = tb.send_video(CHAT_ID, file_data) assert ret_msg.message_id + def test_send_video_dis_noti(self): + file_data = open('./test_data/test_video.mp4', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_video(CHAT_ID, file_data, disable_notification=True) + assert ret_msg.message_id + def test_send_video_more_params(self): file_data = open('./test_data/test_video.mp4', 'rb') tb = telebot.TeleBot(TOKEN) ret_msg = tb.send_video(CHAT_ID, file_data, 1) assert ret_msg.message_id + def test_send_video_more_params_dis_noti(self): + file_data = open('./test_data/test_video.mp4', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_video(CHAT_ID, file_data, 1, disable_notification=True) + assert ret_msg.message_id + def test_send_file_exception(self): tb = telebot.TeleBot(TOKEN) try: @@ -140,6 +171,15 @@ def test_send_photo(self): ret_msg = tb.send_photo(CHAT_ID, ret_msg.photo[0].file_id) assert ret_msg.message_id + def test_send_photo_dis_noti(self): + file_data = open('../examples/detailed_example/kitten.jpg', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_photo(CHAT_ID, file_data) + assert ret_msg.message_id + + ret_msg = tb.send_photo(CHAT_ID, ret_msg.photo[0].file_id, disable_notification=True) + assert ret_msg.message_id + def test_send_audio(self): file_data = open('./test_data/record.mp3', 'rb') tb = telebot.TeleBot(TOKEN) @@ -148,12 +188,26 @@ def test_send_audio(self): assert ret_msg.audio.performer == 'eternnoir' assert ret_msg.audio.title == 'pyTelegram' + def test_send_audio_dis_noti(self): + file_data = open('./test_data/record.mp3', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_audio(CHAT_ID, file_data, 1, 'eternnoir', 'pyTelegram', disable_notification=True) + assert ret_msg.content_type == 'audio' + assert ret_msg.audio.performer == 'eternnoir' + assert ret_msg.audio.title == 'pyTelegram' + def test_send_voice(self): file_data = open('./test_data/record.ogg', 'rb') tb = telebot.TeleBot(TOKEN) ret_msg = tb.send_voice(CHAT_ID, file_data) assert ret_msg.voice.mime_type == 'audio/ogg' + def test_send_voice_dis_noti(self): + file_data = open('./test_data/record.ogg', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True) + assert ret_msg.voice.mime_type == 'audio/ogg' + def test_get_file(self): file_data = open('./test_data/record.ogg', 'rb') tb = telebot.TeleBot(TOKEN) @@ -162,12 +216,26 @@ def test_get_file(self): file_info = tb.get_file(file_id) assert file_info.file_id == file_id + def test_get_file_dis_noti(self): + file_data = open('./test_data/record.ogg', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True) + file_id = ret_msg.voice.file_id + file_info = tb.get_file(file_id) + assert file_info.file_id == file_id + def test_send_message(self): text = 'CI Test Message' tb = telebot.TeleBot(TOKEN) ret_msg = tb.send_message(CHAT_ID, text) assert ret_msg.message_id + def test_send_message_dis_noti(self): + text = 'CI Test Message' + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True) + assert ret_msg.message_id + def test_forward_message(self): text = 'CI forward_message Test Message' tb = telebot.TeleBot(TOKEN) @@ -175,6 +243,13 @@ def test_forward_message(self): ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id) assert ret_msg.forward_from + def test_forward_message_dis_noti(self): + text = 'CI forward_message Test Message' + tb = telebot.TeleBot(TOKEN) + msg = tb.send_message(CHAT_ID, text) + ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id, disable_notification=True) + assert ret_msg.forward_from + def test_reply_to(self): text = 'CI reply_to Test Message' tb = telebot.TeleBot(TOKEN) @@ -203,6 +278,14 @@ def test_send_location(self): assert int(ret_msg.location.longitude) == int(lon) assert int(ret_msg.location.latitude) == int(lat) + def test_send_location_dis_noti(self): + tb = telebot.TeleBot(TOKEN) + lat = 26.3875591 + lon = -161.2901042 + ret_msg = tb.send_location(CHAT_ID, lat, lon, disable_notification=True) + assert int(ret_msg.location.longitude) == int(lon) + assert int(ret_msg.location.latitude) == int(lat) + def test_Chat(self): tb = telebot.TeleBot(TOKEN) me = tb.get_me()