UPdate.
This commit is contained in:
parent
a92fd40b12
commit
4fa67e24cb
@ -1,6 +1,6 @@
|
|||||||
Metadata-Version: 2.4
|
Metadata-Version: 2.4
|
||||||
Name: snekbot
|
Name: snekbot
|
||||||
Version: 1.0.0
|
Version: 1.1.0
|
||||||
Summary: Bot API for Snek chat
|
Summary: Bot API for Snek chat
|
||||||
Author-email: retoor <retoor@molodetz.nl>
|
Author-email: retoor <retoor@molodetz.nl>
|
||||||
Keywords: chat,snek,molodetz,bot
|
Keywords: chat,snek,molodetz,bot
|
||||||
|
@ -89,7 +89,7 @@ class Bot:
|
|||||||
|
|
||||||
async def get_channels(self, refresh=False):
|
async def get_channels(self, refresh=False):
|
||||||
if refresh or not self._channels:
|
if refresh or not self._channels:
|
||||||
self._channels = await (await self.rpc.get_channels())()
|
self._channels = await self.rpc.get_channels()
|
||||||
return self._channels
|
return self._channels
|
||||||
|
|
||||||
async def run_once(self):
|
async def run_once(self):
|
||||||
@ -101,8 +101,8 @@ class Bot:
|
|||||||
rpc = RPC(self.ws)
|
rpc = RPC(self.ws)
|
||||||
self.rpc = rpc
|
self.rpc = rpc
|
||||||
|
|
||||||
await (await rpc.login(self.username, self.password))()
|
await rpc.login(self.username, self.password)
|
||||||
self.user = await (await rpc.get_user(None))()
|
self.user = await rpc.get_user(None)
|
||||||
logger.debug("Logged in as: " + self.user["username"])
|
logger.debug("Logged in as: " + self.user["username"])
|
||||||
|
|
||||||
if is_initial:
|
if is_initial:
|
||||||
@ -118,17 +118,31 @@ class Bot:
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
data = await rpc.receive()
|
data = await rpc.receive()
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
event = "?"
|
||||||
|
try:
|
||||||
|
event = data.event
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
message = data.message.strip()
|
message = data.message.strip()
|
||||||
|
event = "message"
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if event == "?":
|
||||||
continue
|
continue
|
||||||
else:
|
elif event == "message":
|
||||||
break
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
|
await getattr(self, "on_" + data.event)(**data.data)
|
||||||
|
except AttributeError:
|
||||||
|
logger.debug("Not implemented event: " + event)
|
||||||
|
|
||||||
if data.username == self.user["username"]:
|
if data.username == self.user["username"]:
|
||||||
await self.on_own_message(data.channel_uid, message)
|
await self.on_own_message(data.channel_uid, message)
|
||||||
elif message.startswith("ping"):
|
elif message.startswith("ping"):
|
||||||
|
@ -13,7 +13,7 @@ import logging
|
|||||||
import pathlib
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
import uuid
|
import uuid
|
||||||
|
import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
logger = logging.getLogger("snekbot.rpc")
|
logger = logging.getLogger("snekbot.rpc")
|
||||||
@ -53,13 +53,15 @@ class RPC:
|
|||||||
def __init__(self, ws):
|
def __init__(self, ws):
|
||||||
self.ws = ws
|
self.ws = ws
|
||||||
self.current_call_id = None
|
self.current_call_id = None
|
||||||
|
self.queue = asyncio.Queue()
|
||||||
|
|
||||||
async def echo(self, data):
|
async def queue(self, data):
|
||||||
logger.debug("Schedule for retry: " + str(data))
|
logger.debug("Schedule for retry: " + str(data))
|
||||||
await self.ws.send_json({"method": "echo", "args": [data]})
|
await self.queue.put(data)
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
async def method(*args, **kwargs):
|
async def method(*args, **kwargs):
|
||||||
|
no_response = kwargs.pop("_no_response", False)
|
||||||
self.current_call_id = str(uuid.uuid4())
|
self.current_call_id = str(uuid.uuid4())
|
||||||
payload = {
|
payload = {
|
||||||
"method": name,
|
"method": name,
|
||||||
@ -73,12 +75,16 @@ class RPC:
|
|||||||
while True:
|
while True:
|
||||||
response = await self.ws.receive()
|
response = await self.ws.receive()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
if not data.get("callId") == self.current_call_id:
|
if data.get("callId") != self.current_call_id:
|
||||||
await self.echo(data)
|
await self.queue.put(data)
|
||||||
continue
|
continue
|
||||||
|
self.current_call_id = None
|
||||||
return self.Response(data)
|
return self.Response(data)
|
||||||
|
|
||||||
return returner
|
if no_response:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return await returner()
|
||||||
|
|
||||||
return method
|
return method
|
||||||
|
|
||||||
@ -108,6 +114,16 @@ class RPC:
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
async def receive(self):
|
async def receive(self):
|
||||||
|
|
||||||
|
popped = []
|
||||||
|
while not self.queue.empty():
|
||||||
|
msg = await self.queue.get()
|
||||||
|
if self.current_call_id == msg.get("callId"):
|
||||||
|
for m in popped:
|
||||||
|
await self.queue.put(m)
|
||||||
|
self.current_call_id = None
|
||||||
|
return self.Response(msg)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
msg = await self.ws.receive()
|
msg = await self.ws.receive()
|
||||||
@ -121,12 +137,10 @@ class RPC:
|
|||||||
logger.exception("WebSocket error.")
|
logger.exception("WebSocket error.")
|
||||||
break
|
break
|
||||||
elif msg.type == aiohttp.WSMsgType.TEXT:
|
elif msg.type == aiohttp.WSMsgType.TEXT:
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self.current_call_id
|
msg.json().get("callId") != self.current_call_id
|
||||||
and not msg.json().get("callId") != self.current_call_id
|
|
||||||
):
|
):
|
||||||
await self.echo(msg.json())
|
await self.queue.put(msg.json())
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
response = self.Response(msg.json())
|
response = self.Response(msg.json())
|
||||||
|
Loading…
Reference in New Issue
Block a user