xmpp-rs/xmpp/examples/hello_bot.rs

70 lines
2.3 KiB
Rust
Raw Normal View History

// Copyright (c) 2024 The crate authors
2019-03-21 17:41:29 +00:00
//
// 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;
2019-03-21 17:41:29 +00:00
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,
};
2019-03-21 17:41:29 +00:00
#[tokio::main]
async fn main() -> Result<(), Option<xmpp::Error>> {
env_logger::init();
2019-03-21 17:41:29 +00:00
let args: Vec<String> = args().collect();
if args.len() != 3 {
println!("Usage: {} <jid> <password>", args[0]);
return Err(None);
2019-03-21 17:41:29 +00:00
}
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
2019-03-21 17:41:29 +00:00
let password = &args[2];
// Client instance
let client = ClientBuilder::new(jid, password)
2019-03-21 17:41:29 +00:00
.set_client(ClientType::Bot, "xmpp-rs")
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
.set_default_nick("bot")
.build()?;
2019-03-21 17:41:29 +00:00
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,
2019-03-21 17:41:29 +00:00
}
}
2019-03-21 17:41:29 +00:00
Ok(())
2019-03-21 17:41:29 +00:00
}