mirror of
https://github.com/chatmail/core.git
synced 2026-04-02 05:22:14 +03:00
Compare commits
3 Commits
8518d3fc3a
...
rpc-client
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a4d352dff | ||
|
|
8329108c47 | ||
|
|
6d2bae7b1d |
@@ -48,3 +48,7 @@ $ python
|
||||
'awesome'
|
||||
>>> rpc.close()
|
||||
```
|
||||
|
||||
## Usage from async code
|
||||
|
||||
See the [echobot_async.py](./examples/echobot_async.py) example.
|
||||
|
||||
56
deltachat-rpc-client/examples/echobot_async.py
Normal file
56
deltachat-rpc-client/examples/echobot_async.py
Normal 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())
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user