Skip to content
Snippets Groups Projects

Add file and console output of incoming data

Merged Demannu requested to merge log-incoming-data into main
Files
7
+ 13
1
""" Main package for alert_relay """
import json
import os
import requests
import uvicorn
from fastapi import FastAPI
@@ -16,13 +17,23 @@ class AlertBody(BaseModel):
app = FastAPI()
with open("config.json", "r", encoding="utf_8") as file:
with open("/app/data/config.json", "r", encoding="utf_8") as file:
config = json.load(file)
if not os.path.exists("logs/"):
os.mkdir("logs")
@app.post("/alerts")
async def alerts(alert: AlertBody):
"""Route for handling the relay"""
# Open the logfile in "a"ppend mode, with the standard encoding
with open("./logs/incoming-datto.log", "a", encoding="utf8") as log_file:
log_file.write(str(alert))
# Print the incoming data from data
print(alert)
request = requests.post(
f"https://api.telegram.org/{config['auth']}/sendMessage",
json={
@@ -30,6 +41,7 @@ async def alerts(alert: AlertBody):
"chat_id": alert.chat_id,
"text": alert.message,
},
timeout=60,
)
return request.json(), request.status_code
Loading