Skip to content

Commit 01c1c7e

Browse files
authored
Merge pull request #78 from andresdelfino/move_to_MarkdownV2
Move to MarkdownV2, add escape_markdown
2 parents b211974 + 7822bd1 commit 01c1c7e

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

src/pycamp_bot/commands/announcements.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pycamp_bot.commands.auth import get_admins_username
55
from pycamp_bot.logger import logger
66
from pycamp_bot.commands.manage_pycamp import active_needed
7+
from pycamp_bot.utils import escape_markdown
78

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

@@ -78,8 +79,8 @@ async def announce(update: Update, context: CallbackContext) -> str:
7879
if len(_projects) == 0:
7980
await context.bot.send_message(
8081
chat_id=update.message.chat_id,
81-
text=f"No existe el proyecto: *{state.p_name}*.",
82-
parse_mode='Markdown'
82+
text=f"No existe el proyecto: *{escape_markdown(state.p_name)}*.",
83+
parse_mode='MarkdownV2'
8384
)
8485
return ConversationHandler.END
8586
elif not await should_be_able_to_announce(state.username, _projects[0]):
@@ -92,8 +93,8 @@ async def announce(update: Update, context: CallbackContext) -> str:
9293
else:
9394
await context.bot.send_message(
9495
chat_id=update.message.chat_id,
95-
text=f"Anunciando el proyecto: *{_projects[0].name.capitalize()}* !!!",
96-
parse_mode='Markdown'
96+
text=f"Anunciando el proyecto: *{escape_markdown(_projects[0].name).capitalize()}* !!!",
97+
parse_mode='MarkdownV2'
9798
)
9899
state.owner = _projects[0].owner.username
99100
state.current_project = _projects[0]
@@ -184,20 +185,20 @@ async def message_project(update: Update, context: CallbackContext) -> str:
184185
try:
185186
await context.bot.send_message(
186187
chat_id=chat_id,
187-
text=f'''Está por empezar el proyecto *"{(state.p_name).capitalize()}"* a cargo de *@{state.owner}*.\n*¿Dónde?* 👉🏼 {state.lugar}''',
188-
parse_mode='Markdown'
188+
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)}''',
189+
parse_mode='MarkdownV2'
189190
)
190191
if update.message.from_user.username == state.owner:
191192
await context.bot.send_message(
192193
chat_id=chat_id,
193-
text=f'*Project Owner says:* **{state.mensaje}**',
194-
parse_mode='Markdown'
194+
text=f'*Project Owner says:* **{escape_markdown(state.mensaje)}**',
195+
parse_mode='MarkdownV2'
195196
)
196197
else:
197198
await context.bot.send_message(
198199
chat_id=chat_id,
199-
text=f'Admin *@{update.message.from_user.username}* says: **{state.mensaje}**',
200-
parse_mode='Markdown'
200+
text=f'Admin *@{escape_markdown(update.message.from_user.username)}* says: **{escape_markdown(state.mensaje)}**',
201+
parse_mode='MarkdownV2'
201202
)
202203
except Exception as e:
203204
logger.error(f"Error al enviar el mensaje: {e}")

src/pycamp_bot/commands/manage_pycamp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pycamp_bot.models import PycampistaAtPycamp
66
from pycamp_bot.commands.auth import admin_needed
77
from pycamp_bot.logger import logger
8+
from pycamp_bot.utils import escape_markdown
89

910

1011
SET_DATE_STATE = "set_fate"

src/pycamp_bot/commands/projects.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
77
from pycamp_bot.commands.auth import admin_needed, get_admins_username
88
from pycamp_bot.commands.schedule import DIAS
9+
from pycamp_bot.utils import escape_markdown
910

1011

1112
current_projects = {}
@@ -287,8 +288,8 @@ async def show_my_projects(update, context):
287288

288289
project_lines = [
289290
f'{vote.project.slot.start}:00',
290-
vote.project.name,
291-
f'Owner: @{vote.project.owner.username}',
291+
escape_markdown(vote.project.name),
292+
f'Owner: @{escape_markdown(vote.project.owner.username)}',
292293
]
293294

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

302-
await update.message.reply_text(text, parse_mode='Markdown')
303+
await update.message.reply_text(text, parse_mode='MarkdownV2')
303304

304305

305306
def set_handlers(application):

src/pycamp_bot/commands/schedule.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pycamp_bot.commands.auth import admin_needed
55
from pycamp_bot.scheduler.db_to_json import export_db_2_json
66
from pycamp_bot.scheduler.schedule_calculator import export_scheduled_result
7+
from pycamp_bot.utils import escape_markdown
78

89

910
DAY_SLOT_TIME = {
@@ -172,13 +173,13 @@ async def show_schedule(update, context):
172173

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

178179
await context.bot.send_message(
179180
chat_id=update.message.chat_id,
180181
text=cronograma,
181-
parse_mode='Markdown'
182+
parse_mode='MarkdownV2'
182183
)
183184

184185

src/pycamp_bot/commands/wizard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pycamp_bot.commands.auth import admin_needed
99
from pycamp_bot.commands.manage_pycamp import get_active_pycamp
1010
from pycamp_bot.logger import logger
11+
from pycamp_bot.utils import escape_markdown
1112

1213

1314
LUNCH_TIME_START_HOUR = 13
@@ -260,7 +261,7 @@ def format_wizards_schedule(agenda):
260261
msg += "\t \\- {} a {}:\t*{}* \n".format(
261262
i.init.strftime("%H:%M"),
262263
i.end.strftime("%H:%M"),
263-
"@" + i.wizard.username
264+
"@" + escape_markdown(i.wizard.username)
264265
)
265266
return msg
266267

src/pycamp_bot/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def escape_markdown(string):
2+
# See: https://core.telegram.org/bots/api#markdownv2-style
3+
4+
new_string = string
5+
6+
for char in "_*[]()~`>#+-=|{}.!":
7+
new_string = new_string.replace(char, f'\\{char}')
8+
9+
return new_string

0 commit comments

Comments
 (0)