Confusion about bot creation / chat initiation

So, I want to write a bot. I did go to https://bots.delta.chat/quickstart.html, which links to https://py.delta.chat/install.html which is dead. The syntax on the quickstart site seems like it is using some old version of the python library (“deltachat” package instead of the json-rpc-bindings).

Anyway, I found the recommended install instructions on Install — Delta Chat 2.10.0 documentation myself. I installed the required packages using pip and copied the “Echo bot with hooks” example. The example requires an existing account, which is not mentioned anywhere. It was rather unclear to me how an account for a bot should be created in the first place. But, I found How to create an account without an account creation QR code on the forum, which allowed me to just make up an account by creating a random account name.

Great, now I am able to run the minimal echo bot example. But, how do I initiate a chat with this new bot account? The android/desktop apps seemingly do not support starting a chat if all you have got is the bots mail address. I guess I first need to create a QR code for the bot from python? But how do I do that?

How is chat initiation with a bot meant to happen? Did I get something fundamental wrong?

If you can get the bot’s public key and make a VCard of it, then attaching the VCard file to a message in any chat (I suggest Saved Messages) in any Deltachat client will create a click-to-use contact, which can also be re-sent and turned into a QR code etc..

Thanks, your answer lead me to the solution.

The Python library provides Account.get_qr_code() which returns the content of the QR code which is necessary to initiate a conversation. This QR code content turns out to be a URL that can be opened in the app or pasted into the QR code scanning window.

Here is a minimal example that allows to enter the mail and password via the CLI and prints the necessary invitation link to the console:

import getpass
import logging
from deltachat_rpc_client import Bot, DeltaChat, EventType, Rpc, events

hooks = events.HookCollection()

@hooks.on(events.NewMessage(func=lambda e: not e.command))
def echo(event):
    snapshot = event.message_snapshot
    if snapshot.text or snapshot.file:
        snapshot.chat.send_message(text=snapshot.text, file=snapshot.file)

def main():
    with Rpc() as rpc:
        deltachat = DeltaChat(rpc)
        system_info = deltachat.get_system_info()
        logging.info("Running deltachat core %s", system_info.deltachat_core_version)

        accounts = deltachat.get_all_accounts()
        account = accounts[0] if accounts else deltachat.add_account()

        bot = Bot(account, hooks)
        if not bot.is_configured():
            print("no account found, please enter mail and password")
            mail = input("mail address:")
            password = getpass.getpass("password:")
            bot.configure(mail, password)

        qr_code_data = bot.account.get_qr_code()
        print(f"Click the following link to start a chat with the bot: {qr_code_data}")
        bot.run_forever()

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    main()

I’m glad my rather hasty reply was useful. I’ll try to answer your other question a bit better.

Bots which are publically-listed are often listed by the Public-bots bot. They also have invitation codes online (as do other accounts). There are such links posted on the forum and elsewhere. Public bots, you will notice, often have non-random addresses.

It is also possible to share the contact info of a bot with another contact within Deltachat, so a bot need not be listed anywhere to become widely-used by word-of-mobile. This hinders blocking.

If you are using python you should probably try deltabot-cli library.

the docs should probably directly point to such high-level libs, just look all the burden it took to @byte to figure out account creation, qr etc. just to reinvent the wheel and do what that lib offers you out of the box

Can you point me to the direction on how to convert vCard to invitation QR code useable by DC app?

Generically, opgpcard is a tool that makes biz cards with QR codes out of keys in your keyring (which should import VCards no problem). I cannot test what DC does with such QR codes as I can’t get opgpcard to actually make QR codes. I suspect that if I could they would contain shortened (fingerprint, not full public key, <400 chars) VCards, which might import just fine into DC (they do when attached as text files). Open standards. :slightly_smiling_face:

More specifically, DC contact QR codes just encode URLs in the form

https://i.delta.chat/#[openPGP fingerprint]&a=[mail address name]%40[domain, nine.testrun.org or whatever]&n=[profile name]&i=[24 chars]&s=[24 chars]

I am not sure what the “i” and “s” fields are (can anyone say?), but the rest is fairly easy to derive from the VCard.

Sorry not to be more helpful.

It’s explained here 2. Securing communications against network adversaries — SecureJoin 0.20.0 documentation

Thank you! I’d forgotten that. Okay, they are contact-specific challenge codes. I don’t think it’s possible to generate these independently of the client, it would make no sense.

I’ve made up some VCards in QR codes and they do not create contacts, either when sent as an attachment or loaded as an image in QR scan. They offer to open as links but fail.

Externally making a QR code encoding the contact link works, the QR code creates a contact, but of course the challenges must be included.