|
|
|
@ -24,19 +24,26 @@ class Nanobot: |
|
|
|
|
|
|
|
|
|
|
|
async def connect(self): |
|
|
|
async def connect(self): |
|
|
|
uri = f"{NANOBOT_WS_ENDPOINT}/?token={NANOBOT_WS_TOKEN}" |
|
|
|
uri = f"{NANOBOT_WS_ENDPOINT}/?token={NANOBOT_WS_TOKEN}" |
|
|
|
self._ws = await websockets.connect(uri) |
|
|
|
self._ws = await websockets.connect(uri, ping_interval=None) |
|
|
|
ready = json.loads(await self._ws.recv()) |
|
|
|
ready = json.loads(await self._ws.recv()) |
|
|
|
self._chat_id = ready["chat_id"] |
|
|
|
self._chat_id = ready["chat_id"] |
|
|
|
main_logger.info("connected chat_id=%s", self._chat_id) |
|
|
|
main_logger.info("connected chat_id=%s", self._chat_id) |
|
|
|
|
|
|
|
|
|
|
|
async def chat(self, message: str, timeout: float = 10): |
|
|
|
async def chat(self, message: str, timeout: float = 60): |
|
|
|
await self._ws.send(json.dumps({"content": message})) |
|
|
|
try: |
|
|
|
|
|
|
|
await self._ws.send(json.dumps({"content": message})) |
|
|
|
|
|
|
|
except websockets.ConnectionClosed: |
|
|
|
|
|
|
|
main_logger.info("ws: reconnecting...") |
|
|
|
|
|
|
|
await self.connect() |
|
|
|
|
|
|
|
await self._ws.send(json.dumps({"content": message})) |
|
|
|
parts = [] |
|
|
|
parts = [] |
|
|
|
while True: |
|
|
|
while True: |
|
|
|
try: |
|
|
|
try: |
|
|
|
raw = await asyncio.wait_for(self._ws.recv(), timeout=timeout) |
|
|
|
raw = await asyncio.wait_for(self._ws.recv(), timeout=timeout) |
|
|
|
except asyncio.TimeoutError: |
|
|
|
except asyncio.TimeoutError: |
|
|
|
main_logger.info("ws: timeout (no data for %.1fs), generator done", timeout) |
|
|
|
main_logger.info("ws: timeout (no data for %.1fs), reconnecting", timeout) |
|
|
|
|
|
|
|
await self.close() |
|
|
|
|
|
|
|
await self.connect() |
|
|
|
return |
|
|
|
return |
|
|
|
frame = json.loads(raw) |
|
|
|
frame = json.loads(raw) |
|
|
|
event = frame.get("event") |
|
|
|
event = frame.get("event") |
|
|
|
|