How can I initiate a 1:1 conversation for any given mail address from a bot written in Rust?

Hi :wave::

I’m looking at the Rust API for bots and I cannot find the proper way to create a new Chat for a given mail address. It appears that the only way to access such 1:1 chats is by having them already in the database, which would imply that the bot cannot start a conversation with any contact it wants (ie: it’s the contact who must send the first message, as in Telegram API).

Is that by design? Is it a lack of feature? Or is it simply that I’m not able to figure it out? :thinking:

Thanks in advance.

you can look up api here: deltachat - Rust though the c api might be more complete in terms of documentation comments: https://c.delta.chat/

The function you are looking for is Contact::add_or_lookup, though I personally would also have expected some more clear naming like a method with “create” or “new” in it’s name.

1 Like

Thx for the fast reply :slight_smile:. However, I’m not sure if that’s what I’m looking for…

That function adds a contact, but I still need a Chat to use send_msg, and that’s the root of my problem: how do I create a Chat if it is not already present in the DB.

Maybe creating a group chat with create_group_chat and adding a single contact would work? :thinking: I don’t think so, because it would still allow me to add more contacts and the Chat object would not return Single as chat type, I guess…

I’ll review the C API docs in more depth because I hadn’t the link (thx for that).

Regarding Rust docs, I didn’t know about the link but I generated them with cargo doc locally :slight_smile: .

hi @Ivan, I guess you can use ChatId::create_for_contact

I am not a Rust developer, but if I would create a bot in Rust I would have a look at the usage of the API by the deltachat-jsonrpc program, for example, this is how the Rust API is used to create a 1:1 chat there:

so you would first use the email address to create the contact:

and then use the contact to create a chat with the previously mentioned ChatId::create_for_contact

1 Like

Whoa, very unexpected. Good catch :ok_hand:

I was misled by the fact that ChatId sounded like an “empty” type just to wrap the number that identified the chat so I didn’t really realise that I could find the method there.

In fact, looking at the ContactId API for example, there are no methods to create contacts because they are in Contact, as @Simon pointed out in the first answer. Thus, I expected something like Chat::create_for_contact or Contact::create_chat

In any case, thx to both for the answers, I was getting crazy :sweat_smile:

2 Likes