Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,30 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
-
import
|
4 |
-
import os
|
5 |
-
|
6 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
-
class
|
11 |
message: str
|
12 |
-
|
|
|
13 |
|
14 |
-
@app.post("/
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
messages.append({"role": "user", "content": data.message})
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
return {
|
27 |
-
"reply": reply,
|
28 |
-
"history": messages + [{"role": "assistant", "content": reply}]
|
29 |
-
}
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
+
import re
|
|
|
|
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
+
class CommandInput(BaseModel):
|
8 |
message: str
|
9 |
+
user_id: int # the person who sent the command
|
10 |
+
chat_id: int # the chat where the command was used
|
11 |
|
12 |
+
@app.post("/command")
|
13 |
+
def handle_command(data: CommandInput):
|
14 |
+
message = data.message.strip().lower()
|
15 |
+
response = "Unknown command."
|
|
|
16 |
|
17 |
+
if message.startswith("/ban"):
|
18 |
+
match = re.search(r"@?(\w+)", message)
|
19 |
+
if match:
|
20 |
+
target = match.group(1)
|
21 |
+
response = f"User @{target} has been banned in chat {data.chat_id}."
|
22 |
+
elif message.startswith("/mute"):
|
23 |
+
match = re.search(r"@?(\w+)", message)
|
24 |
+
if match:
|
25 |
+
target = match.group(1)
|
26 |
+
response = f"User @{target} has been muted in chat {data.chat_id}."
|
27 |
+
elif message.startswith("/help"):
|
28 |
+
response = "Available commands: /ban @user, /mute @user, /help"
|
29 |
|
30 |
+
return {"response": response}
|
|
|
|
|
|
|
|