Skip to content

Commit

Permalink
Merge pull request #78 from andresdelfino/move_to_MarkdownV2
Browse files Browse the repository at this point in the history
Move to MarkdownV2, add escape_markdown
  • Loading branch information
WinnaZ authored Jun 22, 2024
2 parents b211974 + 7822bd1 commit 01c1c7e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
21 changes: 11 additions & 10 deletions src/pycamp_bot/commands/announcements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pycamp_bot.commands.auth import get_admins_username
from pycamp_bot.logger import logger
from pycamp_bot.commands.manage_pycamp import active_needed
from pycamp_bot.utils import escape_markdown

PROYECTO, LUGAR, MENSAJE = ["proyecto", "lugar", "mensaje"]

Expand Down Expand Up @@ -78,8 +79,8 @@ async def announce(update: Update, context: CallbackContext) -> str:
if len(_projects) == 0:
await context.bot.send_message(
chat_id=update.message.chat_id,
text=f"No existe el proyecto: *{state.p_name}*.",
parse_mode='Markdown'
text=f"No existe el proyecto: *{escape_markdown(state.p_name)}*.",
parse_mode='MarkdownV2'
)
return ConversationHandler.END
elif not await should_be_able_to_announce(state.username, _projects[0]):
Expand All @@ -92,8 +93,8 @@ async def announce(update: Update, context: CallbackContext) -> str:
else:
await context.bot.send_message(
chat_id=update.message.chat_id,
text=f"Anunciando el proyecto: *{_projects[0].name.capitalize()}* !!!",
parse_mode='Markdown'
text=f"Anunciando el proyecto: *{escape_markdown(_projects[0].name).capitalize()}* !!!",
parse_mode='MarkdownV2'
)
state.owner = _projects[0].owner.username
state.current_project = _projects[0]
Expand Down Expand Up @@ -184,20 +185,20 @@ async def message_project(update: Update, context: CallbackContext) -> str:
try:
await context.bot.send_message(
chat_id=chat_id,
text=f'''Está por empezar el proyecto *"{(state.p_name).capitalize()}"* a cargo de *@{state.owner}*.\n*¿Dónde?* 👉🏼 {state.lugar}''',
parse_mode='Markdown'
text=f'''Está por empezar el proyecto *"{escape_markdown(state.p_name).capitalize()}"* a cargo de *@{escape_markdown(state.owner)}*.\n*¿Dónde?* 👉🏼 {escape_markdown(state.lugar)}''',
parse_mode='MarkdownV2'
)
if update.message.from_user.username == state.owner:
await context.bot.send_message(
chat_id=chat_id,
text=f'*Project Owner says:* **{state.mensaje}**',
parse_mode='Markdown'
text=f'*Project Owner says:* **{escape_markdown(state.mensaje)}**',
parse_mode='MarkdownV2'
)
else:
await context.bot.send_message(
chat_id=chat_id,
text=f'Admin *@{update.message.from_user.username}* says: **{state.mensaje}**',
parse_mode='Markdown'
text=f'Admin *@{escape_markdown(update.message.from_user.username)}* says: **{escape_markdown(state.mensaje)}**',
parse_mode='MarkdownV2'
)
except Exception as e:
logger.error(f"Error al enviar el mensaje: {e}")
Expand Down
1 change: 1 addition & 0 deletions src/pycamp_bot/commands/manage_pycamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pycamp_bot.models import PycampistaAtPycamp
from pycamp_bot.commands.auth import admin_needed
from pycamp_bot.logger import logger
from pycamp_bot.utils import escape_markdown


SET_DATE_STATE = "set_fate"
Expand Down
7 changes: 4 additions & 3 deletions src/pycamp_bot/commands/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
from pycamp_bot.commands.auth import admin_needed, get_admins_username
from pycamp_bot.commands.schedule import DIAS
from pycamp_bot.utils import escape_markdown


current_projects = {}
Expand Down Expand Up @@ -287,8 +288,8 @@ async def show_my_projects(update, context):

project_lines = [
f'{vote.project.slot.start}:00',
vote.project.name,
f'Owner: @{vote.project.owner.username}',
escape_markdown(vote.project.name),
f'Owner: @{escape_markdown(vote.project.owner.username)}',
]

text_chunks.append('\n'.join(project_lines))
Expand All @@ -299,7 +300,7 @@ async def show_my_projects(update, context):
else:
text = "No votaste por ningún proyecto"

await update.message.reply_text(text, parse_mode='Markdown')
await update.message.reply_text(text, parse_mode='MarkdownV2')


def set_handlers(application):
Expand Down
7 changes: 4 additions & 3 deletions src/pycamp_bot/commands/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pycamp_bot.commands.auth import admin_needed
from pycamp_bot.scheduler.db_to_json import export_db_2_json
from pycamp_bot.scheduler.schedule_calculator import export_scheduled_result
from pycamp_bot.utils import escape_markdown


DAY_SLOT_TIME = {
Expand Down Expand Up @@ -172,13 +173,13 @@ async def show_schedule(update, context):

for project in projects:
if project.slot_id == slot.id:
cronograma += f'*-* {slot.start}:00hs = *{(project.name).capitalize()}.*\n'
cronograma += f'A cargo de 👉🏼 {"@" + project.owner.username}\n'
cronograma += f'*-* {slot.start}:00hs = *{escape_markdown(project.name).capitalize()}.*\n'
cronograma += f'A cargo de 👉🏼 {"@" + escape_markdown(project.owner.username)}\n'

await context.bot.send_message(
chat_id=update.message.chat_id,
text=cronograma,
parse_mode='Markdown'
parse_mode='MarkdownV2'
)


Expand Down
3 changes: 2 additions & 1 deletion src/pycamp_bot/commands/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pycamp_bot.commands.auth import admin_needed
from pycamp_bot.commands.manage_pycamp import get_active_pycamp
from pycamp_bot.logger import logger
from pycamp_bot.utils import escape_markdown


LUNCH_TIME_START_HOUR = 13
Expand Down Expand Up @@ -260,7 +261,7 @@ def format_wizards_schedule(agenda):
msg += "\t \\- {} a {}:\t*{}* \n".format(
i.init.strftime("%H:%M"),
i.end.strftime("%H:%M"),
"@" + i.wizard.username
"@" + escape_markdown(i.wizard.username)
)
return msg

Expand Down
9 changes: 9 additions & 0 deletions src/pycamp_bot/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def escape_markdown(string):
# See: https://core.telegram.org/bots/api#markdownv2-style

new_string = string

for char in "_*[]()~`>#+-=|{}.!":
new_string = new_string.replace(char, f'\\{char}')

return new_string

0 comments on commit 01c1c7e

Please sign in to comment.