use std::ops::Deref;
use async_trait::async_trait;
use crate::core::base_structs::ClientBase;
use crate::core::ClientBuilder;
use crate::core::session_wrappers::LockedSession;
use crate::core::traits::{Client, Sender};
use crate::core::voiceflow::VoiceflowBlock;
use crate::core::voiceflow::dialog_blocks::VoiceflowCarousel;
use crate::errors::{VoiceflousionError, VoiceflousionResult};
use crate::integrations::telegram::{TelegramResponder, TelegramSender, TelegramUpdate};
pub struct TelegramClient {
client_base: ClientBase<TelegramSender>,
}
impl TelegramClient {
pub fn new(builder: ClientBuilder) -> Self {
let api_key = builder.api_key().clone();
let max_connections_per_moment = builder.max_connections_per_moment();
let connection_duration = builder.connection_duration();
let sender = TelegramSender::new(max_connections_per_moment, api_key, connection_duration);
Self {
client_base: ClientBase::new(builder, sender),
}
}
async fn switch_carousel_card(&self, locked_session: &LockedSession<'_>, carousel: &VoiceflowCarousel, message_id: &String, direction: bool, interaction_time: i64) -> VoiceflousionResult<TelegramResponder> {
locked_session.set_last_interaction(Some(interaction_time));
self.client_base.sender().update_carousel(carousel, direction, locked_session.get_chat_id(), message_id).await
}
}
#[async_trait]
impl Client for TelegramClient {
type ClientUpdate<'async_trait> = TelegramUpdate;
type ClientSender<'async_trait> = TelegramSender;
fn client_base(&self) -> &ClientBase<Self::ClientSender<'_>> {
&self.client_base
}
async fn handle_carousel_switch(&self, locked_session: &LockedSession<'_>, interaction_time: i64, switch_direction: bool) -> VoiceflousionResult<Vec<<Self::ClientSender<'_> as Sender>::SenderResponder>> {
let binding = locked_session.previous_message().await;
let previous_message = binding.deref().as_ref().ok_or_else(|| {
VoiceflousionError::ClientRequestError("TelegramClient".to_string(), "Carousel cannot be switched at the start of the conversation".to_string(), )
})?;
if let VoiceflowBlock::Carousel(carousel) = previous_message.block() {
Ok(vec![self.switch_carousel_card(locked_session, carousel, previous_message.id(), switch_direction, interaction_time).await?])
} else {
Err(VoiceflousionError::ValidationError(
"TelegramClient".to_string(),
"There is no carousel to switch".to_string(),
))
}
}
}