58 lines
1.7 KiB
Rust
58 lines
1.7 KiB
Rust
pub use golgi::sbot::{Sbot, Keystore};
|
|
use async_std::stream::StreamExt;
|
|
use futures::TryStreamExt;
|
|
|
|
use crate::error::SsbBotError;
|
|
|
|
pub mod error;
|
|
|
|
|
|
|
|
pub struct SsbBot {
|
|
sbot: Sbot
|
|
}
|
|
|
|
impl SsbBot {
|
|
pub async fn init(
|
|
keystore: Keystore,
|
|
ip_port: Option<String>,
|
|
net_id: Option<String>,
|
|
) -> Result<SsbBot, SsbBotError> {
|
|
println!("init!");
|
|
let sbot = Sbot::init(keystore, ip_port, net_id).await?;
|
|
Ok(Self {
|
|
sbot,
|
|
})
|
|
}
|
|
|
|
pub async fn listen_for_message(&mut self, msg: &str) -> Result<(), SsbBotError> {
|
|
|
|
// Create an ordered stream of all messages authored by the `author`
|
|
// identity.
|
|
let author = "@L/z54cbc8V1kL1/MiBhpEKuN3QJkSoZYNaukny3ghIs=.ed25519";
|
|
let mut history_stream = self.sbot
|
|
.create_history_stream(author.to_string())
|
|
.await?;
|
|
|
|
// Pin the stream to the stack to allow polling of the `future`.
|
|
futures::pin_mut!(history_stream);
|
|
|
|
println!("looping through stream");
|
|
|
|
// Iterate through each element in the stream and match on the `Result`.
|
|
// In this case, each element has type `Result<SsbMessageValue, GolgiError>`.
|
|
while let Some(res) = history_stream.next().await {
|
|
match res {
|
|
Ok(value) => {
|
|
// Print the `SsbMessageValue` of this element to `stdout`.
|
|
println!("value: {:?}", value);
|
|
}
|
|
Err(err) => {
|
|
// Print the `GolgiError` of this element to `stderr`.
|
|
eprintln!("err: {:?}", err);
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
} |