use serde_json::Value;
use crate::core::voiceflow::dialog_blocks::traits::FromValue;
use crate::errors::{VoiceflousionError, VoiceflousionResult};
#[derive(Debug, Clone)]
pub struct VoiceflowText {
message: String,
}
impl VoiceflowText {
pub fn new(message: String) -> Self {
Self { message }
}
pub(crate) fn error_default(error_text: &str) -> Self {
Self::new(error_text.to_string())
}
pub fn message(&self) -> &String {
&self.message
}
}
impl FromValue for VoiceflowText {
fn from_value(value: &Value) -> VoiceflousionResult<Option<Self>> {
let message = value["trace"]["payload"].get("message")
.and_then(|message| message.as_str())
.map(|s| s.trim_matches('"').to_string()) .ok_or_else(|| VoiceflousionError::VoiceflowBlockConvertationError(
"VoiceflowText text message".to_string(),
value.clone()
))?;
if message.is_empty() {
return Ok(None);
}
Ok(Some(Self::new(message)))
}
}