feat: summarize rss and send to feishu

This commit is contained in:
ECSS 11
2026-01-13 00:44:52 -06:00
parent ed6f65c43f
commit d61262215e
18 changed files with 2654 additions and 7 deletions

36
src/api/client.rs Normal file
View File

@@ -0,0 +1,36 @@
pub async fn get(url: &String) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let response = client.get(url).send().await?;
let body = response.text().await?;
Ok(body)
}
pub async fn request_model(
prompt: &String,
token: &String,
) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let body = serde_json::json!({
"model": "Qwen/Qwen3-8B",
"messages": [{
"role": "user",
"content": prompt
}],
"max_tokens": 8192
});
let response = client
.post("https://api.siliconflow.cn/v1/messages")
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.json(&body)
.send()
.await?;
let body = response.text().await?;
Ok(body)
}

1
src/api/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub(crate) mod client;

38
src/config.rs Normal file
View File

@@ -0,0 +1,38 @@
use std::fs;
#[derive(Debug, serde::Deserialize)]
pub struct Config {
pub subscribes: Vec<SubscribeConfig>,
pub feishu: FeishuConfig,
pub token: String,
}
#[derive(Debug, serde::Deserialize, Clone)]
pub struct SubscribeConfig {
pub name: String,
pub url: String,
pub prompt: String,
}
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct FeishuConfig {
pub app_id: String,
pub app_secret: String,
pub chat_id: String,
}
impl Config {
pub fn build() -> Result<Self, Box<dyn std::error::Error>> {
let yaml_config = fs::read_to_string("./assets/config.yaml")?;
let mut config: Config = serde_yaml::from_str(&yaml_config)?;
// Load env params to yaml.
config.token = std::env::var("TOKEN")?;
config.feishu.app_id = std::env::var("APP_ID")?;
config.feishu.app_secret = std::env::var("APP_SECRET")?;
config.feishu.chat_id = std::env::var("CHAT_ID")?;
Ok(config)
}
}

43
src/controls/feishu.rs Normal file
View File

@@ -0,0 +1,43 @@
use crate::{config, controls, models};
pub struct FeishuClient {
pub client: open_lark::prelude::LarkClient,
}
impl FeishuClient {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let config = config::Config::build()?;
let client = open_lark::prelude::LarkClient::builder(
config.feishu.app_id.as_str(),
config.feishu.app_secret.as_str(),
)
.build();
Ok(Self { client })
}
pub async fn send_message(&self, message: &String) -> Result<(), Box<dyn std::error::Error>> {
let config = config::Config::build()?;
let content = models::feishu::MessageContent {
text: message.to_string(),
};
let content = serde_json::to_string(&content)?;
let message = open_lark::prelude::CreateMessageRequestBody::builder()
.receive_id(config.feishu.chat_id)
.msg_type("text")
.content(content)
.build();
let request = open_lark::prelude::CreateMessageRequest::builder()
.receive_id_type("chat_id")
.request_body(message)
.build();
self.client.im.v1.message.create(request, None).await?;
Ok(())
}
}

2
src/controls/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod summary;
pub mod feishu;

28
src/controls/summary.rs Normal file
View File

@@ -0,0 +1,28 @@
use crate::api;
use crate::config;
use crate::models;
use std::error::Error;
pub async fn summarize_rss() -> Result<Vec<models::summary::Summary>, Box<dyn Error>> {
let config = config::Config::build()?;
let mut summary_list: Vec<models::summary::Summary> = Vec::new();
let token = &config.token;
let subscribes = &config.subscribes;
for subscribe in subscribes {
// Get rss message and prompt.
let rss_message = api::client::get(&subscribe.url).await?;
let prompt = format!("{} \n {}", &subscribe.prompt, rss_message);
// Get response from remote AI model.
let model_response = api::client::request_model(&prompt, token).await?;
let parse_model_response = models::silicon_cloud::ModelResponse::build(model_response)?;
let message = &parse_model_response.content.first().unwrap().text;
// Save summary result in list.
let summary = models::summary::Summary::new(subscribe.name.clone(), message.to_string());
summary_list.push(summary);
}
Ok(summary_list)
}

View File

@@ -1,3 +1,17 @@
fn main() {
println!("Hello, world!");
mod api;
mod config;
mod models;
mod controls;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let summary_list = controls::summary::summarize_rss().await?;
let app = controls::feishu::FeishuClient::new()?;
for summary in summary_list {
app.send_message(&summary.name).await?;
app.send_message(&summary.description).await?;
}
Ok(())
}

4
src/models/feishu.rs Normal file
View File

@@ -0,0 +1,4 @@
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct MessageContent {
pub text: String,
}

3
src/models/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod silicon_cloud;
pub mod summary;
pub mod feishu;

View File

@@ -0,0 +1,18 @@
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Content {
pub text: String,
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct ModelResponse {
pub id: String,
pub content: Vec<Content>,
pub model: String
}
impl ModelResponse {
pub fn build(response: String) -> Result<Self, Box<dyn std::error::Error>> {
let prase_response: ModelResponse = serde_json::from_str(&response)?;
Ok(prase_response)
}
}

13
src/models/summary.rs Normal file
View File

@@ -0,0 +1,13 @@
pub struct Summary {
pub name: String,
pub description: String,
}
impl Summary {
pub fn new(name: String, description: String) -> Self {
Self {
name,
description,
}
}
}