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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use futures_async_stream::for_await;
use telegram_client::{MessageKind, Update, UpdateKind};
use cxmr_broker::SharedBroker;
use super::{plugins, ChatClient, Error};
pub async fn start_bot(chat: ChatClient, broker: SharedBroker) -> Result<(), Error> {
chat.notify_owner("crypto-bank is starting; use /cmds to list commands".to_string())
.await?;
let mut stream = chat.api.clone().into_stream();
#[for_await]
for update in stream.updates() {
match update {
Ok(update) => {
if let Err(e) = handle_message(&chat, &broker, update).await {
error!("Telegram handle message error: {:?}", e);
}
}
Err(e) => {
error!("Telegram bot error: {:?}", e);
}
}
}
Ok(())
}
async fn handle_message(
chat: &ChatClient,
broker: &SharedBroker,
update: Update,
) -> Result<(), Error> {
if let UpdateKind::Message(message) = update.kind {
if message.chat.id() != chat.owner {
info!("Telegram unknown sender: {:?}", &message.from);
}
if let MessageKind::Text { ref data, .. } = message.kind {
info!("Telegram: <{}>: {}", &message.from.first_name, data);
let mut msg = data.split_whitespace();
match msg.next() {
Some("/cmds") | Some("/commands") | Some("/help") => {
plugins::commands(&chat, &message).await?;
}
Some("/bnc") => {
plugins::balances(&chat, &message, broker).await?;
}
Some("/btc") => {
plugins::btc(&chat, &message, broker).await?;
}
Some(other) => {
chat.send(
&message.chat,
format!(
"Hi, **{}**! Your command '{}' was not recognized. Try /cmds to list commands.",
&message.from.first_name, other
),
).await?;
}
None => {}
}
}
}
Ok(())
}