Skip to content

Commit 83e6c56

Browse files
authored
update python and add python cffi example (#63)
* rename python folder to python_cffi * make sure it works with newest core version * add python jsonrpc
1 parent bc92cb5 commit 83e6c56

File tree

10 files changed

+151
-8
lines changed

10 files changed

+151
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ An echo bot in multiple languages to get you started.
1010
| [Go](./go) | `1.110.0` |
1111
| [node.js over cffi](./nodejs_cffi) | `1.132.1` |
1212
| [node.js over jsonrpc](./nodejs_napi_jsonrpc) (unmaintained) | `1.101.0` |
13-
| [Python](./python) | `1.94.0` |
13+
| [Python over cffi](./python_cffi) | `1.132.1` |
14+
| [Python over jsonrpc](./python_jsonrpc) | `1.132.1` |
1415
| [Rust](./rust) | `1.132.1` |
1516

1617
### With abstraction layer / bot framework:
File renamed without changes.

python/README.md python_cffi/README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# Python
1+
# Python (CFFI)
22

3-
> There are 2 alternative options available:
3+
> There are 3 alternative options available:
44
>
5+
> using the newer [jsonrpc based bindings](../python_jsonrpc)
6+
>
7+
> frameworks:
58
> - [using deltabot](../python_deltabot_plugin) (bot framework, includes features as chat command parsing)
69
> - [using simplebot](../python_simplebot_plugin) (simplebot is a maintained fork of deltabot with many plugins availible)
710
@@ -15,8 +18,11 @@ source .venv/bin/activate
1518
pip3 install -U pip wheel
1619

1720
# install deltachat
18-
pip3 install deltachat # if it doesn't work, see https://github.com/deltachat/deltachat-core-rust/tree/master/python for instructions on how to install it from source
21+
pip3 install deltachat
1922
```
23+
If it doesn't work (for example because you are not on linux),
24+
see https://py.delta.chat/cffi/install.html#installing-bindings-from-source for instructions on how to install it from source.
25+
2026

2127
## Usage
2228

@@ -27,6 +33,6 @@ python echo_bot.py /tmp/db --email ADDRESS --password PASSWORD
2733
### References
2834

2935
- all available hooks can be found in https://github.com/deltachat/deltachat-core-rust/blob/master/python/src/deltachat/hookspec.py
30-
- Python bindings documentation py.delta.chat
31-
- https://py.delta.chat/examples.html
36+
- Python bindings documentation https://py.delta.chat
37+
- https://py.delta.chat/cffi/examples.html
3238
- source code of python bindings https://github.com/deltachat/deltachat-core-rust/tree/master/python
File renamed without changes.

python_deltabot_plugin/README.MD

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ a pluggable deltachat bot.
66
> There are 2 alternative options available:
77
>
88
> - [using simplebot](../python_simplebot_plugin) (simplebot is a maintained fork of deltabot with many plugins availible)
9-
> - [using python directly](../python) (more controll, but also more code to take care of)
9+
> - [using python directly](../python_cffi) (more controll, but also more code to take care of)
1010
1111
## Usage
1212

python_jsonrpc/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.venv
2+
accounts

python_jsonrpc/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Python (CFFI)
2+
3+
> There are 3 alternative options available:
4+
>
5+
> using the older legacy [cffi based bindings](../python_cffi)
6+
>
7+
> frameworks:
8+
> - [using deltabot](../python_deltabot_plugin) (bot framework, includes features as chat command parsing)
9+
> - [using simplebot](../python_simplebot_plugin) (simplebot is a maintained fork of deltabot with many plugins availible)
10+
11+
## Installation
12+
13+
```sh
14+
# Optional create virtualenv
15+
pip3 install virtualenv
16+
virtualenv .venv
17+
source .venv/bin/activate
18+
19+
# install rpc server
20+
pip install deltachat-rpc-server
21+
# install client
22+
pip install deltachat-rpc-client
23+
```
24+
25+
For alternative installation instructions refer to https://py.delta.chat/jsonrpc/install.html#install (building from souce or download prbuilds manually).
26+
27+
## Usage
28+
29+
set credentials
30+
```sh
31+
# mac & linux
32+
33+
export MAIL_PW="my_password"
34+
# windows
35+
36+
set MAIL_PW="my_password"
37+
```
38+
39+
start the bot:
40+
```
41+
python echo_bot.py /tmp/dc-accounts
42+
```
43+
44+
or start the version made with the hook system:
45+
```
46+
python ./echo_bot_with_hooks.py --email $MAIL_ADDR --password $MAIL_PW
47+
```
48+
49+
### References
50+
51+
- Python bindings documentation https://py.delta.chat
52+
- https://py.delta.chat/jsonrpc/examples.html
53+
- source code of python bindings https://github.com/deltachat/deltachat-core-rust/tree/master/deltachat-rpc-client

python_jsonrpc/echo_bot.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Example echo bot without using hooks
4+
"""
5+
import logging
6+
import sys
7+
import os
8+
9+
from deltachat_rpc_client import DeltaChat, EventType, Rpc, SpecialContactId
10+
11+
12+
def main():
13+
with Rpc() as rpc:
14+
deltachat = DeltaChat(rpc)
15+
system_info = deltachat.get_system_info()
16+
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
17+
18+
accounts = deltachat.get_all_accounts()
19+
account = accounts[0] if accounts else deltachat.add_account()
20+
21+
account.set_config("bot", "1")
22+
if not account.is_configured():
23+
logging.info("Account is not configured, configuring")
24+
account.set_config("addr", os.environ['MAIL_ADDR'])
25+
account.set_config("mail_pw", os.environ['MAIL_PW'])
26+
account.configure()
27+
logging.info("Configured")
28+
else:
29+
logging.info("Account is already configured")
30+
deltachat.start_io()
31+
32+
def process_messages():
33+
for message in account.get_next_messages():
34+
snapshot = message.get_snapshot()
35+
if snapshot.from_id != SpecialContactId.SELF and not snapshot.is_bot and not snapshot.is_info:
36+
snapshot.chat.send_text(snapshot.text)
37+
snapshot.message.mark_seen()
38+
39+
# Process old messages.
40+
process_messages()
41+
42+
while True:
43+
event = account.wait_for_event()
44+
if event["kind"] == EventType.INFO:
45+
logging.info("%s", event["msg"])
46+
elif event["kind"] == EventType.WARNING:
47+
logging.warning("%s", event["msg"])
48+
elif event["kind"] == EventType.ERROR:
49+
logging.error("%s", event["msg"])
50+
elif event["kind"] == EventType.INCOMING_MSG:
51+
logging.info("Got an incoming message")
52+
process_messages()
53+
54+
55+
if __name__ == "__main__":
56+
logging.basicConfig(level=logging.INFO)
57+
main()

python_jsonrpc/echo_bot_with_hooks.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
"""Minimal echo bot example.
3+
4+
it will echo back any text send to it, it also will print to console all Delta Chat core events.
5+
Pass --help to the CLI to see available options.
6+
"""
7+
from deltachat_rpc_client import events, run_bot_cli
8+
9+
hooks = events.HookCollection()
10+
11+
12+
@hooks.on(events.RawEvent)
13+
def log_event(event):
14+
print(event)
15+
16+
17+
@hooks.on(events.NewMessage)
18+
def echo(event):
19+
snapshot = event.message_snapshot
20+
snapshot.chat.send_text(snapshot.text)
21+
22+
23+
if __name__ == "__main__":
24+
run_bot_cli(hooks)

python_simplebot_plugin/README.MD

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ which is a fork of [deltabot](https://github.com/deltachat-bot/deltabot) and has
77
> There are 2 alternative options available:
88
>
99
> - [using deltabot](../python_deltabot_plugin) (similar to simplebot, but less plugins)
10-
> - [using python directly](../python) (more control, but also more code to take care of)
10+
> - [using python directly](../python_cffi) (more control, but also more code to take care of)
1111
1212
## Usage
1313

0 commit comments

Comments
 (0)