-
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.
Split monolithic code into several crates: core, telegram.
Remove http interface, for now.
- Loading branch information
Showing
23 changed files
with
512 additions
and
372 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "notifico-core" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
async-trait = "0.1.82" | ||
serde = { version = "1.0.210", features = ["derive"] } | ||
serde_json = "1.0.128" | ||
uuid = { version = "1.10.0", features = ["v4", "serde"] } | ||
reqwest = "0.12.7" | ||
url = "2.5.2" |
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,13 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Credential { | ||
pub r#type: String, | ||
pub name: String, | ||
pub value: Value, | ||
} | ||
|
||
pub trait Credentials: Send + Sync { | ||
fn get_credential(&self, r#type: &str, name: &str) -> Option<Value>; | ||
} |
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,38 @@ | ||
use crate::pipeline::SerializedStep; | ||
use crate::recipient::Recipient; | ||
use crate::templater::TemplaterError; | ||
use async_trait::async_trait; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{Map, Value}; | ||
use std::any::Any; | ||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Default, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct EventContext(pub Map<String, Value>); | ||
|
||
#[derive(Default, Debug)] | ||
pub struct PipelineContext { | ||
pub recipient: Option<Recipient>, | ||
pub event_context: EventContext, | ||
pub plugin_contexts: HashMap<Cow<'static, str>, Value>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum EngineError { | ||
TemplaterError(TemplaterError), | ||
PluginNotFound(SerializedStep), | ||
PipelineInterrupted, | ||
} | ||
|
||
#[async_trait] | ||
pub trait EnginePlugin: Send + Sync + Any { | ||
async fn execute_step( | ||
&self, | ||
context: &mut PipelineContext, | ||
step: &SerializedStep, | ||
) -> Result<(), EngineError>; | ||
|
||
fn step_type(&self) -> Cow<'static, str>; | ||
} |
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,5 @@ | ||
pub mod credentials; | ||
pub mod engine; | ||
pub mod pipeline; | ||
pub mod recipient; | ||
pub mod templater; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct Recipient { | ||
pub id: Uuid, | ||
pub contacts: Vec<Contact>, | ||
} | ||
|
||
impl Recipient { | ||
pub fn get_primary_contact(&self, r#type: &str) -> Option<&Contact> { | ||
for contact in &self.contacts { | ||
if contact.r#type() == r#type { | ||
return Some(&contact); | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct Contact(Value); | ||
|
||
impl Contact { | ||
pub fn r#type(&self) -> &str { | ||
self.0["type"] | ||
.as_str() | ||
.expect("Contact type must be a string") | ||
} | ||
|
||
pub fn into_json(self) -> Value { | ||
self.0 | ||
} | ||
} |
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,36 @@ | ||
use async_trait::async_trait; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{Map, Value}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug)] | ||
pub enum TemplaterError { | ||
RequestError(reqwest::Error), | ||
UrlError(url::ParseError), | ||
} | ||
|
||
impl From<reqwest::Error> for TemplaterError { | ||
fn from(err: reqwest::Error) -> Self { | ||
TemplaterError::RequestError(err) | ||
} | ||
} | ||
|
||
impl From<url::ParseError> for TemplaterError { | ||
fn from(err: url::ParseError) -> Self { | ||
TemplaterError::UrlError(err) | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct RenderResponse(pub Map<String, Value>); | ||
|
||
#[async_trait] | ||
pub trait Templater: Send + Sync { | ||
async fn render( | ||
&self, | ||
template_type: &str, | ||
template_id: Uuid, | ||
context: Map<String, Value>, | ||
) -> Result<RenderResponse, TemplaterError>; | ||
} |
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,14 @@ | ||
[package] | ||
name = "notifico-telegram" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
async-trait = "0.1.82" | ||
serde = { version = "1.0.210", features = ["derive"] } | ||
serde_json = "1.0.128" | ||
tracing = "0.1.40" | ||
uuid = { version = "1.10.0", features = ["v4"] } | ||
notifico-core = { path = "../notifico-core" } | ||
teloxide = "0.13.0" | ||
|
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,23 @@ | ||
use notifico_core::recipient::Contact; | ||
use serde::Deserialize; | ||
use teloxide::prelude::ChatId; | ||
use teloxide::types::Recipient; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct TelegramContact { | ||
chat_id: ChatId, | ||
} | ||
|
||
impl TelegramContact { | ||
pub(crate) fn into_recipient(self) -> Recipient { | ||
Recipient::Id(self.chat_id) | ||
} | ||
} | ||
|
||
impl TryFrom<Contact> for TelegramContact { | ||
type Error = (); | ||
|
||
fn try_from(value: Contact) -> Result<Self, Self::Error> { | ||
serde_json::from_value(value.into_json()).map_err(|_| ()) | ||
} | ||
} |
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,130 @@ | ||
use async_trait::async_trait; | ||
use contact::TelegramContact; | ||
use notifico_core::credentials::Credentials; | ||
use notifico_core::engine::{EngineError, EnginePlugin, PipelineContext}; | ||
use notifico_core::pipeline::SerializedStep; | ||
use notifico_core::templater::{RenderResponse, Templater}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
use std::borrow::Cow; | ||
use std::sync::Arc; | ||
use step::{CredentialSelector, TelegramStep}; | ||
use teloxide::prelude::Requester; | ||
use teloxide::Bot; | ||
use tracing::debug; | ||
use uuid::Uuid; | ||
|
||
mod contact; | ||
mod step; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct TelegramBotCredentials { | ||
token: String, | ||
} | ||
|
||
pub struct TelegramPlugin { | ||
templater: Arc<dyn Templater>, | ||
credentials: Arc<dyn Credentials>, | ||
} | ||
|
||
impl TelegramPlugin { | ||
pub fn new(templater: Arc<dyn Templater>, credentials: Arc<dyn Credentials>) -> Self { | ||
Self { | ||
templater, | ||
credentials, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Serialize, Deserialize)] | ||
struct TelegramContext { | ||
template_id: Option<Uuid>, | ||
} | ||
|
||
#[async_trait] | ||
impl EnginePlugin for TelegramPlugin { | ||
async fn execute_step( | ||
&self, | ||
context: &mut PipelineContext, | ||
step: &SerializedStep, | ||
) -> Result<(), EngineError> { | ||
let telegram_context = context | ||
.plugin_contexts | ||
.entry("telegram".into()) | ||
.or_insert(Value::Object(Default::default())); | ||
|
||
debug!("Plugin context: {:?}", telegram_context); | ||
|
||
let mut plugin_context: TelegramContext = | ||
serde_json::from_value(telegram_context.clone()).unwrap(); | ||
let telegram_step: TelegramStep = step.clone().try_into().unwrap(); | ||
|
||
match telegram_step { | ||
TelegramStep::LoadTemplate { template_id } => { | ||
plugin_context.template_id = Some(template_id); | ||
context.plugin_contexts.insert( | ||
"telegram".into(), | ||
serde_json::to_value(plugin_context).unwrap(), | ||
); | ||
} | ||
TelegramStep::Send(cred_selector) => { | ||
let Some(template_id) = plugin_context.template_id else { | ||
return Err(EngineError::PipelineInterrupted); | ||
}; | ||
|
||
let bot_token = match cred_selector { | ||
CredentialSelector::BotName { bot_name } => self | ||
.credentials | ||
.get_credential("telegram_token", &bot_name) | ||
.unwrap(), | ||
}; | ||
|
||
let tgcred: TelegramBotCredentials = serde_json::from_value(bot_token).unwrap(); | ||
|
||
let bot = Bot::new(tgcred.token); | ||
|
||
let rendered_template = self | ||
.templater | ||
.render("telegram", template_id, context.event_context.0.clone()) | ||
.await | ||
.unwrap(); | ||
|
||
let rendered_template: TelegramBody = rendered_template.try_into().unwrap(); | ||
|
||
let contact = TelegramContact::try_from( | ||
context | ||
.recipient | ||
.clone() | ||
.unwrap() | ||
.get_primary_contact("telegram") | ||
.ok_or(EngineError::PipelineInterrupted) | ||
.cloned()?, | ||
) | ||
.unwrap(); | ||
|
||
bot.send_message(contact.into_recipient(), rendered_template.body) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn step_type(&self) -> Cow<'static, str> { | ||
"telegram".into() | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Clone)] | ||
pub struct TelegramBody { | ||
pub body: String, | ||
} | ||
|
||
impl TryFrom<RenderResponse> for TelegramBody { | ||
type Error = (); | ||
|
||
fn try_from(value: RenderResponse) -> Result<Self, Self::Error> { | ||
serde_json::from_value(Value::from_iter(value.0)).map_err(|_| ()) | ||
} | ||
} |
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,30 @@ | ||
use notifico_core::pipeline::SerializedStep; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
pub enum CredentialSelector { | ||
BotName { bot_name: String }, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(tag = "step")] | ||
pub enum TelegramStep { | ||
#[serde(rename = "telegram.load_template")] | ||
LoadTemplate { template_id: Uuid }, | ||
// #[serde(rename = "telegram.set_recipients")] | ||
// SetRecipients { telegram_id: Vec<i64> }, | ||
#[serde(rename = "telegram.send")] | ||
Send(CredentialSelector), | ||
} | ||
|
||
impl TryFrom<SerializedStep> for TelegramStep { | ||
type Error = (); | ||
|
||
fn try_from(value: SerializedStep) -> Result<Self, Self::Error> { | ||
let s = serde_json::to_string(&value.into_value()).unwrap(); | ||
|
||
Ok(serde_json::from_str(&s).unwrap()) | ||
} | ||
} |
Oops, something went wrong.