Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwin-Stockinger committed Apr 27, 2023
0 parents commit 1a17462
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
Empty file added .cargo/config.toml
Empty file.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "slack-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11.16", features = ["json"] }
serde = { version = "1.0.160", features = ["derive"] }
thiserror = "1.0.40"
48 changes: 48 additions & 0 deletions src/blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlackMessage {
pub text: String,
pub blocks: Vec<Block>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
#[serde(rename = "type")]
pub type_field: String,
pub text: Option<Text>,
#[serde(rename = "block_id")]
pub block_id: Option<String>,
pub accessory: Option<Accessory>,
#[serde(default)]
pub fields: Vec<Field>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Text {
#[serde(rename = "type")]
pub type_field: String,
pub text: String,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Accessory {
#[serde(rename = "type")]
pub type_field: String,
#[serde(rename = "image_url")]
pub image_url: String,
#[serde(rename = "alt_text")]
pub alt_text: String,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Field {
#[serde(rename = "type")]
pub type_field: String,
pub text: String,
}
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use blocks::SlackMessage;

mod blocks;

pub struct Client {
reqwest_client: reqwest::Client,
webhook_url: String,
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
}

impl Client {
pub async fn new(webhook_url: String) -> Self {
let reqwest_client = reqwest::Client::new();

Self {
reqwest_client,
webhook_url,
}
}

pub async fn post_message(&self, msg: &SlackMessage) -> Result<(), Error> {
self.reqwest_client
.post(&self.webhook_url)
.json(msg)
.send()
.await?;

Ok(())
}
}

0 comments on commit 1a17462

Please sign in to comment.