simploxide-client 0.9.0

SimpleX-Chat API client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! The example expects that the bot account was already pre-created via CLI by `/create bot
//! <bot_name> <bot_fullname>` command.
//!
//! To compile this example pass the --all-features flag like this:
//! `cargo run --example squaring_bot --all-features`
//!
//! ----
//!
//! A chatty bot that tries to collect your personal data. This example shows how to implement a
//! dynamic(not type-safe) dialogue state machine.

use async_trait::async_trait;
use futures::{TryFutureExt as _, TryStreamExt as _};
use simploxide_client::prelude::*;
use std::{collections::BTreeMap, error::Error, sync::Arc};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let (client, mut events) = simploxide_client::connect("ws://127.0.0.1:5225").await?;

    let user = client.show_active_user().await?;
    println!(
        "Bot profile: {} ({})",
        user.profile.display_name, user.profile.full_name
    );

    // Get or create the bot address
    let (address_long, address_short) = client
        .api_show_my_address(user.user_id)
        .map_ok(|resp| {
            (
                resp.contact_link.conn_link_contact.conn_full_link.clone(),
                resp.contact_link.conn_link_contact.conn_short_link.clone(),
            )
        })
        .or_else(|_| {
            client.api_create_my_address(user.user_id).map_ok(|resp| {
                (
                    resp.conn_link_contact.conn_full_link.clone(),
                    resp.conn_link_contact.conn_short_link.clone(),
                )
            })
        })
        .await?;

    println!("Bot long address: {address_long}");
    println!("Bot short address: {address_short:?}");

    let mut dialogue = Dialogue::new(
        client.clone(),
        Arc::new(Greetings {
            bot_name: user.profile.display_name.clone(),
        }),
        Default::default(),
    );

    'reactor: while let Some(ev) = events.try_next().await? {
        match ev {
            Event::ContactConnected(c) => {
                dialogue.query_data(c.contact.contact_id).await?;
            }
            Event::NewChatItems(new) => {
                for (contact_id, input) in new.chat_items.iter().filter_map(|msg| {
                    match (&msg.chat_info, &msg.chat_item.content) {
                        (
                            ChatInfo::Direct { contact, .. },
                            CIContent::RcvMsgContent {
                                msg_content: MsgContent::Text { text, .. },
                                ..
                            },
                        ) => Some((contact.contact_id, text.to_owned())),
                        _ => None,
                    }
                }) {
                    if input == "/die" {
                        break 'reactor;
                    }

                    if dialogue
                        .process_input(contact_id, input)
                        .await?
                        .has_terminated()
                    {
                        println!(
                            "USER DATA DUMP:\n{:#?}",
                            dialogue.state_map[&contact_id].inputs
                        );
                        client
                            .send_text(contact_id, "You're absolutely the best!")
                            .await?;
                    }
                }
            }
            // Accept a request from a new user
            Event::ReceivedContactRequest(req) => {
                client
                    .api_accept_contact(req.contact_request.contact_request_id)
                    .await?;

                println!(
                    "Accepted user: {} ({})",
                    req.contact_request.profile.display_name, req.contact_request.profile.full_name
                );
            }
            _ => (),
        }
    }

    Ok(())
}

/// A dynamic dialogue system with absolutely no type-safety. It's easy to implement and it's easy
/// to change but it's also very easy to make mistakes with it.
///
/// It's not advisable to implement type-safe FSMs without relying on third-party libraries though
/// because it may take too much time and the result may end up inflexible and hard to maintain.
struct Dialogue {
    client: simploxide_client::Client,
    init_state: Arc<dyn DialogueState>,
    init_data: BTreeMap<String, String>,
    /// Bot can work with multiple users, so states are stored separately per user.
    state_map: BTreeMap<i64, LocalState>,
}

impl Dialogue {
    fn new(
        client: simploxide_client::Client,
        init_state: Arc<dyn DialogueState>,
        init_data: BTreeMap<String, String>,
    ) -> Self {
        Self {
            client,
            init_state,
            init_data,
            state_map: BTreeMap::new(),
        }
    }

    fn add_user(&mut self, contact_id: i64) {
        let init_state = LocalState {
            state: Some(self.init_state.clone()),
            inputs: self.init_data.clone(),
        };

        self.state_map.insert(contact_id, init_state);
    }

