add telegram
This commit is contained in:
@@ -11,6 +11,7 @@ SERVER_TOKEN = config["server"]["bearer_token"]
|
|||||||
|
|
||||||
APNS2_CONFIG = config["apns2"]
|
APNS2_CONFIG = config["apns2"]
|
||||||
FIREBASE_CONFIG = config["firebase"]
|
FIREBASE_CONFIG = config["firebase"]
|
||||||
|
TELEGRAM_CONFIG = config["telegram"]
|
||||||
|
|
||||||
|
|
||||||
PREFIX_MAP = {
|
PREFIX_MAP = {
|
||||||
@@ -86,6 +87,32 @@ def send_to_firebase(notification_type, title, message):
|
|||||||
"body": response.text
|
"body": response.text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def send_to_telegram(notification_type, title, message):
|
||||||
|
prefix = PREFIX_MAP.get(notification_type, "")
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"message": f"{prefix}{title}\n{message}"
|
||||||
|
}
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"X-API-Key": TELEGRAM_CONFIG["api_key"],
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.post(
|
||||||
|
TELEGRAM_CONFIG["url"],
|
||||||
|
json=payload,
|
||||||
|
headers=headers,
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status_code": response.status_code,
|
||||||
|
"body": response.text
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/notify", methods=["POST"])
|
@app.route("/notify", methods=["POST"])
|
||||||
def notify():
|
def notify():
|
||||||
if not check_auth(request):
|
if not check_auth(request):
|
||||||
@@ -150,6 +177,20 @@ def notify():
|
|||||||
"error": str(e)
|
"error": str(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
telegram_result = send_to_telegram(
|
||||||
|
notification_type,
|
||||||
|
title,
|
||||||
|
message
|
||||||
|
)
|
||||||
|
|
||||||
|
results["telegram"] = telegram_result
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
results["telegram"] = {
|
||||||
|
"error": str(e)
|
||||||
|
}
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"success": True,
|
"success": True,
|
||||||
"results": results
|
"results": results
|
||||||
|
|||||||
@@ -8,3 +8,7 @@ apns2:
|
|||||||
firebase:
|
firebase:
|
||||||
url: "https://firebase.example.com/push"
|
url: "https://firebase.example.com/push"
|
||||||
bearer_token: "firebase-token"
|
bearer_token: "firebase-token"
|
||||||
|
|
||||||
|
telegram:
|
||||||
|
url: "https://telegram.example.com/push"
|
||||||
|
api_key: "telegram-api-key"
|
||||||
Reference in New Issue
Block a user