Skip to content

Commit

Permalink
Merge pull request #246 from nlef/html-formatting
Browse files Browse the repository at this point in the history
add html formatting support for user notifications
  • Loading branch information
nlef authored Feb 5, 2023
2 parents cf19c2c + 3aea0b9 commit b9b4700
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions bot/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from typing import Any, Callable, List, Optional, Union

from telegram.utils.helpers import escape_markdown
from telegram.utils.helpers import escape


class ConfigHelper:
Expand Down Expand Up @@ -424,9 +424,9 @@ def __init__(self, config: configparser.ConfigParser):
def configuration_errors(self) -> str:
error_message: str = ""
if self.unknown_fields:
error_message += escape_markdown(f"\n{self.unknown_fields}", version=2)
error_message += escape(f"\n{self.unknown_fields}")
if self.parsing_errors:
error_message += escape_markdown(f"\n{self.parsing_errors}", version=2)
error_message += escape(f"\n{self.parsing_errors}")
if error_message:
error_message += "Please correct the configuration according to the [wiki](https://github.com/nlef/moonraker-telegram-bot/wiki/Sample-config)"
error_message += 'Please correct the configuration according to the <a href="https://github.com/nlef/moonraker-telegram-bot/wiki">wiki</a>'
return error_message
36 changes: 18 additions & 18 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
ReplyKeyboardMarkup,
Update,
)
from telegram.constants import PARSEMODE_MARKDOWN_V2
from telegram.constants import PARSEMODE_HTML
from telegram.error import BadRequest
from telegram.ext import CallbackContext, CallbackQueryHandler, CommandHandler, Filters, MessageHandler, Updater
from telegram.utils.helpers import escape_markdown
from telegram.utils.helpers import escape
from websocket_helper import WebSocketHelper

from camera import Camera
Expand Down Expand Up @@ -130,10 +130,10 @@ def unknown_chat(update: Update, _: CallbackContext) -> None:
if update.effective_chat.id < 0 or update.effective_message is None:
return

mess = f"Unauthorized access detected with chat_id: {update.effective_chat.id}.\n||This incident will be reported.||"
mess = f"Unauthorized access detected with chat_id: {update.effective_chat.id}.\n<tg-spoiler>This incident will be reported.</tg-spoiler>"
update.effective_message.reply_text(
escape_markdown(mess, version=2),
parse_mode=PARSEMODE_MARKDOWN_V2,
mess,
parse_mode=PARSEMODE_HTML,
quote=True,
)
logger.error("Unauthorized access detected from `%s` with chat_id `%s`. Message: %s", update.effective_chat.username, update.effective_chat.id, update.effective_message.to_json())
Expand All @@ -149,22 +149,22 @@ def status(update: Update, _: CallbackContext) -> None:
time.sleep(configWrap.camera.light_timeout + 1.5)
update.effective_message.delete()
else:
mess = escape_markdown(klippy.get_status(), version=2)
mess = escape(klippy.get_status())
if cameraWrap.enabled:
with cameraWrap.take_photo() as bio:
update.effective_message.bot.send_chat_action(chat_id=configWrap.secrets.chat_id, action=ChatAction.UPLOAD_PHOTO)
update.effective_message.reply_photo(
photo=bio,
caption=mess,
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
disable_notification=notifier.silent_commands,
)
bio.close()
else:
update.effective_message.bot.send_chat_action(chat_id=configWrap.secrets.chat_id, action=ChatAction.TYPING)
update.effective_message.reply_text(
mess,
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
disable_notification=notifier.silent_commands,
quote=True,
)
Expand Down Expand Up @@ -380,7 +380,7 @@ def light_toggle(update: Update, _: CallbackContext) -> None:
mess = f"Device `{light_power_device.name}` toggled " + ("on" if light_power_device.toggle_device() else "off")
update.effective_message.reply_text(
mess,
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
disable_notification=notifier.silent_commands,
quote=True,
)
Expand Down Expand Up @@ -559,15 +559,15 @@ def button_handler(update: Update, context: CallbackContext) -> None:
psu_power_device.switch_device(False)
update.effective_message.reply_to_message.reply_text(
f"Device `{psu_power_device.name}` toggled off",
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
quote=True,
)
query.delete_message()
elif query.data == "power_on_printer":
psu_power_device.switch_device(True)
update.effective_message.reply_to_message.reply_text(
f"Device `{psu_power_device.name}` toggled on",
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
quote=True,
)
query.delete_message()
Expand Down Expand Up @@ -937,13 +937,13 @@ def help_command(update: Update, _: CallbackContext) -> None:
logger.warning("Undefined effective message")
return
mess = (
escape_markdown(klippy.get_versions_info(bot_only=True), version=2)
+ escape_markdown("\n".join([f"/{c} - {a}" for c, a in bot_commands().items()]), version=2)
+ "\n\nPlease refer to the [wiki](https://github.com/nlef/moonraker-telegram-bot/wiki) for additional information"
escape(klippy.get_versions_info(bot_only=True))
+ escape("\n".join([f"/{c} - {a}" for c, a in bot_commands().items()]))
+ '\n\nPlease refer to the <a href="https://github.com/nlef/moonraker-telegram-bot/wiki">wiki</a> for additional information'
)
update.effective_message.reply_text(
text=mess,
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
quote=True,
)

Expand Down Expand Up @@ -976,17 +976,17 @@ def greeting_message(bot: telegram.Bot) -> None:
response = klippy.check_connection()
mess = ""
if response:
mess += escape_markdown(f"Bot online, no moonraker connection!\n {response} \nFailing...", version=2)
mess += escape(f"Bot online, no moonraker connection!\n {response} \nFailing...")
else:
mess += "Printer online"
if configWrap.configuration_errors:
mess += escape_markdown(klippy.get_versions_info(bot_only=True), version=2) + configWrap.configuration_errors
mess += escape(klippy.get_versions_info(bot_only=True)) + configWrap.configuration_errors

reply_markup = ReplyKeyboardMarkup(create_keyboard(), resize_keyboard=True)
bot.send_message(
configWrap.secrets.chat_id,
text=mess,
parse_mode=PARSEMODE_MARKDOWN_V2,
parse_mode=PARSEMODE_HTML,
reply_markup=reply_markup,
disable_notification=notifier.silent_status,
)
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ memory_profiler==0.61.0
mypy==0.982
numpy~=1.21.6
opencv-python~=3.4.17.63
pre-commit==3.0.3
pre-commit==3.0.4
pylint==2.16.1
pytest==7.2.1
types-backports==0.1.3
Expand Down

0 comments on commit b9b4700

Please sign in to comment.