connect

Function connect 

Source
pub async fn connect<S: AsRef<str>>(
    uri: S,
) -> Result<(Client, EventStream), WsError>
Expand description

A wrapper over simploxide_core::connect that turns simploxide_core::RawClient into Client and the event queue into the event stream with automatic event deserialization.

let (client, mut events) = simploxide_client::connect("ws://127.0.0.1:5225").await?;

let current_user  = client.api_show_active_user().await?;
println!("{current_user:#?}");

while let Some(ev) = events.try_next().await? {
    // Process events...
}
Examples found in repository?
examples/multi_user_bot.rs (line 18)
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}