Skip to content

Commit

Permalink
#152, probably fixed :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Ableev committed Mar 16, 2019
1 parent 5b942f0 commit 7bf5f18
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions zbxtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ def ok_update(self):
return True


def markdown_fix(message, offset):
def markdown_fix(message, offset, emoji=False):
offset = int(offset)
if emoji: # https://github.com/ableev/Zabbix-in-Telegram/issues/152
offset -= 2
message = "\n".join(message)
message = message[:offset] + message[offset+1:]
message = message.split("\n")
Expand Down Expand Up @@ -822,13 +824,16 @@ def main():
pass

# replace text with emojis
internal_using_emoji = False # I hate that, but... https://github.com/ableev/Zabbix-in-Telegram/issues/152
if hasattr(zbxtg_settings, "emoji_map"):
zbxtg_body_text_emoji_support = []
for l in zbxtg_body_text:
l_new = l
for k, v in list(zbxtg_settings.emoji_map.items()):
l_new = l_new.replace("{{" + k + "}}", v)
zbxtg_body_text_emoji_support.append(l_new)
if len("".join(zbxtg_body_text)) - len("".join(zbxtg_body_text_emoji_support)):
internal_using_emoji = True
zbxtg_body_text = zbxtg_body_text_emoji_support

if not is_single_message:
Expand All @@ -844,15 +849,19 @@ def main():

# another case if markdown is enabled and we got parse error, try to remove "bad" symbols from message
if tg.markdown and tg.error.find("Can't find end of the entity starting at byte offset") > -1:
markdown_warning = "Original message has been fixed due to {0}. " \
"Please, fix the markdown, it's slowing down messages sending."\
.format(url_wiki_base + "/" + settings_description["markdown"]["url"])
markdown_fix_attempts = 0
while not tg.ok and markdown_fix_attempts != 10:
while not tg.ok and markdown_fix_attempts != 3:
offset = re.search("Can't find end of the entity starting at byte offset ([0-9]+)", tg.error).group(1)
zbxtg_body_text = markdown_fix(zbxtg_body_text, offset)
zbxtg_body_text = markdown_fix(zbxtg_body_text, offset, emoji=internal_using_emoji) + \
["\n"] + [markdown_warning]
tg.disable_web_page_preview = True
tg.send_message(uid, zbxtg_body_text)
markdown_fix_attempts += 1
if tg.ok:
print_message("Original message has been fixed due to {0}"
.format(url_wiki_base + "/" + settings_description["markdown"]["url"]))
print_message(markdown_warning)

if is_debug:
print((tg.result))
Expand Down

0 comments on commit 7bf5f18

Please sign in to comment.