-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
60 lines (46 loc) · 1.8 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import re
from datetime import datetime
from dateutil.parser import parse
import pandas as pd
import requests
from dotenv import load_dotenv
load_dotenv()
def send_telegram_message(message):
token = os.getenv("TELEGRAM_BOT_TOKEN")
chat_id = os.getenv("TELEGRAM_CHAT_ID")
url = (
f"https://api.telegram.org/bot{token}/"
f"sendMessage?chat_id={chat_id}&"
f"parse_mode=MarkdownV2&text={message}"
)
response = requests.get(url).json()
if response.get("ok") is False:
raise Exception(f"Failed to send message. Error: {response}")
else:
print("Message sent.")
def escape_markdown(text):
text = text.replace("&", "and")
escape_chars = r'_*[]()~`>#+-=|{}.!'
return re.sub(f'([{re.escape(escape_chars)}])', r'\\\1', text)
def send_new_courses():
available_statuses = {"waiting list": "📖", "available": "💥"}
message = "🆕 [{course_title}]({course_url}) from {start} to {end} {status}"
df = pd.read_csv("courses.csv")
for _, row in df[df["updated_at"].isna()].iterrows():
start_date = parse(row['start'], dayfirst=True)
in_the_future = start_date > datetime.now()
has_spots = row['availability'] in available_statuses.keys()
if in_the_future and has_spots:
formatted_message = message.format(
course_title=escape_markdown(row['title']),
course_url=row['course_url'],
start=escape_markdown(row['start']),
end=escape_markdown(row['end']),
status=available_statuses.get(row['availability'])
)
print(formatted_message)
send_telegram_message(formatted_message)
df['updated_at'] = str(datetime.now())
df.to_csv('courses.csv', index=False)
send_new_courses()