diff --git a/telebot/__init__.py b/telebot/__init__.py index aa74d11..96e7e3d 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -247,6 +247,15 @@ class TeleBot: for listener in self.update_listener: self._exec_task(listener, new_messages) + def infinity_polling(self, *args, **kwargs): + while not self.__stop_polling.is_set(): + try: + self.polling(*args, **kwargs) + except Exception as e: + time.sleep(5) + pass + logger.info("Break infinity polling") + def polling(self, none_stop=False, interval=0, timeout=20): """ This function creates a new Thread that calls an internal __retrieve_updates function. @@ -486,7 +495,7 @@ class TeleBot: def delete_message(self, chat_id, message_id): """ - Use this method to delete message. Returns True on success. + Use this method to delete message. Returns True on success. :param chat_id: in which chat to delete :param message_id: which message to delete :return: API reply. diff --git a/telebot/types.py b/telebot/types.py index 7af3ac4..f996436 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -438,6 +438,63 @@ class Message(JsonDeserializable): setattr(self, key, options[key]) self.json = json_string + @property + def html_text(self): + """ + Author: @sviat9440 + Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username" + + Example: + message.html_text + >> "Test parse formatting, url, text_mention and mention @username" + + Cusom subs: + You can customize the substitutes. By default, there is no substitute for the entities: hashtag, bot_command, email. You can add or modify substitute an existing entity. + Example: + message.custom_subs = {"bold": "{text}", "italic": "{text}", "mention": "{text}"} + message.html_text + >> "Test parse formatting, url and text_mention and mention @username" + """ + + if not self.entities: + return self.text + _subs = { + "bold": "{text}", + "italic": "{text}", + "pre": "
{text}
", + "code": "{text}", + "url": "{text}" + } + if hasattr(self, "custom_subs"): + for type in self.custom_subs: + _subs[type] = self.custom_subs[type] + html_text = "" + def func(text, type=None, url=None, user=None): + if type == "text_mention": + type = "url" + url = "tg://user?id={0}".format(user.id) + elif type == "mention": + url = "https://t.me/{0}".format(text[1:]) + if not type or not _subs.get(type): + return text + subs = _subs.get(type) + text = text.replace("&", "&").replace("<", "<").replace(">", ">") + return subs.format(text=text, url=url) + + offset = 0 + for entity in self.entities: + if entity.type == "bot_command": + entity.offset -= 1 + entity.length += 1 + if entity.offset > offset: + html_text += func(self.text[offset:entity.offset]) + offset = entity.offset + html_text += func(self.text[offset:offset + entity.length], entity.type, entity.url, entity.user) + offset += entity.length + if offset < len(self.text): + html_text += func(self.text[offset:]) + return html_text + class MessageEntity(JsonDeserializable): @classmethod @@ -1822,7 +1879,7 @@ class ShippingOption(JsonSerializable): def add_price(self, *args): """ Add LabeledPrice to ShippingOption - :param args: LabeledPrices + :param args: LabeledPrices """ for price in args: self.prices.append(price)