diff --git a/FR2T/fr2t.py b/FR2T/fr2t.py index f684eb1..d9cdb7e 100644 --- a/FR2T/fr2t.py +++ b/FR2T/fr2t.py @@ -13,7 +13,8 @@ from jinja2 import Template from pymongo import MongoClient from .parser import rssParser, objParser -from .utils import postData, escapeAll, escapeText +from .sender import editToTelegram, sendToTelegram +from .utils import escapeAll, escapeText class FR2T: @@ -238,46 +239,3 @@ def handleText(name, id, text, tg, db): ) print(f"Sent 1 message: {text_hash} in {name}") - - -def sendToTelegram(tg, text): - url = "https://api.telegram.org/bot" + tg["token"] + "/sendMessage" - payload = { - "chat_id": tg["chat_id"], - "text": text, - "parse_mode": tg["parse_mode"], - "disable_web_page_preview": tg["disable_web_page_preview"], - "disable_notification": tg["disable_notification"], - } - - r = postData(url, data=payload) - - if r.json()["ok"]: - return r.json()["result"]["message_id"] - elif r.json()["error_code"] == 429: - print("\nToo frequently! Sleep 30s.\n") - time.sleep(30) - sendToTelegram(tg, text) - else: - print("\nError: failed to sending message:") - print(text) - print(r.json()["description"] + "\n") - return False - - -def editToTelegram(tg, message_id, text): - url = "https://api.telegram.org/bot" + tg["token"] + "/editMessageText" - payload = { - "chat_id": tg["chat_id"], - "message_id": message_id, - "text": text, - "parse_mode": tg["parse_mode"], - "disable_web_page_preview": tg["disable_web_page_preview"], - } - - r = postData(url, data=payload) - - if r.json()["ok"] or "exactly the same" in r.json()["description"]: - return True - else: - return False diff --git a/FR2T/sender.py b/FR2T/sender.py new file mode 100644 index 0000000..7f768d7 --- /dev/null +++ b/FR2T/sender.py @@ -0,0 +1,46 @@ +import time + +from utils import postData + + +def sendToTelegram(tg, text): + url = "https://api.telegram.org/bot" + tg["token"] + "/sendMessage" + payload = { + "chat_id": tg["chat_id"], + "text": text, + "parse_mode": tg["parse_mode"], + "disable_web_page_preview": tg["disable_web_page_preview"], + "disable_notification": tg["disable_notification"], + } + + r = postData(url, data=payload) + + if r.json()["ok"]: + return r.json()["result"]["message_id"] + elif r.json()["error_code"] == 429: + print("\nToo frequently! Sleep 30s.\n") + time.sleep(30) + sendToTelegram(tg, text) + else: + print("\nError: failed to sending message:") + print(text) + print(r.json()["description"] + "\n") + return False + + +def editToTelegram(tg, message_id, text): + url = "https://api.telegram.org/bot" + tg["token"] + "/editMessageText" + payload = { + "chat_id": tg["chat_id"], + "message_id": message_id, + "text": text, + "parse_mode": tg["parse_mode"], + "disable_web_page_preview": tg["disable_web_page_preview"], + } + + r = postData(url, data=payload) + + if r.json()["ok"] or "exactly the same" in r.json()["description"]: + return True + else: + return False diff --git a/FR2T/utils.py b/FR2T/utils.py index 1cce2d2..8f412f6 100644 --- a/FR2T/utils.py +++ b/FR2T/utils.py @@ -1,6 +1,6 @@ +import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry -import requests def postData(url, data, headers=None, retry=5, timeout=10):