dingtalk_stream/lib.rs
1//! DingTalk Stream SDK for Rust
2//!
3//! A Rust implementation of the DingTalk Stream SDK, based on the Node.js and Python SDKs.
4//!
5//! # Quick Start
6//!
7//! ```rust
8//! use dingtalk_stream::{Credential, DingTalkStream, CallbackHandler, MessageTopic, TOPIC_ROBOT};
9//! use dingtalk_stream::handlers::{Resp, Error, ErrorCode};
10//! use async_trait::async_trait;
11//! use dingtalk_stream::frames::{CallbackMessage, CallbackWebhookMessage};
12//! use tokio::sync::mpsc::Sender;
13//!
14//! // Define a handler for robot messages
15//! struct MyRobotHandler(MessageTopic);
16//!
17//! #[async_trait]
18//! impl CallbackHandler for MyRobotHandler {
19//! async fn process(&self, message: &CallbackMessage, cb_webhook_msg_sender: Option<Sender<CallbackWebhookMessage>>) -> Result<Resp, Error> {
20//! // Process the message and return a response
21//! Ok(Resp::Text("Hello from DingTalk SDK!".to_string()))
22//! }
23//!
24//! fn topic(&self) -> &MessageTopic {
25//! &self.0
26//! }
27//! }
28//!
29//! #[tokio::main]
30//! async fn main() -> Result<()> {
31//! let credential = Credential::new(
32//! "your-client-id".to_string(),
33//! "your-client-secret".to_string(),
34//! );
35//!
36//! let mut client = DingTalkStream::new(credential)
37//! .register_callback_handler(MyRobotHandler(
38//! MessageTopic::Callback(TOPIC_ROBOT.to_string()),
39//! ));
40//!
41//! client.start().await;
42//! Ok(())
43//! }
44//! ```
45
46pub mod client;
47pub mod credential;
48pub mod frames;
49pub mod handlers;
50pub mod utils;
51
52// Re-export for convenience
53pub use client::{ClientConfig, DingTalkStream};
54pub use credential::Credential;
55
56pub type Result<T, E = anyhow::Error> = anyhow::Result<T, E>;
57
58/// The DingTalk gateway URL for opening connections
59pub const GATEWAY_URL: &str = "https://api.dingtalk.com/v1.0/gateway/connections/open";
60
61/// The DingTalk API endpoint for getting access tokens
62pub const GET_TOKEN_URL: &str = "https://api.dingtalk.com/v1.0/oauth2/accessToken";
63
64pub const MESSAGE_FILES_DOWNLOAD_URL: &str =
65 "https://api.dingtalk.com/v1.0/robot/messageFiles/download";
66
67pub const MEDIA_UPLOAD_URL: &str = "https://oapi.dingtalk.com/media/upload";
68
69pub const ROBOT_SEND_PRIVATE_MESSAGE: &str =
70 "https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend";
71
72pub const ROBOT_SEND_GROUP_MESSAGE: &str = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
73
74/// The topic for robot message callbacks
75pub const TOPIC_ROBOT: &str = "/v1.0/im/bot/messages/get";
76
77/// The topic for robot delegate message callbacks
78pub const TOPIC_ROBOT_DELEGATE: &str = "/v1.0/im/bot/messages/delegate";
79
80/// The topic for card callback
81pub const TOPIC_CARD: &str = "/v1.0/card/instances/callback";
82
83/// SDK version
84pub const VERSION: &str = env!("CARGO_PKG_VERSION");