Update foo example

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2024-08-22 00:59:36 +02:00
parent 42130dd927
commit 9543c0985b

View file

@ -1,17 +1,18 @@
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
// Copyright (c) 2024 The crate authors
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use env_logger;
use log::debug;
use std::env::args;
use std::str::FromStr;
use tokio_xmpp::parsers::{message::MessageType, BareJid, Jid};
use tokio_xmpp::Element;
use tokio_xmpp::{minidom::Element, parsers::message::Message};
use xmpp::{
stream::{IqRequest, IqRequestType},
ClientBuilder, ClientFeature, ClientType, Event,
jid::{BareJid, Jid},
stream::{IqRequest, IqRequestType, NonTransactional},
ClientBuilder, ClientFeature, ClientType,
};
#[tokio::main]
@ -28,13 +29,10 @@ async fn main() -> Result<(), Option<xmpp::Error>> {
let password = &args[2];
// Client instance
let mut client = ClientBuilder::new(jid, password)
let client = ClientBuilder::new(jid, password)
.set_client(ClientType::Bot, "xmpp-rs")
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
.set_default_nick("bot")
.enable_feature(ClientFeature::Avatars)
.enable_feature(ClientFeature::ContactList)
.enable_feature(ClientFeature::JoinRooms)
.build()?;
println!("FOO0: {:?}", client);
@ -44,11 +42,28 @@ async fn main() -> Result<(), Option<xmpp::Error>> {
to: Jid::new("bouah.net").unwrap(),
data: IqRequestType::Get(elem),
};
let resp = client.send_iq(req).await?;
let resp = client.send_iq(req).await.unwrap();
println!("Response: {:?}", resp);
let _ = client.disconnect().await?;
let receiver = client.misc_receiver().await;
// tokio::time::sleep(tokio::time::Duration::from_millis(10000)).await;
loop {
match receiver.lock().await.recv().await {
Some(NonTransactional::Presence(presence)) => debug!("Presence: {:?}", presence),
Some(NonTransactional::Message(message)) => {
debug!("Message: {:?}", message);
let (lang, body) =
if let Some((lang, body)) = message.get_best_body(vec!["en", "fr"]) {
(lang.to_string(), format!("Echo: {}", body.0))
} else {
(format!("en"), format!("Echo?!"))
};
let echo_jid = message.from.unwrap();
let _echo = Message::groupchat(echo_jid).with_body(lang, body);
// client.send_message(echo).await?
}
None => break,
}
}
Ok(())
}