This commit is contained in:
Apocalypsor 2021-05-27 23:20:59 +08:00
parent dda8cd7485
commit 7358c15ef0
3 changed files with 49 additions and 45 deletions

View File

@ -13,7 +13,8 @@ from jinja2 import Template
from pymongo import MongoClient from pymongo import MongoClient
from .parser import rssParser, objParser from .parser import rssParser, objParser
from .utils import postData, escapeAll, escapeText from .sender import editToTelegram, sendToTelegram
from .utils import escapeAll, escapeText
class FR2T: class FR2T:
@ -238,46 +239,3 @@ def handleText(name, id, text, tg, db):
) )
print(f"Sent 1 message: {text_hash} in {name}") 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

46
FR2T/sender.py Normal file
View File

@ -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

View File

@ -1,6 +1,6 @@
import requests
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry from requests.packages.urllib3.util.retry import Retry
import requests
def postData(url, data, headers=None, retry=5, timeout=10): def postData(url, data, headers=None, retry=5, timeout=10):