Skip to content

Commit

Permalink
update to new deltabot-cli API
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Jan 29, 2024
1 parent b151bfd commit 3c54906
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ cd ..
pip install .
```

### Installing deltachat-rpc-server

This package depends on a standalone Delta Chat RPC server `deltachat-rpc-server` program.
To install it check:
https://github.com/deltachat/deltachat-core-rust/tree/master/deltachat-rpc-server

## Configuration

Configure the bot:
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
version = "0.1.0"
version = "0.2.0"
name = "webxdcbot"
description = "Example Delta Chat bot + webxdc app"
readme = "README.md"
requires-python = ">=3.8"
license = {file = "LICENSE.txt"}
keywords = ["deltachat", "bot"]
keywords = ["deltachat", "bot", "webxdc"]
authors = [
{name = "adbenitez", email = "adbenitez@hispanilandia.net"},
{name = "adbenitez", email = "adbenitez@merlinux.eu"},
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python"
]
dependencies = [
"deltabot-cli>=0.1.0",
"deltabot-cli>=1.0.0",
"fortune-python",
]

Expand Down
26 changes: 17 additions & 9 deletions webxdcbot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3
"""Minimal bot + webxdc example."""

import json
import logging
from pathlib import Path

from deltabot_cli import BotCli, EventType, events
from deltachat_rpc_client.const import ChatType
from deltabot_cli import BotCli, ChatType, EventType, events

from .app import get_response

Expand All @@ -16,22 +16,30 @@
@cli.on(events.RawEvent)
def on_event(event):
"""process webxdc status updates"""
if event.type == EventType.WEBXDC_STATUS_UPDATE:
if event.kind == EventType.WEBXDC_STATUS_UPDATE:
logging.info(event)
msg = event.account.get_message_by_id(event.msg_id)
update = msg.get_webxdc_status_updates(event.status_update_serial - 1)[0]
rpc = event.rpc
accid = event.accid
msgid = event.msg_id
serial = event.status_update_serial - 1
update = json.loads(rpc.get_webxdc_status_updates(accid, msgid, serial))[0]
resp = get_response(update["payload"])
if resp:
msg.send_webxdc_status_update(resp, resp.get("info", ""))
rpc.send_webxdc_status_update(
accid, msgid, json.dumps(resp), resp.get("info", "")
)


@cli.on(events.NewMessage)
def send_app(event):
"""send the webxdc app on every 1:1 (private) message"""
if event.message_snapshot.chat.get_basic_snapshot().chat_type != ChatType.SINGLE:
rpc = event.rpc
accid = event.accid
chatid = event.msg.chat_id
if rpc.get_basic_chat_info(accid, chatid).chat_type != ChatType.SINGLE:
return
logging.info(f"new 1:1 message: {event.message_snapshot.text!r}")
event.message_snapshot.chat.send_message(file=XDC_PATH)
logging.info(f"new 1:1 message: {event.msg.text!r}")
rpc.send_msg(accid, chatid, {"file": XDC_PATH})


def main():
Expand Down

0 comments on commit 3c54906

Please sign in to comment.