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 24)
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}