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