xmpp-rs/xmpp/examples/hello_bot.rs
Maxime “pep” Buquet f090110227
xmpp: Rename tmp example to hello_bot
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2024-08-22 14:03:56 +02:00

69 lines
2.3 KiB
Rust

// 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::{minidom::Element, parsers::message::Message};
use xmpp::{
jid::{BareJid, Jid},
stream::{IqRequest, IqRequestType, NonTransactional},
ClientBuilder, ClientFeature, ClientType,
};
#[tokio::main]
async fn main() -> Result<(), Option<xmpp::Error>> {
env_logger::init();
let args: Vec<String> = args().collect();
if args.len() != 3 {
println!("Usage: {} <jid> <password>", args[0]);
return Err(None);
}
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
let password = &args[2];
// Client instance
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")
.build()?;
println!("FOO0: {:?}", client);
let elem: Element = r#"<ping xmlns="urn:xmpp:ping"/>"#.parse().unwrap();
let req = IqRequest {
to: Jid::new("bouah.net").unwrap(),
data: IqRequestType::Get(elem),
};
let resp = client.send_iq(req).await.unwrap();
println!("Response: {:?}", resp);
let receiver = client.misc_receiver().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(())
}