use std::ops::Deref;
use async_trait::async_trait;
use reqwest::Response;
use serde::Deserialize;
use crate::core::base_structs::ResponderBase;
use crate::core::traits::Responder;
use crate::core::voiceflow::VoiceflowBlock;
use crate::errors::{VoiceflousionError, VoiceflousionResult};
#[derive(Debug)]
pub struct TelegramResponder {
bot_id: String,
responder_base: ResponderBase
}
#[derive(Debug, Deserialize)]
struct Chat {
id: u64,
}
#[derive(Debug, Deserialize)]
struct From {
id: u64,
}
#[derive(Debug, Deserialize)]
struct TelegramResult {
chat: Chat,
date: i64,
from: From,
message_id: u64,
}
#[derive(Debug, Deserialize)]
struct ResponseBody {
result: TelegramResult,
}
impl TelegramResponder{
pub fn bot_id(&self) -> &String{
&self.bot_id
}
}
impl Deref for TelegramResponder {
type Target = ResponderBase;
fn deref(&self) -> &Self::Target {
&self.responder_base
}
}
#[async_trait]
impl Responder for TelegramResponder {
async fn from_response(response: Response, content: VoiceflowBlock) -> VoiceflousionResult<Self> {
let body = response.json::<ResponseBody>().await
.map_err(|e| VoiceflousionError::ClientResponseReadingError("TelegramResponder".to_string(), e.to_string()))?;
let result = body.result;
Ok(Self {
bot_id: result.from.id.to_string(),
responder_base: ResponderBase::new(result.chat.id.to_string(), result.message_id.to_string(), content, result.date)
})
}
}