multi_user_bot/
multi_user_bot.rs

1//! The example expects that the bot account was already pre-created via CLI by `create bot
2//! <bot_name> <bot_fullname>` command.
3//!
4//! To compile this example pass the --all-features flag like this:
5//! `cargo run --example squaring_bot --all-features`
6//!
7//! ----
8//!
9//! TODO: Description
10
11use futures::TryStreamExt as _;
12use simploxide_api_types::LocalProfile;
13use simploxide_client::{
14    prelude::*,
15    types::{AutoAccept, CIContent},
16};
17use std::{
18    error::Error,
19    sync::{Arc, Mutex},
20};
21
22#[tokio::main]
23async fn main() -> Result<(), Box<dyn Error>> {
24    let (client, mut events) = simploxide_client::connect("ws://127.0.0.1:5225").await?;
25
26    // Use destructuring to extract data from the expected response
27    let ShowActiveUserResponse::ActiveUser(ActiveUserResponse { ref user, .. }) =
28        *client.show_active_user().await?
29    else {
30        return Err("No active user profile".into());
31    };
32
33    println!(
34        "Bot profile: {} ({})",
35        user.profile.display_name, user.profile.full_name
36    );
37
38    // Alternatively, use response getters
39    let (address_long, address_short) = match client
40        .api_show_my_address(user.user_id)
41        .await?
42        .user_contact_link()
43    {
44        Some(resp) => (
45            resp.contact_link.conn_link_contact.conn_full_link.clone(),
46            resp.contact_link.conn_link_contact.conn_short_link.clone(),
47        ),
48        None => client
49            .api_create_my_address(user.user_id)
50            .await?
51            .user_contact_link_created()
52            .map(|resp| {
53                (
54                    resp.conn_link_contact.conn_full_link.clone(),
55                    resp.conn_link_contact.conn_short_link.clone(),
56                )
57            })
58            .ok_or("Failed to create bot address")?,
59    };
60
61    println!("Bot long address: {address_long}");
62    println!("Bot short address: {address_short:?}");
63
64    let mut counter = 0;
65    'reactor: while let Some(ev) = events.try_next().await? {
66        match ev.as_ref() {
67            // Got the message -> square the number
68            Event::NewChatItems(new_msgs) => {
69                counter += 1;
70                if counter > 10 {
71                    break 'reactor;
72                }
73            }
74            // Ignore all other events
75            _ => (),
76        }
77    }
78
79    client
80        .api_set_address_settings(
81            user.user_id,
82            AddressSettings {
83                business_address: false,
84                auto_accept: None,
85                auto_reply: None,
86                undocumented: Default::default(),
87            },
88        )
89        .await?;
90
91    client
92        .api_set_profile_address(ApiSetProfileAddress {
93            user_id: user.user_id,
94            enable: false,
95        })
96        .await?;
97
98    Ok(())
99}
100
101/// One of the ways to conveniently provide helper methods to your bot is to contain the client in
102/// a new type or a record type, and implement higher-level methods on it. You can implement
103/// `Deref<Target=Client>` or make client field public to keep access to the lower-level API
104/// methods.
105struct SupportDesk {
106    team: Vec<SupportDeskBot>,
107}
108
109struct SupportDeskBot {
110    client: simploxide_client::Client,
111    user_id: i64,
112    address: String,
113    profile: Profile,
114}
115
116/// Use trait extensions to provide extra methods for `simploxide-api-types`
117trait ProfileExts {
118    fn from_local(local: LocalProfile) -> Self;
119}
120
121impl ProfileExts for Profile {
122    fn from_local(local: LocalProfile) -> Profile {
123        Profile {
124            display_name: local.display_name,
125            full_name: local.full_name,
126            short_descr: local.short_descr,
127            image: local.image,
128            contact_link: local.contact_link,
129            preferences: local.preferences,
130            peer_type: local.peer_type,
131            undocumented: local.undocumented,
132        }
133    }
134}