Skip to content

Commit

Permalink
Merge pull request #61 from chameleon-lizard/fix/PMB-27/fix_history
Browse files Browse the repository at this point in the history
Fix/pmb 27/fix history
  • Loading branch information
chameleon-lizard authored Feb 28, 2024
2 parents dbebe1d + a72bb72 commit 465e4a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def history(message: telebot.types.Message) -> None:
return

# Else getting only last 10 songs to limit the amount of spam
song_history = response['Result'][:10]
song_history = response['Result'][::-1][:10]

# Sending song info
bot.reply_to(
Expand Down
69 changes: 67 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,71 @@ def download_song(url: str, suggested_by: User) -> Song:
return video


def format_for_telegram(text: str) -> str:
"""
Formats text for telegram, escaping the following characters:
_ with \_;
* with \*;
[ with \[;
] with \];
( with \(;
) with \);
~ with \~;
` with \`;
> with \>;
# with \#;
+ with \+;
- with \-;
= with \=;
| with \|;
{ with \{;
} with \};
. with \.;
! with \!;
:param text: Text to format
:return: Formatted text
"""
return text.replace(
'_', '\_'
).replace(
'*', '\*'
).replace(
'[', '\['
).replace(
']', '\]'
).replace(
'(', '\('
).replace(
')', '\)'
).replace(
'~', '\~'
).replace(
'`', '\`'
).replace(
'>', '\>'
).replace(
'#', '\#'
).replace(
'+', '\+'
).replace(
'-', '\-'
).replace(
'=', '\='
).replace(
'|', '\|'
).replace(
'{', '\{'
).replace(
'}', '\}'
).replace(
'.', '\.'
).replace(
'!', '\!'
)


def get_song_text(song_dict: dict) -> str:
"""
Formats the text for the bot to send.
Expand All @@ -165,8 +230,8 @@ def get_song_text(song_dict: dict) -> str:
if 'Result' in song_dict:
song_dict = song_dict['Result']

suggested_by = song_dict['suggested_by']['username'].replace('_', r'\_')
return f"[{song_dict['name']}]({song_dict['url']}) - suggested by {suggested_by}"
suggested_by = format_for_telegram(song_dict['suggested_by']['username'])
return f"[{format_for_telegram(song_dict['name'])}]({song_dict['url']}) - suggested by {suggested_by}"


def send_history_to_all_users(users: list[User], history: list[Song], token: str) -> None:
Expand Down

0 comments on commit 465e4a0

Please sign in to comment.