-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendMessage.py
More file actions
55 lines (42 loc) · 1.42 KB
/
Copy pathSendMessage.py
File metadata and controls
55 lines (42 loc) · 1.42 KB
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
import requests
import os
from Image2Url import image_to_data_url
from dotenv import load_dotenv
load_dotenv()
# Move configurations to the top so functions can see them
API_URL = os.getenv("API_URL")
RECIEVER_ID = os.getenv("RECIEVER_ID")
HEADERS = {"Content-Type": "application/json"}
def getToken():
payload = {
"email": "cctv@gmail.com",
"password": "123456"
}
# Fixed f-string placement
response = requests.post(f"{API_URL}/auth/login", json=payload, headers=HEADERS)
try:
response_json = response.json()
# Use .get() to avoid KeyError if 'token' is missing
return response_json.get("token")
except requests.exceptions.JSONDecodeError:
print("❌ Error: Response is not valid JSON!")
return None
# Get the token first
TOKEN = getToken()
# Now set up cookies
COOKIES = {"jwt": TOKEN}
def send_message(text, image_path=None):
# Fixed f-string placement
url = f"{API_URL}/messages/send/{RECIEVER_ID}"
image_url = None
if image_path:
image_url = image_to_data_url(image_path)
payload = {
"text": text,
"image": image_url
}
response = requests.post(url, json=payload, headers=HEADERS, cookies=COOKIES)
print(f"Status: {response.status_code}")
return response.json()
# Example call
# send_message("Hello, this is an accident", "./avatar.png")