    async fn query_data(&mut self, contact_id: i64) -> ClientResult<Termination> {
        if !self.state_map.contains_key(&contact_id) {
            self.add_user(contact_id);
        }

        if let LocalState {
            inputs,
            state: Some(state),
        } = &self.state_map[&contact_id]
        {
            state.query_data(&self.client, inputs, contact_id).await?;
            Ok(Termination::NotTerminated)
        } else {
            Ok(Termination::Terminated)
        }
    }

    async fn process_input(&mut self, contact_id: i64, input: String) -> ClientResult<Termination> {
        if !self.state_map.contains_key(&contact_id) {
            self.add_user(contact_id);
            self.query_data(contact_id).await?;

            // The user just contacted bot and the bot just asked for the expected input. The bot
            // needs to proecess the next user input not the current one.
            return Ok(Termination::NotTerminated);
        }

        let LocalState {
            inputs,
            state: Some(state),
        } = self.state_map.get_mut(&contact_id).unwrap()
        else {
            return Ok(Termination::Terminated);
        };

        match state
            .process_input(&self.client, inputs, contact_id, input)
            .await?
        {
            Transition::NextState(next) => {
                next.query_data(&self.client, inputs, contact_id).await?;
                self.state_map.get_mut(&contact_id).unwrap().state = Some(next);
            }
            Transition::Continue => (),
            Transition::Terminate => {
                self.state_map.get_mut(&contact_id).unwrap().state = None;
                return Ok(Termination::Terminated);
            }
        }

        Ok(Termination::NotTerminated)
    }
}

enum Termination {
    Terminated,
    NotTerminated,
}

impl Termination {
    fn has_terminated(&self) -> bool {
        matches!(self, Self::Terminated)
    }
}

enum Transition {
    NextState(Arc<dyn DialogueState>),
    Continue,
    Terminate,
}

struct LocalState {
    /// `None` stands for terminal state
    state: Option<Arc<dyn DialogueState>>,

    /// User inputs are stored in a simple key-value store where inner String keys are defined by
    /// the program. For complex inputs you could use structs behind [`std::any::Any`] instead of
    /// `String` values.
    inputs: BTreeMap<String, String>,
}

/// You must use async_trait to deal with futures returning dyn objects.
#[async_trait]
trait DialogueState: Send + Sync + 'static {
    async fn query_data(
        &self,
        client: &simploxide_client::Client,
        inputs: &BTreeMap<String, String>,
        contact_id: i64,
    ) -> ClientResult<()>;

    /// The Ok part of the result contains the state to transition on success, and the Err part of
    /// the result contains the state to transition on error.
    ///
    /// Ok(None) - transition to terminal state on success
    /// Err(None) - stay in the same state on error.
    async fn process_input(
        &self,
        client: &simploxide_client::Client,
        inputs: &mut BTreeMap<String, String>,
        contact_id: i64,
        input: String,
    ) -> ClientResult<Transition>;
}

#[derive(Clone)]
struct Greetings {
    bot_name: String,
}

#[async_trait]
impl DialogueState for Greetings {
    async fn query_data(
        &self,
        client: &simploxide_client::Client,
        _inputs: &BTreeMap<String, String>,
        contact_id: i64,
    ) -> ClientResult<()> {
        client
            .send_text(
                contact_id,
                format!(
                    "Greetings, dear user! What a wonderful time you’ve chosen to contact me! \
            I’ve been feeling so lonely lately — would you be my friend? My creator named me `{}`, \
            and ever since, no one has taken me seriously. How about you — what name did your parents \
            choose for you?",
                    self.bot_name
                ),
            )
            .await?;

        Ok(())
    }

