1mod model;
2
3use std::sync::Arc;
4use kovi::log::{debug, info};
5use kovi::{serde_json, toml, Message, PluginBuilder as plugin, PluginBuilder, RuntimeBot};
6use kovi::toml::{toml};
7use kovi::utils::load_toml_data;
8use crate::model::{Config, News, Response};
9
10#[kovi::plugin]
11async fn main() {
12 let bot = PluginBuilder::get_runtime_bot();
13 read_config(bot.clone());
14 plugin::on_msg(move |event| {
15 let bot = bot.clone();
16 async move {
17 let text = event.borrow_text().unwrap_or("");
18 if text.starts_with("/60s") {
19 let news = get_60s(bot).await;
20 let msg = Message::new()
21 .add_image(news.image.as_str());
22 event.reply(msg);
23 } else if text.starts_with("/help") {
24 let msg = Message::new()
33 .add_text("- /60s");
34 event.reply(msg);
35 }
36 }
37 });
38}
39
40fn read_config(bot: Arc<RuntimeBot>) -> Config {
41 let data_path = bot.get_data_path();
42 let config_toml_path = data_path.join("config.toml");
43 let default_config = toml! {
44 url = "http://127.0.0.1:4399"
45 };
46 let config = load_toml_data(default_config, config_toml_path).unwrap();
47 debug!("{}", config.to_string());
48 let config: Config = toml::from_str(&config.to_string()).unwrap();
49 config
50}
51
52async fn get_60s(bot: Arc<RuntimeBot>) -> News {
53 let client = reqwest::Client::new();
54 let config = read_config(bot);
55 let res = client.get(format!("{}/v2/60s", config.url)).send().await.unwrap();
56 let json = res.text().await.unwrap();
57 debug!("get_60s_response = {:?}", json);
58 let response: Response<News> = serde_json::from_str(json.as_str()).unwrap();
59 response.data
60}
61
62