main
py 103 lines 3.09 KB
Raw
1 """
2 WhatsApp bridge HTTP client.
3
4 No agent/tool dependencies.
5 """
6
7 import aiohttp
8
9
10 async def get_messages(base_url: str) -> list[dict]:
11 async with aiohttp.ClientSession() as session:
12 async with session.get(
13 f"{base_url}/messages", timeout=aiohttp.ClientTimeout(total=10),
14 ) as resp:
15 if resp.status == 200:
16 return await resp.json()
17 return []
18
19
20 async def send_message(
21 base_url: str, chat_id: str, message: str, reply_to: str = "",
22 ) -> dict:
23 payload: dict = {"chatId": chat_id, "message": message}
24 if reply_to:
25 payload["replyTo"] = reply_to
26 async with aiohttp.ClientSession() as session:
27 async with session.post(
28 f"{base_url}/send",
29 json=payload,
30 timeout=aiohttp.ClientTimeout(total=30),
31 ) as resp:
32 return await resp.json()
33
34
35 async def send_media(
36 base_url: str,
37 chat_id: str,
38 file_path: str,
39 caption: str = "",
40 media_type: str = "",
41 file_name: str = "",
42 ) -> dict:
43 payload: dict = {"chatId": chat_id, "filePath": file_path}
44 if caption:
45 payload["caption"] = caption
46 if media_type:
47 payload["mediaType"] = media_type
48 if file_name:
49 payload["fileName"] = file_name
50 async with aiohttp.ClientSession() as session:
51 async with session.post(
52 f"{base_url}/send-media",
53 json=payload,
54 timeout=aiohttp.ClientTimeout(total=30),
55 ) as resp:
56 return await resp.json()
57
58
59 async def send_typing(base_url: str, chat_id: str, paused: bool = False) -> None:
60 try:
61 payload: dict = {"chatId": chat_id}
62 if paused:
63 payload["status"] = "paused"
64 async with aiohttp.ClientSession() as session:
65 async with session.post(
66 f"{base_url}/typing",
67 json=payload,
68 timeout=aiohttp.ClientTimeout(total=5),
69 ) as resp:
70 await resp.json()
71 except Exception:
72 pass
73
74
75 async def get_health(base_url: str) -> dict:
76 async with aiohttp.ClientSession() as session:
77 async with session.get(
78 f"{base_url}/health", timeout=aiohttp.ClientTimeout(total=5),
79 ) as resp:
80 if resp.status == 200:
81 return await resp.json()
82 return {"status": "error", "queueLength": 0, "uptime": 0}
83
84
85 async def get_qr(base_url: str) -> dict:
86 async with aiohttp.ClientSession() as session:
87 async with session.get(
88 f"{base_url}/qr", timeout=aiohttp.ClientTimeout(total=5),
89 ) as resp:
90 if resp.status == 200:
91 return await resp.json()
92 return {"status": "error", "qr": None}
93
94
95 async def get_chat_info(base_url: str, chat_id: str) -> dict:
96 async with aiohttp.ClientSession() as session:
97 async with session.get(
98 f"{base_url}/chat/{chat_id}",
99 timeout=aiohttp.ClientTimeout(total=10),
100 ) as resp:
101 if resp.status == 200:
102 return await resp.json()
103 return {"name": "", "isGroup": False, "participants": []}