11 个版本
0.1.91 | 2024年7月29日 |
---|---|
0.1.9 | 2024年1月18日 |
0.1.2 | 2023年10月3日 |
#9 在 #homepage
每月下载 133次
13KB
234 行
Slack API
use slack_api_client::{SlackClient, CreateMessage};
let client = SlackClient::new("<SLACK_BEARER_TOKEN>");
let response = CreateMessage::Text("Hello World".to_string())
.send_to_channel(&client, "<YOUR_CHANNEL_ID>".to_string())
.await?;
示例
创建块消息
use slack_api_client::CreateMessage;
pub fn hello_world() -> CreateMessage {
CreateMessage::Blocks(serde_json::json!([{
"type": "section",
"text": {
"type": "plain_text",
"text": "hello world",
"emoji": true
}
}]))
}
在Slack中打开模态框
pub struct SlackModal<'a> {
pub callback_id: &'a str,
}
impl<'a> SlackModal<'a> {
// https://api.slack.com/surfaces/modals#updating_views
pub async fn open(
&self,
client: &slack_api_client::SlackClient,
trigger_id: &str,
title: &str,
blocks: serde_json::Value,
) -> anyhow::Result<()> {
let response = client
.client
.post("https://slack.com/api/views.open")
.header("Content-Type", "application/json")
.body(
serde_json::json!({
"trigger_id": trigger_id,
"view": {
"type": "modal",
"callback_id": self.callback_id,
"title": {
"type": "plain_text",
"text": title
},
"blocks": blocks
}
})
.to_string(),
)
.send()
.await?;
let response: SlackResponse = response.json().await?;
response.is_ok()?;
Ok(())
}
}
#[derive(serde::Deserialize)]
pub struct SlackResponse {
ok: bool,
warning: Option<serde_json::Value>,
error: Option<serde_json::Value>,
response_metadata: Option<serde_json::Value>,
}
impl SlackResponse {
pub fn is_ok(&self) -> anyhow::Result<()> {
if self.ok {
return Ok(());
}
Err(anyhow::Error::msg(serde_json::json!({
"error": self.error,
"warning": self.warning,
"response_metadata": self.response_metadata,
})))
}
}
依赖项
~4–15MB
~207K SLoC