diff --git a/src/pacroller/config.py b/src/pacroller/config.py
index 30f19ff..72e76e8 100644
--- a/src/pacroller/config.py
+++ b/src/pacroller/config.py
@@ -13,6 +13,7 @@ F_KNOWN_OUTPUT_OVERRIDE = 'known_output_override.py'
LIB_DIR = Path('/var/lib/pacroller')
DB_FILE = 'db'
NEWS_FILE = 'news'
+DEF_HTTP_HDRS = {'User-Agent': 'Mozilla/5.0 (compatible; Pacroller/0.1; +https://github.com/isjerryxiao/pacroller)'}
LOG_DIR = Path('/var/log/pacroller')
PACMAN_CONFIG = '/etc/pacman.conf'
PACMAN_LOG = '/var/log/pacman.log'
diff --git a/src/pacroller/mailer.py b/src/pacroller/mailer.py
index 9504539..4663826 100644
--- a/src/pacroller/mailer.py
+++ b/src/pacroller/mailer.py
@@ -5,7 +5,7 @@ from typing import List
import logging
import urllib.request, urllib.parse
from platform import node
-from pacroller.config import NETWORK_RETRY, SMTP_ENABLED, SMTP_SSL, SMTP_HOST, SMTP_PORT, SMTP_FROM, SMTP_TO, SMTP_AUTH, TG_ENABLED, TG_BOT_TOKEN, TG_API_HOST, TG_RECIPIENT
+from pacroller.config import NETWORK_RETRY, SMTP_ENABLED, SMTP_SSL, SMTP_HOST, SMTP_PORT, SMTP_FROM, SMTP_TO, SMTP_AUTH, TG_ENABLED, TG_BOT_TOKEN, TG_API_HOST, TG_RECIPIENT, DEF_HTTP_HDRS
logger = logging.getLogger()
hostname = node() or "unknown-host"
@@ -42,14 +42,14 @@ def send_tg_message(text: str, subject: str, mailto: List[str]) -> bool:
try:
for recipient in mailto:
url = f'https://{TG_API_HOST}/bot{TG_BOT_TOKEN}/sendMessage'
- headers = {'User-Agent': 'Mozilla/5.0 (compatible; Pacroller/0.1; +https://github.com/isjerryxiao/pacroller)'}
- data = urllib.parse.urlencode({"chat_id": recipient, "text": f"{subject}\n\n{text}
", "parse_mode": "HTML"})
- req = urllib.request.Request(url, data=data.encode(), headers=headers)
+ headers = {'Content-Type': 'application/json'}
+ data = json.dumps({"chat_id": recipient, "text": f"{subject}\n\n{text[:4000]}
", "parse_mode": "HTML"})
+ req = urllib.request.Request(url, data=data.encode('utf-8'), headers={**DEF_HTTP_HDRS, **headers})
resp = urllib.request.urlopen(req).read().decode('utf-8')
content = json.loads(resp)
- if not content["ok"]:
+ if not content.get("ok"):
all_succeeded = False
- logger.error(f"unable to send telegram message to {recipient}: %s" % content['description'])
+ logger.error(f"unable to send telegram message to {recipient}: {content.get('description')}")
except Exception:
logger.exception("error while send_tg_message")
else:
@@ -79,4 +79,4 @@ class MailSender:
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s')
- MailSender().send_text_plain("This is a test mail\nIf you see this mail, your notification config is working.")
+ print(MailSender().send_text_plain("This is a test mail\nIf you see this mail, your notification config is working."))
diff --git a/src/pacroller/news.py b/src/pacroller/news.py
index b8d9b72..3d14613 100644
--- a/src/pacroller/news.py
+++ b/src/pacroller/news.py
@@ -1,11 +1,11 @@
import urllib.request
from xml.etree import ElementTree as etree
from typing import List
+from pacroller.config import DEF_HTTP_HDRS
def get_news(old_news: str) -> List[str]:
ARCH_RSS_URL = 'https://archlinux.org/feeds/news/'
- headers = {'User-Agent': 'Mozilla/5.0 (compatible; Pacroller/0.1; +https://github.com/isjerryxiao/pacroller)'}
- req = urllib.request.Request(ARCH_RSS_URL, data=None, headers=headers)
+ req = urllib.request.Request(ARCH_RSS_URL, data=None, headers=DEF_HTTP_HDRS)
rss_text = urllib.request.urlopen(req).read().decode('utf-8')
xml_root = etree.fromstring(rss_text)