use crate::channel::{MessageEvent, OutboundMessage, PlatformAdapter};
use anyhow::Result;
use async_trait::async_trait;
use tokio::sync::mpsc;
pub struct HttpPlatformAdapter {
inbound_rx: mpsc::Receiver<MessageEvent>,
outbound_tx: mpsc::Sender<OutboundMessage>,
}
pub struct HttpAdapterHandle {
pub inbound_tx: mpsc::Sender<MessageEvent>,
pub outbound_rx: mpsc::Receiver<OutboundMessage>,
}
impl HttpPlatformAdapter {
pub fn new() -> (Self, HttpAdapterHandle) {
let (inbound_tx, inbound_rx) = mpsc::channel(32);
let (outbound_tx, outbound_rx) = mpsc::channel(32);
(
Self { inbound_rx, outbound_tx },
HttpAdapterHandle { inbound_tx, outbound_rx },
)
}
}
#[async_trait]
impl PlatformAdapter for HttpPlatformAdapter {
fn platform_name(&self) -> &str {
"http"
}
async fn connect(&mut self) -> Result<()> {
Ok(())
}
async fn disconnect(&mut self) -> Result<()> {
Ok(())
}
async fn send(&self, msg: &OutboundMessage) -> Result<Option<String>> {
let id = uuid::Uuid::new_v4().to_string();
self.outbound_tx.send(msg.clone()).await?;
Ok(Some(id))
}
async fn send_typing(&self, _chat_id: &str) -> Result<()> {
Ok(())
}
async fn edit_message(&self, _chat_id: &str, _message_id: &str, _text: &str) -> Result<()> {
Ok(())
}
fn interrupt_session(&self, _session_key: &str) {}
async fn recv(&mut self) -> Result<MessageEvent> {
self.inbound_rx
.recv()
.await
.ok_or_else(|| anyhow::anyhow!("channel closed"))
}
}