From 2a4d352dff3d9628d277a48bd2bab4966deef923 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Fri, 27 Oct 2023 01:33:21 +0200 Subject: [PATCH] add example --- .../examples/echobot_async.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 deltachat-rpc-client/examples/echobot_async.py diff --git a/deltachat-rpc-client/examples/echobot_async.py b/deltachat-rpc-client/examples/echobot_async.py new file mode 100644 index 000000000..36ba32f64 --- /dev/null +++ b/deltachat-rpc-client/examples/echobot_async.py @@ -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())