diff --git a/README.md b/README.md index cf0db6d..5a24107 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ while True: ## TODO - [x] getMe -- [ ] sendMessage +- [x] sendMessage - [ ] forwardMessage - [ ] sendPhoto - [ ] sendAudio diff --git a/telebot/__init__.py b/telebot/__init__.py index 12c7cd5..dde1b9f 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -36,6 +36,8 @@ class TeleBot: self.update_entries = {} self.update_listener = [] self.chat_list = {} + self.update_id_list = [] + self.max_message_size = 100000 self.polling_thread = None self.__stop_polling = False self.interval = 3 @@ -50,23 +52,35 @@ class TeleBot: if update['update_id'] in self.update_entries: continue msg = types.Message.de_json(json.dumps(update['message'])) - self.update_entries[update['update_id']] = msg + self.__append_message_to_cache(update['update_id'], msg) notify_updates.append(msg) self.__notify_update(notify_updates) + def __append_message_to_cache(self, update_id, message): + # over buffer size + if len(self.update_id_list) > self.max_message_size: + # remove oldest element. + upid = self.update_id_list[0] + if upid in self.update_entries: + del self.update_entries[upid] + self.update_entries[update_id] = message + self.update_id_list.append(update_id) + def __notify_update(self, new_messages): for listener in self.update_listener: t = threading.Thread(target=listener, args=(new_messages)) t.start() - def polling(self, interval): + def polling(self, interval=3): """ Always get updates. :param interval: iterval secs. :return: """ + self.interval = interval + # clear thread. self.__stop_polling = True - time.sleep(1) + time.sleep(interval + 1) self.__stop_polling = False self.polling_thread = threading.Thread(target=self.__polling, args=()) self.polling_thread.start() @@ -95,3 +109,6 @@ class TeleBot: def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None): return apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id, reply_markup) + + def forward_message(self, chat_id, from_chat_id, message_id): + return apihelper.forward_message(self.token, chat_id, from_chat_id, message_id) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index b09b0d9..effdfc4 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -40,3 +40,11 @@ def get_updates(token): request_url = api_url+'bot'+token+'/'+method_url req = requests.get(request_url) return req.json() + +def forward_message(token,chat_id,from_chat_id,message_id): + api_url = telebot.API_URL + method_url = r'forwardMessage' + request_url = api_url+'bot'+token+'/'+method_url + 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()