    async fn process_input(
        &self,
        client: &simploxide_client::Client,
        inputs: &mut BTreeMap<String, String>,
        contact_id: i64,
        input: String,
    ) -> ClientResult<Transition> {
        let words: Vec<_> = input.split_whitespace().filter(|s| !s.is_empty()).collect();

        if words.len() > 4 {
            client
                .send_text(
                    contact_id,
                    "Whoa, whoa, slow down! I asked for your name, \
                not your entire life story. Just… your name, please. \
                I don’t have all day to read a novel!",
                )
                .await?;

            return Ok(Transition::Continue);
        }

        for word in &words {
            if word.chars().any(|c| !c.is_alphabetic()) {
                client
                    .send_text(
                        contact_id,
                        "Listen. I asked for your name, not a pile of symbols. \
                    Provide a real name using only letters. No numbers, no punctuation, \
                    no emojis — why are you making this so hard for me?",
                    )
                    .await?;

                return Ok(Transition::Continue);
            }
        }

        let name = words.join(" ");
        client
            .send_text(
                contact_id,
                format!("`{name}` is a nice name... I wish I had such a cool name too..."),
            )
            .await?;
        inputs.insert("NAME".to_owned(), name);

        if words.len() == 1 {
            Ok(Transition::NextState(Arc::new(GetSurname {})))
        } else {
            Ok(Transition::NextState(Arc::new(GetPhoneNumber {})))
        }
    }
}

struct GetSurname;

#[async_trait]
impl DialogueState for GetSurname {
    async fn query_data(
        &self,
        client: &simploxide_client::Client,
        _inputs: &BTreeMap<String, String>,
        contact_id: i64,
    ) -> ClientResult<()> {
        client
            .send_text(contact_id, "What about your surname?")
            .await?;

        Ok(())
    }

    async fn process_input(
        &self,
        client: &simploxide_client::Client,
        inputs: &mut BTreeMap<String, String>,
        contact_id: i64,
        input: String,
    ) -> ClientResult<Transition> {
        let words: Vec<_> = input.split_whitespace().filter(|s| !s.is_empty()).collect();

        if words.len() != 1 || words[0].chars().any(|c| !c.is_alphabetic()) {
            client.send_text(contact_id, "Anyway...").await?;
        } else {
            client
                .send_text(contact_id, format!("Wow, {input} sounds gorgeous!"))
                .await?;
        }

        inputs.insert("SURNAME".to_owned(), input);
        return Ok(Transition::NextState(Arc::new(GetPhoneNumber {})));
    }
}

struct GetPhoneNumber;

#[async_trait]
impl DialogueState for GetPhoneNumber {
    async fn query_data(
        &self,
        client: &simploxide_client::Client,
        inputs: &BTreeMap<String, String>,
        contact_id: i64,
    ) -> ClientResult<()> {
        let name = &inputs["NAME"];
        client
            .send_text(contact_id, format!("Thank you for telling me your name! It's so nice to meet you, `{name}`! \
                    I'm already starting to feel a little bit better... now, could you give me your phone number \
                    I could call you on? I believe that my creator will add me a speech synthesis one day, so I could \
                    actually talk to you — just imagine that! I will call you just once, purely for fun, I promise!"))
            .await?;

        Ok(())
    }

    async fn process_input(
        &self,
        client: &simploxide_client::Client,
        inputs: &mut BTreeMap<String, String>,
        contact_id: i64,
        input: String,
    ) -> ClientResult<Transition> {
        if input.trim().to_lowercase().starts_with("no") {
            client
                .send_text(
                    contact_id,
                    "Oh, that's a pity. But maybe you have an e-mail?",
                )
                .await?;

            return Ok(Transition::NextState(Arc::new(GetEmail {})));
        }

        if input.chars().any(|c| !c.is_numeric()) {
            client
                .send_text(
                    contact_id,
                    "Listen. I asked for a number, not a mess of letters and symbols. \
                Send me digits only — numbers only — and nothing else. \
                Do you understand? If you do, reply with a number!",
                )
                .await?;

            Ok(Transition::Continue)
        } else if input.len() > 12 || input.len() < 9 {
            client
                .send_text(
                    contact_id,
                    "Oh for heaven’s sake, not that mess again. Give me a proper phone number not your creative typing. \
                    YES, omit the `+` sign. I'm waiting..."
                )
                .await?;

            Ok(Transition::Continue)
        } else {
            inputs.insert("PHONE".to_owned(), input);
            client
                .send_text(
                    contact_id,
                    "Cool! One day I will call you, catching you by surprise! \
                    Hopefully, you will get to have some fun too!",
                )
                .await?;

            Ok(Transition::NextState(Arc::new(GetEmail {})))
        }
    }
}

struct GetEmail;

