1
1
"""Lambda function file for sending scheduled message to a connected Telegram chat via Chat ID."""
2
2
3
3
import os
4
- import re
5
4
import requests
6
5
import telegram
7
- from markdownify import markdownify as md
6
+
7
+ # AWS Lambda loads handler in a special way so we need to import local modules from 'app'
8
+ from app .utils import markdown
8
9
9
10
LEETCODE_DOMAIN = 'https://leetcode.com'
10
11
LEETCODE_ALL_PROBLEM_URL = LEETCODE_DOMAIN + '/problemset/all/'
@@ -60,31 +61,13 @@ def get_question_of_today():
60
61
try :
61
62
return response .json ()
62
63
except ValueError :
63
- print (" Failed to decode JSON, API response:" )
64
+ print (' Failed to decode JSON, API response:' )
64
65
print (response .text )
65
66
raise
66
67
except BaseException as error :
67
- print (f" Unexpected { error = } , { type (error )= } " )
68
+ print (f' Unexpected { error = } , { type (error )= } ' )
68
69
raise
69
70
70
- def generate_telegram_markdown (content ):
71
- """Convert HTML to Telegram Markdown syntax"""
72
-
73
- formatted_content = content
74
- # Special handling for superscript and subscript since standard markdown does not support them
75
- formatted_content = re .sub ('<sup>' , '<sup>^' , formatted_content )
76
- formatted_content = re .sub ('<sub>' , '<sub>_' , formatted_content )
77
- # Convert allowed tags to markdown
78
- # Note that supported markdown syntax is different in Telegram
79
- # https://core.telegram.org/bots/api#formatting-options
80
- formatted_content = md (formatted_content , convert = ['p' , 'img' , 'code' , 'pre' ])
81
- # Replace multiple empty lines
82
- formatted_content = re .sub ('(\s+)?\n {2,}' , '\n \n ' , formatted_content )
83
- # Special handling for images
84
- formatted_content = re .sub ('\!\[(.+)?\]\((.+)\)' , r'image: \2' , formatted_content )
85
-
86
- return formatted_content .strip ()
87
-
88
71
def send_message (event , context ):
89
72
"""Lambda function handler to send text message."""
90
73
@@ -100,12 +83,11 @@ def send_message(event, context):
100
83
question_difficulty = question_info ['question' ]['difficulty' ]
101
84
question_content = question_info ['question' ]['content' ]
102
85
103
-
104
- message = f"*{ question_date } *\n " \
105
- f"*{ question_id } . { question_title } *\n \n " \
106
- f"*Topic:* { question_topic } \n " \
107
- f"*Difficulty:* { question_difficulty } \n \n " \
108
- f"*Problem:*\n { generate_telegram_markdown (question_content )} "
86
+ message = f'*{ question_date } *\n ' \
87
+ f'*{ question_id } . { question_title } *\n \n ' \
88
+ f'*Topic:* { question_topic } \n ' \
89
+ f'*Difficulty:* { question_difficulty } \n \n ' \
90
+ f'*Problem:*\n { markdown .generate (question_content )} '
109
91
110
92
bot = telegram .Bot (token = TOKEN )
111
93
bot .send_message (
@@ -114,7 +96,8 @@ def send_message(event, context):
114
96
reply_markup = telegram .InlineKeyboardMarkup ([
115
97
[telegram .InlineKeyboardButton (text = "View on Leetcode" , url = question_url )]
116
98
]),
117
- parse_mode = 'Markdown'
99
+ parse_mode = 'Markdown' ,
100
+ disable_web_page_preview = True
118
101
)
119
102
else :
120
103
raise Exception ('Invalid API response. No "data" node found in API response.' )
0 commit comments