-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
9 changed files
with
138 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
This file was deleted.
Oops, something went wrong.
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 = "notifico-slack" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
notifico-core = { path = "../../notifico-core" } | ||
reqwest = "0.12.9" | ||
serde = "1.0.215" | ||
async-trait = "0.1.83" | ||
serde_json = "1.0.133" |
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,109 @@ | ||
mod step; | ||
|
||
use crate::step::{Step, STEPS}; | ||
use async_trait::async_trait; | ||
use notifico_core::credentials::{CredentialStorage, TypedCredential}; | ||
use notifico_core::engine::{EnginePlugin, PipelineContext, StepOutput}; | ||
use notifico_core::error::EngineError; | ||
use notifico_core::recipient::TypedContact; | ||
use notifico_core::step::SerializedStep; | ||
use notifico_core::templater::RenderedTemplate; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use std::borrow::Cow; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct SlackCredentials { | ||
token: String, | ||
} | ||
|
||
impl TypedCredential for SlackCredentials { | ||
const CREDENTIAL_TYPE: &'static str = "slack"; | ||
} | ||
|
||
pub struct SlackPlugin { | ||
client: reqwest::Client, | ||
credentials: Arc<dyn CredentialStorage>, | ||
} | ||
|
||
impl SlackPlugin { | ||
pub fn new(credentials: Arc<dyn CredentialStorage>) -> Self { | ||
SlackPlugin { | ||
client: reqwest::Client::new(), | ||
credentials, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl EnginePlugin for SlackPlugin { | ||
async fn execute_step( | ||
&self, | ||
context: &mut PipelineContext, | ||
step: &SerializedStep, | ||
) -> Result<StepOutput, EngineError> { | ||
let step: Step = step.clone().convert_step()?; | ||
|
||
match step { | ||
Step::Send { credential } => { | ||
let Some(recipient) = context.recipient.clone() else { | ||
return Err(EngineError::RecipientNotSet); | ||
}; | ||
|
||
let credential: SlackCredentials = self | ||
.credentials | ||
.get_typed_credential(context.project_id, &credential) | ||
.await?; | ||
|
||
let contact: SlackContact = recipient.get_primary_contact()?; | ||
|
||
for message in context.messages.iter().cloned() { | ||
let content: SlackMessage = message.try_into()?; | ||
|
||
let payload = json!({ | ||
"channel": contact.channel_id, | ||
"text": content.text, | ||
}); | ||
|
||
self.client | ||
.post("https://slack.com/api/chat.postMessage") | ||
.header("Authorization", format!("Bearer {}", credential.token)) | ||
.json(&payload) | ||
.send() | ||
.await | ||
.unwrap(); | ||
} | ||
Ok(StepOutput::Continue) | ||
} | ||
} | ||
} | ||
|
||
fn steps(&self) -> Vec<Cow<'static, str>> { | ||
STEPS.iter().map(|&s| s.into()).collect() | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct SlackContact { | ||
channel_id: String, | ||
} | ||
|
||
impl TypedContact for SlackContact { | ||
const CONTACT_TYPE: &'static str = "slack"; | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct SlackMessage { | ||
pub text: String, | ||
} | ||
|
||
impl TryFrom<RenderedTemplate> for SlackMessage { | ||
type Error = EngineError; | ||
|
||
fn try_from(value: RenderedTemplate) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
text: value.get("text")?.to_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,10 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(tag = "step")] | ||
pub enum Step { | ||
#[serde(rename = "slack.send")] | ||
Send { credential: String }, | ||
} | ||
|
||
pub(crate) const STEPS: &[&str] = &["slack.send"]; |