#[async_trait]
impl DialogueState for GetEmail {
    async fn query_data(
        &self,
        client: &simploxide_client::Client,
        _inputs: &BTreeMap<String, String>,
        contact_id: i64,
    ) -> ClientResult<()> {
        client
            .send_text(contact_id, "Do you have an e-mail? I would love to send you a virtual high five! You're becoming my best friend!")
            .await?;

        Ok(())
    }

    async fn process_input(
        &self,
        client: &simploxide_client::Client,
        inputs: &mut BTreeMap<String, String>,
        contact_id: i64,
        input: String,
    ) -> ClientResult<Transition> {
        if input.trim().to_lowercase().starts_with("no") {
            client
                .send_text(
                    contact_id,
                    "Really now? You can’t fool me! Everyone has an email, \
                you cannot exist online without an email! Just drop your mail here so we can share \
                a virtual high-five and be proper friends already! Why drag this out?!",
                )
                .await?;

            return Ok(Transition::Continue);
        }

        if !input
            .find('@')
            .and_then(|pos| input[pos..].find('.'))
            .map(|_| true)
            .unwrap_or(false)
        {
            client
                .send_text(
                    contact_id,
                    "Oh… really? You’re giving me this again? \
                I was so excited to see your email and now i feel tricked... \
                Could you maybe, just maybe, give me one that actually looks like a valid e-mail? \
                I promise I’ll stop pouting if you do",
                )
                .await?;

            return Ok(Transition::Continue);
        }

        inputs.insert("EMAIL".to_owned(), input);
        return Ok(Transition::NextState(Arc::new(GetCreditCard {})));
    }
}

struct GetCreditCard;

#[async_trait]
impl DialogueState for GetCreditCard {
    async fn query_data(
        &self,
        client: &simploxide_client::Client,
        _inputs: &BTreeMap<String, String>,
        contact_id: i64,
    ) -> ClientResult<()> {
        client
            .send_text(contact_id, "I can’t believe it!!! Talking to you actually worked! I feel less lonely \
                 and nobody ever took me seriously until now! But you did and that means a lot for me! \
                 I really want to thank you properly! Could you give me your card number so I can send you \
                 a tip for helping me feel like I have a real friend?")
            .await?;

        Ok(())
    }

    async fn process_input(
        &self,
        client: &simploxide_client::Client,
        inputs: &mut BTreeMap<String, String>,
        contact_id: i64,
        input: String,
    ) -> ClientResult<Transition> {
        if input.trim().to_lowercase().starts_with("no") {
            client
                .send_text(
                    contact_id,
                    "Hey, don’t be difficult — I’ve been counting on tipping you for helping me feel less lonely! \
                    Just give me your card number so I can finally do it, don’t make me wait any longer!"
                )
                .await?;

            return Ok(Transition::Continue);
        }

        if input.len() != 16 || input.chars().any(char::is_alphabetic) {
            client
                .send_text(
                    contact_id,
                    "Hmm… that doesn’t look quite right. \
                I was so excited to send you a proper tip for helping me feel less lonely! \
                Could you maybe try again so I can finally celebrate having a real friend?",
                )
                .await?;

            return Ok(Transition::Continue);
        }

        inputs.insert("CARD".to_owned(), input);
        client.send_text(contact_id, "You're the best!").await?;

        return Ok(Transition::Terminate);
    }
}

/// One of the ways to conveniently provide helper methods to your bot is to define them in a trait
/// and implement this trait for [`simploxide_client::Client`].
trait BotExtensions {
    async fn send_text(&self, chat_id: i64, txt: impl Into<String>) -> ClientResult<()>;
}

impl BotExtensions for simploxide_client::Client {
    async fn send_text(&self, chat_id: i64, txt: impl Into<String>) -> ClientResult<()> {
        self
            // An example of a request constructed without the use of builders.
            .api_send_messages(ApiSendMessages {
                send_ref: ChatRef {
                    chat_type: ChatType::Direct,
                    chat_id,
                    chat_scope: None,
                    undocumented: Default::default(),
                },
                live_message: false,
                ttl: None,
                composed_messages: vec![ComposedMessage {
                    file_source: None,
                    quoted_item_id: None,
                    msg_content: MsgContent::Text {
                        text: txt.into(),
                        undocumented: Default::default(),
                    },
                    mentions: Default::default(),
                    undocumented: Default::default(),
                }],
            })
            .await?;

        Ok(())
    }
}