Compare commits

...

3 Commits

Author SHA1 Message Date
adbenitez
2a4d352dff add example 2023-10-27 01:33:21 +02:00
adbenitez
8329108c47 re-use get_async_rpc() 2023-10-26 23:34:32 +02:00
adbenitez
6d2bae7b1d allow to use the JSON RPC client from async code 2023-10-26 23:30:13 +02:00
3 changed files with 88 additions and 0 deletions

View File

@@ -48,3 +48,7 @@ $ python
'awesome'
>>> rpc.close()
```
## Usage from async code
See the [echobot_async.py](./examples/echobot_async.py) example.

View File

@@ -0,0 +1,56 @@
#!/usr/bin/env python3
"""
Example asynchronous echo bot
"""
import asyncio
import logging
import sys
from deltachat_rpc_client import EventType, Rpc, SpecialContactId
async def main():
async with Rpc() as rpc:
system_info = await rpc.get_system_info()
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
account_ids = await rpc.get_all_account_ids()
accid = account_ids[0] if account_ids else await rpc.add_account()
await rpc.set_config(accid, "bot", "1")
if not await rpc.is_configured(accid):
logging.info("Account is not configured, configuring")
await rpc.set_config(accid, "addr", sys.argv[1])
await rpc.set_config(accid, "mail_pw", sys.argv[2])
await rpc.configure(accid)
logging.info("Configured")
else:
logging.info("Account is already configured")
await rpc.start_io(accid)
async def process_messages():
for msgid in await rpc.get_next_msgs(accid):
msg = await rpc.get_message(accid, msgid)
if msg["from_id"] != SpecialContactId.SELF and not msg["is_bot"] and not msg["is_info"]:
await rpc.misc_send_text_message(accid, msg["chat_id"], msg["text"])
await rpc.markseen_msgs(accid, [msgid])
# Process old messages.
await process_messages()
while True:
event = await rpc.wait_for_event(accid)
if event["kind"] == EventType.INFO:
logging.info("%s", event["msg"])
elif event["kind"] == EventType.WARNING:
logging.warning("%s", event["msg"])
elif event["kind"] == EventType.ERROR:
logging.error("%s", event["msg"])
elif event["kind"] == EventType.INCOMING_MSG:
logging.info("Got an incoming message (id=%s)", event["msg_id"])
await process_messages()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(main())

View File

@@ -1,3 +1,4 @@
import asyncio
import json
import logging
import os
@@ -35,6 +36,10 @@ class Rpc:
self.writer_thread: Thread
self.events_thread: Thread
def get_async_rpc(self) -> "AsyncRpc":
"""Get asynchronous wrapper to use the RPC methods from async code."""
return AsyncRpc(self)
def start(self) -> None:
if sys.version_info >= (3, 11):
self.process = subprocess.Popen(
@@ -84,6 +89,13 @@ class Rpc:
def __exit__(self, _exc_type, _exc, _tb):
self.close()
async def __aenter__(self):
self.__enter__()
return self.get_async_rpc()
async def __aexit__(self, _exc_type, _exc, _tb):
self.__exit__(_exc_type, _exc, _tb)
def reader_loop(self) -> None:
try:
while True:
@@ -165,3 +177,19 @@ class Rpc:
return None
return method
class AsyncRpc:
def __init__(self, sync_rpc: Rpc) -> None:
self._sync_rpc = sync_rpc
def __getattr__(self, attr: str) -> Any:
sync_method = getattr(self._sync_rpc, attr)
if sync_method:
async def method(*args) -> Any:
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, sync_method, *args)
return method
return None