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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use telegram_client::{CanSendMessage, ChatId, DefaultApi, Message, ParseMode, ToChatRef};
use cxmr_broker::SharedBroker;
use super::{start_bot, Error};
#[derive(Clone, Deserialize, Serialize)]
pub struct Config {
pub token: String,
pub owner: i64,
}
#[derive(Clone)]
pub struct ChatClient {
pub api: DefaultApi,
pub owner: ChatId,
}
impl ChatClient {
pub fn new(cfg: Config) -> Result<Self, Error> {
let api = DefaultApi::new_default(cfg.token.clone())?;
let owner = ChatId::new(cfg.owner);
Ok(ChatClient { api, owner })
}
pub async fn start(self, broker: SharedBroker) -> Result<(), Error> {
Ok(start_bot(self, broker).await?)
}
pub async fn send<Chat: ToChatRef>(&self, chat: Chat, msg: String) -> Result<Message, Error> {
Ok(self
.api
.send(chat.text(msg).parse_mode(ParseMode::Markdown))
.await?)
}
pub async fn notify_owner(&self, msg: String) -> Result<Message, Error> {
Ok(self
.api
.send(self.owner.text(msg).parse_mode(ParseMode::Markdown))
.await?)
}
}