Examples

Once you have installed deltachat bindings you need email/password credentials for an IMAP/SMTP account. Delta Chat developers and the CI system use a special URL to create temporary email accounts on testrun.org for testing.

Receiving a Chat message from the command line

Here is a simple bot that:

  • receives a message and sends back (“echoes”) a message

  • terminates the bot if the message /quit is sent

# content of echo_and_quit.py

from deltachat import account_hookimpl, run_cmdline


class EchoPlugin:
    @account_hookimpl
    def ac_incoming_message(self, message):
        print("process_incoming message", message)
        if message.text.strip() == "/quit":
            message.account.shutdown()
        else:
            # unconditionally accept the chat
            message.create_chat()
            addr = message.get_sender_contact().addr
            if message.is_system_message():
                message.chat.send_text(f"echoing system message from {addr}:\n{message}")
            else:
                text = message.text
                message.chat.send_text(f"echoing from {addr}:\n{text}")

    @account_hookimpl
    def ac_message_delivered(self, message):
        print("ac_message_delivered", message)


def main(argv=None):
    run_cmdline(argv=argv, account_plugins=[EchoPlugin()])


if __name__ == "__main__":
    main()

With this file in your working directory you can run the bot by specifying a database path, an email address and password of a SMTP-IMAP account:

$ cd examples
$ python echo_and_quit.py /tmp/db --email ADDRESS --password PASSWORD

While this process is running you can start sending chat messages to ADDRESS.

Track member additions and removals in a group

Here is a simple bot that:

  • echoes messages sent to it

  • tracks if configuration completed

  • tracks member additions and removals for all chat groups

# content of group_tracking.py

from deltachat import account_hookimpl, run_cmdline


class GroupTrackingPlugin:
    @account_hookimpl
    def ac_incoming_message(self, message):
        print("process_incoming message", message)
        if message.text.strip() == "/quit":
            message.account.shutdown()
        else:
            # unconditionally accept the chat
            message.create_chat()
            addr = message.get_sender_contact().addr
            text = message.text
            message.chat.send_text(f"echoing from {addr}:\n{text}")

    @account_hookimpl
    def ac_outgoing_message(self, message):
        print("ac_outgoing_message:", message)

    @account_hookimpl
    def ac_configure_completed(self, success):
        print("ac_configure_completed:", success)

    @account_hookimpl
    def ac_chat_modified(self, chat):
        print("ac_chat_modified:", chat.id, chat.get_name())
        for member in chat.get_contacts():
            print(f"chat member: {member.addr}")

    @account_hookimpl
    def ac_member_added(self, chat, contact, actor, message):
        print(f"ac_member_added {contact.addr} to chat {chat.id} from {actor or message.get_sender_contact().addr}")
        for member in chat.get_contacts():
            print(f"chat member: {member.addr}")

    @account_hookimpl
    def ac_member_removed(self, chat, contact, actor, message):
        print(f"ac_member_removed {contact.addr} from chat {chat.id} by {actor or message.get_sender_contact().addr}")


def main(argv=None):
    run_cmdline(argv=argv, account_plugins=[GroupTrackingPlugin()])


if __name__ == "__main__":
    main()

With this file in your working directory you can run the bot by specifying a database path, an email address and password of a SMTP-IMAP account:

python group_tracking.py --email ADDRESS --password PASSWORD /tmp/db

When this process is running you can start sending chat messages to ADDRESS.

Writing bots for real

The deltabot repository contains a little framework for writing deltachat bots in Python.