I’m calling the API with the Python bindings from within a bash script in the following way:
test.sh
#!/usr/bin/env bash
set -uo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
test () {
python -c "import deltachat
ac = deltachat.account.Account('$SCRIPT_DIR/deltachat_db/db')
ac.run_account(addr='xxxxxx@nine.testrun.org', password='xxxxxx')
chat = [c for c in ac.get_chats() if c.get_name() == 'Test'][0]
chat.send_text('msg from $(hostname)')
"
}
test
echo "done"
However, the message is not delivered. Only when using a bash timeout and ac.wait_shutdown(), the message is sent successfully:
test2.sh
#!/usr/bin/env bash
set -uo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
test () {
timeout 2 python -c "import deltachat
ac = deltachat.account.Account('$SCRIPT_DIR/deltachat_db/db')
ac.run_account(addr='xxxxxx@nine.testrun.org', password='xxxxxx')
chat = [c for c in ac.get_chats() if c.get_name() == 'Test'][0]
chat.send_text('msg from $(hostname)')
ac.wait_shutdown()
"
}
test
echo "done"
I guess this is because the test.sh script exits while the sending action is still in progress and has not yet completed. However, my workaround from test2.sh seems not clean, because if the process takes longer than the 2 seconds granted, it may still fail.
I was unable to find a function in the bot API that blocks until all events are handled. Only wait_shutdown was able to process the events, but it blocks forever. Is there any API function that can do this? I have tried maybe_network(), stop_io(), stop_ongoing() and shutdown() but they all have no effect.
There is no easy way to do this, bots are supposed to be long-running processes. If you want to use the bot to send some sort of notifications, better start a bot process that takes new notifications from some IPC channel or just a spool folder and sends them out without terminating the connection to SMTP and IMAP each time.
There is a dc_send_msg_sync C API that can be used to send a single message over a dedicated SMTP connection and wait for sending to finish, but it is not even exposed to high-level Python API.
Thanks for your answer. I see that I’m using it in an unintended way. However, writing a full-blown bot would be too much of an overkill for my setup. I thought I could replicate the Telegram API where you can just send a curl request to get the message through.
I have made some scripts in the past to send a message with DC core and terminate, when you send the message, you get back the message ID, with that, you can listen to the core events and wait for the “message delivered” event with the corresponding message ID of the message you just sent, then terminate/stop IO
it is not a long-running process, as I said it is:
so it does what you want, just send a message with SMTP and terminate execution
note: what the script does is to add a sub-command to the bot CLI “send” that can be used to send files and exit instead of using the long-running sub-command “serve” example usage: