rsiot/components/cmp_telegram/tasks/
input.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::{
    executor::CmpInOut,
    message::{Message, MsgDataBound, ServiceBound},
};

use super::TelegramBot;

pub struct Input<TMsg, TService>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    pub input: CmpInOut<TMsg, TService>,
    pub bot: TelegramBot,
    pub fn_input: fn(Message<TMsg>) -> Option<String>,
}

impl<TMsg, TService> Input<TMsg, TService>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    pub async fn spawn(mut self) -> super::Result<()> {
        while let Ok(msg) = self.input.recv_input().await {
            let msg = (self.fn_input)(msg);
            let Some(msg) = msg else { continue };
            self.bot.send_message(&msg).await;
        }

        Err(super::Error::TaskEndInput)
    }
}