Skip to content

Commit 0d0e37d

Browse files
authored
Merge pull request eternnoir#487 from sviat9440/master
Bugfixes and minor improvements
2 parents 9ae20b4 + 36d088d commit 0d0e37d

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

telebot/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ def __notify_update(self, new_messages):
247247
for listener in self.update_listener:
248248
self._exec_task(listener, new_messages)
249249

250+
def infinity_polling(self, *args, **kwargs):
251+
while not self.__stop_polling.is_set():
252+
try:
253+
self.polling(*args, **kwargs)
254+
except Exception as e:
255+
time.sleep(5)
256+
pass
257+
logger.info("Break infinity polling")
258+
250259
def polling(self, none_stop=False, interval=0, timeout=20):
251260
"""
252261
This function creates a new Thread that calls an internal __retrieve_updates function.
@@ -486,7 +495,7 @@ def forward_message(self, chat_id, from_chat_id, message_id, disable_notificatio
486495

487496
def delete_message(self, chat_id, message_id):
488497
"""
489-
Use this method to delete message. Returns True on success.
498+
Use this method to delete message. Returns True on success.
490499
:param chat_id: in which chat to delete
491500
:param message_id: which message to delete
492501
:return: API reply.

telebot/types.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,63 @@ def __init__(self, message_id, from_user, date, chat, content_type, options, jso
438438
setattr(self, key, options[key])
439439
self.json = json_string
440440

441+
@property
442+
def html_text(self):
443+
"""
444+
Author: @sviat9440
445+
Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username"
446+
447+
Example:
448+
message.html_text
449+
>> "<b>Test</b> parse <i>formatting</i>, <a href=\"https://example.com\">url</a>, <a href=\"tg://user?id=123456\">text_mention</a> and mention @username"
450+
451+
Cusom subs:
452+
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.
453+
Example:
454+
message.custom_subs = {"bold": "<strong class=\"example\">{text}</strong>", "italic": "<i class=\"example\">{text}</i>", "mention": "<a href={url}>{text}</a>"}
455+
message.html_text
456+
>> "<strong class=\"example\">Test</strong> parse <i class=\"example\">formatting</i>, <a href=\"https://example.com\">url</a> and <a href=\"tg://user?id=123456\">text_mention</a> and mention <a href=\"https://t.me/username\">@username</a>"
457+
"""
458+
459+
if not self.entities:
460+
return self.text
461+
_subs = {
462+
"bold": "<b>{text}</b>",
463+
"italic": "<i>{text}</i>",
464+
"pre": "<pre>{text}</pre>",
465+
"code": "<code>{text}</code>",
466+
"url": "<a href=\"{url}\">{text}</a>"
467+
}
468+
if hasattr(self, "custom_subs"):
469+
for type in self.custom_subs:
470+
_subs[type] = self.custom_subs[type]
471+
html_text = ""
472+
def func(text, type=None, url=None, user=None):
473+
if type == "text_mention":
474+
type = "url"
475+
url = "tg://user?id={0}".format(user.id)
476+
elif type == "mention":
477+
url = "https://t.me/{0}".format(text[1:])
478+
if not type or not _subs.get(type):
479+
return text
480+
subs = _subs.get(type)
481+
text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
482+
return subs.format(text=text, url=url)
483+
484+
offset = 0
485+
for entity in self.entities:
486+
if entity.type == "bot_command":
487+
entity.offset -= 1
488+
entity.length += 1
489+
if entity.offset > offset:
490+
html_text += func(self.text[offset:entity.offset])
491+
offset = entity.offset
492+
html_text += func(self.text[offset:offset + entity.length], entity.type, entity.url, entity.user)
493+
offset += entity.length
494+
if offset < len(self.text):
495+
html_text += func(self.text[offset:])
496+
return html_text
497+
441498

442499
class MessageEntity(JsonDeserializable):
443500
@classmethod
@@ -1822,7 +1879,7 @@ def __init__(self, id, title):
18221879
def add_price(self, *args):
18231880
"""
18241881
Add LabeledPrice to ShippingOption
1825-
:param args: LabeledPrices
1882+
:param args: LabeledPrices
18261883
"""
18271884
for price in args:
18281885
self.prices.append(price)

0 commit comments

Comments
 (0)