-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.py
61 lines (48 loc) · 2.03 KB
/
notify.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
61
"""Send messages via Telegram bot."""
from os import environ
from __init__ import session, escape_markdown
api_send = "https://api.telegram.org/bot{}/sendMessage"
try:
telegram_bot_token = environ["TELEGRAM_BOT_TOKEN"]
telegram_chat_id = environ["TELEGRAM_CHAT_ID"]
except KeyError:
raise LookupError("TELEGRAM_BOT_TOKEN and/or TELEGRAM_CHAT_ID is not set in the environment variable")
class TelegramError(Exception):
"""TelegramError is raised when the response from Telegram API is not 200 OK."""
pass
def send_message(message):
"""Send a message via Telegram. Message is written in markdown format."""
params = {
"chat_id": telegram_chat_id,
"parse_mode": "MarkdownV2", # do not use markdown (version 1)!
"text": message
}
response = session.get(api_send.format(telegram_bot_token), params=params).json()
if not response['ok']:
raise TelegramError(f"fail to send message via Telegram ({response['error_code']}): {response['description']}")
return response
if __name__ == "__main__":
from json import loads
from time import sleep
from __init__ import config_filename
from config_parser import ConfigParser
from translate import get_pending_recommendations
data = get_pending_recommendations()
sent_items = []
# Record the items that have been sent to prevent repeating messages
parser = ConfigParser()
parser.read(config_filename)
try:
sent_items = loads(parser["translate"]["sent"])
except KeyError:
sent_items = []
ids = []
for item in data:
if item["id"] not in sent_items:
send_message(f"{escape_markdown(item['recommend_name'])}推荐了一篇文章:"
f"[{escape_markdown(item['title_src'])}]({item['link_url']})({item['word_count']} 单词),"
f"快去[审核](https://juejin.cn/translate/manage)吧!")
ids.append(item["id"])
sleep(1)
parser["translate"] = {"sent": str(ids)}
parser.write(config_filename)