-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (67 loc) · 2.45 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import requests
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class InstanceData(BaseModel):
idInstance: str
apiTokenInstance: str
class MessageData(BaseModel):
idInstance: str
apiTokenInstance: str
chatId: str
message: str
class FileData(BaseModel):
idInstance: str
apiTokenInstance: str
chatId: str
urlFile: str
fileName: str
caption: str
@app.post("/getSettings")
async def get_settings(data: InstanceData):
url = f"https://7103.api.greenapi.com/waInstance{data.idInstance}/getSettings/{data.apiTokenInstance}"
response = requests.get(url)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.json())
return response.json()
@app.post("/getStateInstance")
async def get_state_instance(data: InstanceData):
url = f"https://7103.api.greenapi.com/waInstance{data.idInstance}/getStateInstance/{data.apiTokenInstance}"
response = requests.get(url)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.json())
return response.json()
@app.post("/sendMessage")
async def send_message(data: MessageData):
url = f"https://7103.api.greenapi.com/waInstance{data.idInstance}/sendMessage/{data.apiTokenInstance}"
payload = {
"chatId": f"{data.chatId}@c.us",
"message": data.message
}
response = requests.post(url, json=payload)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.json())
return response.json()
@app.post("/sendFileByUrl")
async def send_file_by_url(data: FileData):
url = f"https://7103.api.greenapi.com/waInstance{data.idInstance}/sendFileByUrl/{data.apiTokenInstance}"
payload = {
"chatId": f"{data.chatId}@c.us",
"urlFile": data.urlFile,
"fileName": data.fileName,
"caption": data.caption
}
response = requests.post(url, json=payload)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.json())
return response.json()