2021-05-27 15:20:59 +00:00
|
|
|
import time
|
|
|
|
|
2021-05-27 16:49:11 +00:00
|
|
|
from .utils import postData
|
2021-05-27 15:20:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2021-05-27 17:28:17 +00:00
|
|
|
print("\nError: failed to send the message:")
|
2021-05-27 15:20:59 +00:00
|
|
|
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)
|
|
|
|
|
2021-05-27 17:28:17 +00:00
|
|
|
if r.json()["ok"] or "exactly the same" in r.json()["description"] or "message to edit not found" in r.json()[
|
|
|
|
"description"]:
|
2021-05-27 15:20:59 +00:00
|
|
|
return True
|
|
|
|
else:
|
2021-05-27 17:28:17 +00:00
|
|
|
print("\nError: failed to edit the message:")
|
|
|
|
print(text)
|
|
|
|
print(r.json()["description"] + "\n")
|
2021-05-27 15:20:59 +00:00
|
|
|
return False
|