feat(server): Write JSON

Trying to work with JSON, but thats kinda clunky for that purpose, I think SQLite would be easier
This commit is contained in:
nikurasu 2024-02-10 00:56:18 +01:00
parent f2263d3c18
commit 6ff7c21763
Signed by: Nikurasu
GPG key ID: 9E7F14C03EF1F271
4 changed files with 68 additions and 10 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
__pycache__
.venv

29
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python Debugger: main.py",
"type": "debugpy",
"request": "launch",
"program": "src/main.py",
"console": "integratedTerminal"
},
{
"name": "uvicorn: Dev",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": ["src.main:app", "--reload", "--port", "8080"]
}
]
}

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
fastapi==0.109.2
pydantic==2.6.1
uvicorn==0.27.0.post1

View file

@ -1,12 +1,36 @@
import argparse
import requests
import uvicorn
import json
from pydantic import BaseModel
from fastapi import FastAPI, status
from fastapi.responses import JSONResponse
from os.path import expanduser
from pathlib import Path
app = FastAPI()
Path(f"{expanduser("~")}/.local/share/telegram-autoclear").mkdir(parents=True, exist_ok=True)
class SendMessageBody(BaseModel):
name: str
@app.get("/")
def read_root():
return {"Made by": "Nikurasu", "Version": "0.0.0"}
@app.post("/send_message")
def send_message(body: SendMessageBody):
with open(f"{expanduser("~")}/.local/share/telegram-autoclear/messages.json", "r") as messages_file:
try:
messages_data = json.load(messages_file)
except Exception as e:
return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"error": f"{e}"})
if "messages" not in messages_data:
write_data = {"messages": [{"name": body.name}]}
else:
write_data = {"messages": messages_data["messages"]}
write_data["messages"].append({"name": body.name})
with open(f"{expanduser("~")}/.local/share/telegram-autoclear/messages.json", "w") as messages_file:
json.dump(write_data, messages_file)
return {"name": body.name}
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="telegram-autoclear", description="simple python script to delete old telegram bot messages", epilog="made by NikUwU")
parser.add_argument("-t", "--token")
args = parser.parse_args()
url = f"https://api.telegram.org/bot{args.token}/getUpdates"
response = requests.get(url)
responsej = response.json()
print(responsej['result'])
uvicorn.run(app, host="0.0.0.0", port=8080)