-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1a17462
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |