-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.rs
240 lines (212 loc) · 7.67 KB
/
main.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::borrow::Cow;
use std::env;
use std::error::Error;
use std::sync::Arc;
use dotenv::dotenv;
use indoc::formatdoc;
use lazy_static::lazy_static;
use reqwest::Url;
use teloxide::adaptors::DefaultParseMode;
use teloxide::types::InputFile;
use teloxide::types::ParseMode::Html;
use teloxide::{prelude::*, utils::command::BotCommand};
use solar_system_info_rpc::solar_system_info::solar_system_info_client::SolarSystemInfoClient;
use solar_system_info_rpc::solar_system_info::PlanetRequest;
use crate::conversion::PlanetWrapper;
use crate::error::SolarSystemInfoBotError;
mod conversion;
mod error;
lazy_static! {
static ref GRPC_SERVER_ADDRESS: String =
std::env::var("GRPC_SERVER_ADDRESS").expect("Can't read gRPC server address");
}
#[tokio::main]
async fn main() {
dotenv().ok();
teloxide::enable_logging!();
log::info!("Starting Solar System info bot");
let api_url = std::env::var("TELEGRAM_API_URL").expect("Can't get Telegram API URL");
let api_url = Url::parse(&api_url).expect("Can't parse Telegram API URL");
let bot = Bot::from_env()
.set_api_url(api_url)
.parse_mode(Html)
.auto_send();
// TODO: remove this line after the fix will be issued
let bot = Arc::new(bot);
let grpc_client = create_grpc_client().await;
teloxide::commands_repl(bot, "solar-system-info-bot", move |cx, command| {
answer(cx, command, grpc_client.clone())
})
.await;
}
type UpdateCtx = UpdateWithCx<Arc<AutoSend<DefaultParseMode<Bot>>>, Message>;
async fn answer(
ctx: UpdateCtx,
command: Command,
grpc_client: SolarSystemInfoClient<tonic::transport::Channel>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match command {
Command::Start => {
let username: String = match ctx.update.from() {
Some(user) => user.username.clone().unwrap_or_default(),
None => String::new(),
};
ctx.answer(get_start_message(&username)).await?;
ctx.answer(Command::descriptions()).await?;
}
Command::Help => {
ctx.answer(Command::descriptions()).await?;
}
Command::List => {
let response = get_planets_list(grpc_client).await?;
ctx.answer(response).await?;
}
Command::Planets => {
get_planets(grpc_client, ctx).await?;
}
Command::Planet(planet_name) => {
let response = get_planet(grpc_client, String::from(planet_name.trim())).await?;
ctx.answer_photo(response.0).caption(response.1).await?;
}
};
Ok(())
}
fn get_start_message(username: &str) -> String {
formatdoc!(
"Hi {username}, this bot can show basic information about planets in the Solar System",
username = &username
)
}
async fn get_planets_list(
mut grpc_client: SolarSystemInfoClient<tonic::transport::Channel>,
) -> Result<String, SolarSystemInfoBotError> {
let response = grpc_client.get_planets_list(tonic::Request::new(())).await;
return match response {
Ok(response) => {
let message = response
.into_inner()
.list
.into_iter()
.map(|planet_name| {
format!(
"{} (use <code>/planet {}</code>)",
planet_name,
planet_name.to_lowercase()
)
})
.collect::<Vec<String>>()
.join("\n");
Ok(message)
}
Err(e) => {
log::error!("There was an error while handling list of planets: {}", e);
Err(SolarSystemInfoBotError::new(
"Internal error while handling list of planets",
))
}
};
}
async fn get_planets(
mut grpc_client: SolarSystemInfoClient<tonic::transport::Channel>,
ctx: UpdateCtx,
) -> Result<(), SolarSystemInfoBotError> {
let response = grpc_client.get_planets(tonic::Request::new(())).await;
match response {
Ok(response) => {
let mut planets_stream = response.into_inner();
while let Some(response) = planets_stream.message().await.expect("Can't get a planet") {
match response.planet {
Some(planet) => {
let message = PlanetWrapper(&planet).to_string();
match ctx.answer(message).await {
Ok(_) => {}
Err(e) => {
log::error!("Something went wrong while handling planets: {}", e);
}
};
}
None => {
log::error!("Something went wrong while handling planets");
return Err(SolarSystemInfoBotError::new(
"Something went wrong while handling planets",
));
}
}
}
}
Err(e) => {
log::error!("There was an error while handling planets: {}", e);
return Err(SolarSystemInfoBotError::new(
"Internal error while handling planets",
));
}
};
Ok(())
}
async fn get_planet(
mut grpc_client: SolarSystemInfoClient<tonic::transport::Channel>,
planet_name: String,
) -> Result<(InputFile, String), SolarSystemInfoBotError> {
let request = tonic::Request::new(PlanetRequest {
name: planet_name.to_string(),
});
let response = grpc_client.get_planet(request).await;
return match response {
Ok(response) => match response.into_inner().planet {
Some(planet) => {
let pw = PlanetWrapper(&planet);
let file = InputFile::Memory {
file_name: format!("{}.png", &planet_name),
data: Cow::from(planet.image.clone()),
};
Ok((file, pw.to_string()))
}
None => {
log::error!(
"Something went wrong while handling a planet {}",
&planet_name
);
Err(SolarSystemInfoBotError::new(
format!(
"Something went wrong while handling a planet {}",
planet_name
)
.as_str(),
))
}
},
Err(status) => {
log::error!("There was an error while handling a planet: {}", status);
match status.code() {
tonic::Code::NotFound => Err(SolarSystemInfoBotError::new(
format!("Planet with name {} not found", planet_name).as_str(),
)),
_ => Err(SolarSystemInfoBotError::new(
format!("Internal error while handling a planet {}", planet_name).as_str(),
)),
}
}
};
}
#[derive(BotCommand)]
#[command(rename = "lowercase", description = "These commands are supported:")]
enum Command {
Start,
#[command(description = "show this message")]
Help,
#[command(description = "show list of planets")]
List,
#[command(description = "show info about all planets")]
Planets,
#[command(
description = "show info about specified planet (for example, enter <code>/planet neptune</code>)"
)]
Planet(String),
}
async fn create_grpc_client() -> SolarSystemInfoClient<tonic::transport::Channel> {
let channel = tonic::transport::Channel::from_static(&GRPC_SERVER_ADDRESS)
.connect()
.await
.expect("Can't create a channel");
SolarSystemInfoClient::new(channel)
}