use simploxide_client::{
prelude::*,
ws::{self, ClientResult},
};
use std::{error::Error, sync::Arc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let (bot, events, mut cli) = ws::BotBuilder::new("SimplOxide Examples", 5225)
.db_prefix("test_db/bot")
.auto_accept_with(
"Hello, I'm a simple squaring bot. Send me a number and I will calculate its square",
)
.launch()
.await?;
let address = bot.address().await?;
println!("Bot address: {address}");
events.into_dispatcher(bot).on(new_msgs).dispatch().await?;
cli.kill().await?;
Ok(())
}
async fn new_msgs(ev: Arc<NewChatItems>, bot: ws::Bot) -> ClientResult<StreamEvents> {
for (chat, msg, content) in ev.chat_items.filter_messages() {
if msg.meta.item_text.trim() == "/die" {
return Ok(StreamEvents::Break);
}
if let Some(num) = content
.text()
.and_then(|txt| txt.trim().parse::<i64>().ok())
{
let square = num.saturating_mul(num);
bot.send_msg(chat, format!("Squared: {square}"))
.reply_to(msg)
.send()
.await?;
} else {
bot.send_msg(chat, "Me understands only numbers!")
.reply_to(msg)
.send()
.await?;
}
}
Ok(StreamEvents::Continue)
}