golgi/examples/basic.rs

100 lines
3.4 KiB
Rust
Raw Normal View History

2021-12-02 17:48:16 +00:00
use std::process;
use golgi::{messages::SsbMessageContent, sbot::Keystore, GolgiError, Sbot};
2021-12-02 17:48:16 +00:00
2022-02-15 08:26:51 +00:00
// Golgi is an asynchronous library so we must call it from within an
// async function. The `GolgiError` type encapsulates all possible
// error variants for the library.
2021-12-02 17:48:16 +00:00
async fn run() -> Result<(), GolgiError> {
// Attempt to initialise a connection to an sbot instance using the
// secret file at the Patchwork path and the default IP address, port
// and network key (aka. capabilities key).
let mut sbot_client = Sbot::init(Keystore::Patchwork, None, None).await?;
2021-12-02 17:48:16 +00:00
2022-02-15 08:26:51 +00:00
// Alternatively, we could specify a non-standard IP and port.
// let ip_port = "127.0.0.1:8021".to_string();
// let mut sbot_client = Sbot::init(Some(ip_port), None).await?;
// Call the `whoami` RPC method to retrieve the public key for the sbot
// identity. This is our 'local' public key.
2021-12-02 17:48:16 +00:00
let id = sbot_client.whoami().await?;
2022-02-15 08:26:51 +00:00
// Print the public key (identity) to `stdout`.
2022-01-05 20:08:51 +00:00
println!("whoami: {}", id);
2021-12-02 17:48:16 +00:00
2022-02-15 08:26:51 +00:00
// Compose an SSB `about` message type.
// The `SsbMessageContent` type has many variants and allows for a high
// degree of control when creating messages.
2022-01-14 17:41:15 +00:00
let name = SsbMessageContent::About {
about: id.clone(),
name: Some("golgi".to_string()),
title: None,
branch: None,
image: None,
description: None,
location: None,
start_datetime: None,
};
2022-02-15 08:26:51 +00:00
// Publish the name message. The `publish` method returns a reference to
// the published message.
2022-01-14 17:41:15 +00:00
let name_msg_ref = sbot_client.publish(name).await?;
2022-02-15 08:26:51 +00:00
// Print the message reference to `stdout`.
2022-01-14 17:41:15 +00:00
println!("name_msg_ref: {}", name_msg_ref);
2021-12-29 16:38:03 +00:00
2022-02-15 08:26:51 +00:00
// Compose an SSB `post` message type.
2021-12-29 16:38:03 +00:00
let post = SsbMessageContent::Post {
text: "golgi go womp womp".to_string(),
mentions: None,
};
2022-02-15 08:26:51 +00:00
// Publish the post.
2021-12-29 16:38:03 +00:00
let post_msg_ref = sbot_client.publish(post).await?;
2022-02-15 08:26:51 +00:00
// Print the post reference to `stdout`.
2022-01-05 20:08:51 +00:00
println!("post_msg_ref: {}", post_msg_ref);
2021-12-29 16:38:03 +00:00
2022-02-15 08:26:51 +00:00
// Golgi also exposes convenience methods for some of the most common
// message types. Here we see an example of a convenience method for
// posting a description message. The description is for the local
// identity, ie. we are publishing this about "ourself".
2022-01-05 18:58:48 +00:00
let post_msg_ref = sbot_client
2022-02-08 13:04:44 +00:00
.publish_description("this is a description")
2022-01-05 18:58:48 +00:00
.await?;
2022-02-15 08:26:51 +00:00
// Print the description message reference to `stdout`.
2021-12-29 15:59:05 +00:00
println!("description: {}", post_msg_ref);
2022-01-05 20:08:51 +00:00
let author: String = id.clone();
println!("author: {:?}", author);
2022-02-15 08:26:51 +00:00
// Retrieve the description for the given public key (identity).
2022-01-05 20:08:51 +00:00
let description = sbot_client.get_description(&author).await?;
2022-02-15 08:26:51 +00:00
// Print the description to `stdout`.
2022-01-05 20:08:51 +00:00
println!("found description: {:?}", description);
2022-02-15 08:26:51 +00:00
// Compose and publish another `post` message type.
let post = SsbMessageContent::Post {
text: "golgi go womp womp2".to_string(),
mentions: None,
};
let post_msg_ref = sbot_client.publish(post).await?;
println!("post_msg_ref2: {}", post_msg_ref);
2021-12-02 17:48:16 +00:00
Ok(())
}
2022-02-15 08:26:51 +00:00
// Enable an async main function and execute the `run()` function,
// catching any errors and printing them to `stderr` before exiting the
// process.
2021-12-02 17:48:16 +00:00
#[async_std::main]
async fn main() {
if let Err(e) = run().await {
eprintln!("Application error: {}", e);
process::exit(1);
}
}