refactor: pass args to send_comic in a struct
This commit is contained in:
parent
2c9baa16df
commit
3790fab0bc
2 changed files with 28 additions and 9 deletions
29
src/main.rs
29
src/main.rs
|
@ -117,15 +117,18 @@ async fn handler(cfg: &Config, s: &mut State) -> Result<(), reqwest::Error> {
|
||||||
match args.next()? {
|
match args.next()? {
|
||||||
"ru" => {
|
"ru" => {
|
||||||
let comic_id = args.next()?.parse::<u16>().ok()?;
|
let comic_id = args.next()?.parse::<u16>().ok()?;
|
||||||
Some((msg, format!("https://xkcd.ru/{}/?json=true", comic_id)))
|
let url = format!("https://xkcd.ru/{}/?json=true", comic_id);
|
||||||
|
Some(SenderCtx { msg, url, comic_id })
|
||||||
}
|
}
|
||||||
"en" => {
|
"en" => {
|
||||||
let comic_id = args.next()?.parse::<u16>().ok()?;
|
let comic_id = args.next()?.parse::<u16>().ok()?;
|
||||||
Some((msg, format!("https://xkcd.com/{}/info.0.json", comic_id)))
|
let url = format!("https://xkcd.com/{}/info.0.json", comic_id);
|
||||||
|
Some(SenderCtx { msg, url, comic_id })
|
||||||
}
|
}
|
||||||
comic_id => {
|
comic_id => {
|
||||||
let comic_id = comic_id.parse::<u16>().ok()?;
|
let comic_id = comic_id.parse::<u16>().ok()?;
|
||||||
Some((msg, format!("https://xkcd.com/{}/info.0.json", comic_id)))
|
let url = format!("https://xkcd.com/{}/info.0.json", comic_id);
|
||||||
|
Some(SenderCtx { msg, url, comic_id })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -134,8 +137,8 @@ async fn handler(cfg: &Config, s: &mut State) -> Result<(), reqwest::Error> {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.for_each(|(msg, url)| {
|
.for_each(|ctx| {
|
||||||
tokio::spawn(send_comic(cfg.clone(), msg, url));
|
tokio::spawn(send_comic(cfg.clone(), ctx));
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(u) = updates.last() {
|
if let Some(u) = updates.last() {
|
||||||
|
@ -145,16 +148,24 @@ async fn handler(cfg: &Config, s: &mut State) -> Result<(), reqwest::Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_comic(cfg: Config, msg: TgMessage, url: String) -> Result<(), reqwest::Error> {
|
async fn send_comic(cfg: Config, ctx: SenderCtx) -> Result<(), reqwest::Error> {
|
||||||
let info = cfg.client.get(url).send().await?.json::<XkcdInfo>().await?;
|
let info = cfg
|
||||||
|
.client
|
||||||
|
.get(ctx.url)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.error_for_status()?
|
||||||
|
.json::<XkcdInfo>()
|
||||||
|
.await?;
|
||||||
|
|
||||||
cfg.client
|
cfg.client
|
||||||
.post((*cfg.send_url).clone())
|
.post((*cfg.send_url).clone())
|
||||||
.form(&[
|
.form(&[
|
||||||
("chat_id", msg.chat.id.to_string()),
|
("chat_id", ctx.msg.chat.id.to_string()),
|
||||||
(
|
(
|
||||||
"message_thread_id",
|
"message_thread_id",
|
||||||
msg.thread
|
ctx.msg
|
||||||
|
.thread
|
||||||
.map(|t| t.to_string())
|
.map(|t| t.to_string())
|
||||||
.unwrap_or("null".to_owned()),
|
.unwrap_or("null".to_owned()),
|
||||||
),
|
),
|
||||||
|
|
|
@ -17,6 +17,14 @@ pub struct Config {
|
||||||
pub send_url: Arc<Url>,
|
pub send_url: Arc<Url>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Object holding a context for fn send_comic
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SenderCtx {
|
||||||
|
pub msg: TgMessage,
|
||||||
|
pub url: String,
|
||||||
|
pub comic_id: u16,
|
||||||
|
}
|
||||||
|
|
||||||
/// Telegram Bot API response:
|
/// Telegram Bot API response:
|
||||||
/// these idiots use `{ok:true,response:...}` schema
|
/// these idiots use `{ok:true,response:...}` schema
|
||||||
/// for some reason
|
/// for some reason
|
||||||
|
|
Loading…
Add table
Reference in a new issue