wx-bot-sdk 0.1.3

Standalone Weixin Bot SDK in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
pub mod config_cache;
pub mod session_guard;

pub use config_cache::{CachedConfig, WeixinConfigManager};
pub use session_guard::{SESSION_EXPIRED_ERRCODE, get_remaining_pause_ms, pause_session};

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::util::random_uint32_base64;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

const APP_ID: &str = "bot";
const PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
const DEFAULT_LONG_POLL_TIMEOUT_MS: u64 = 35_000;
const DEFAULT_API_TIMEOUT_MS: u64 = 15_000;
const DEFAULT_CONFIG_TIMEOUT_MS: u64 = 10_000;

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct BaseInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel_version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bot_agent: Option<String>,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum UploadMediaType {
    Image = 1,
    Video = 2,
    File = 3,
    Voice = 4,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum MessageType {
    None = 0,
    User = 1,
    Bot = 2,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum MessageItemType {
    None = 0,
    Text = 1,
    Image = 2,
    Voice = 3,
    File = 4,
    Video = 5,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum MessageState {
    New = 0,
    Generating = 1,
    Finish = 2,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum TypingStatus {
    Typing = 1,
    Cancel = 2,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct TextItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct CdnMedia {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypt_query_param: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aes_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypt_type: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_url: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ImageItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CdnMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_media: Option<CdnMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aeskey: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mid_size: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_size: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_height: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_width: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hd_size: Option<usize>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct VoiceItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CdnMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encode_type: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bits_per_sample: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_rate: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub playtime: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct FileItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CdnMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub md5: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub len: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct VideoItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CdnMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_size: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub play_length: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_md5: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_media: Option<CdnMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_size: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_height: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_width: Option<usize>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct RefMessage {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_item: Option<Box<MessageItem>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct MessageItem {
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub item_type: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub create_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_completed: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub msg_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ref_msg: Option<RefMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_item: Option<TextItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_item: Option<ImageItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_item: Option<VoiceItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_item: Option<FileItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_item: Option<VideoItem>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct WeixinMessage {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seq: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_user_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_user_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub create_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delete_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_type: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_state: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub item_list: Option<Vec<MessageItem>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_token: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GetUpdatesReq {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub get_updates_buf: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GetUpdatesResp {
    pub ret: Option<i32>,
    pub errcode: Option<i32>,
    pub errmsg: Option<String>,
    pub msgs: Option<Vec<WeixinMessage>>,
    pub get_updates_buf: Option<String>,
    pub longpolling_timeout_ms: Option<u64>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SendMessageReq {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub msg: Option<WeixinMessage>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GetUploadUrlReq {
    pub filekey: Option<String>,
    pub media_type: Option<i32>,
    pub to_user_id: Option<String>,
    pub rawsize: Option<usize>,
    pub rawfilemd5: Option<String>,
    pub filesize: Option<usize>,
    pub thumb_rawsize: Option<usize>,
    pub thumb_rawfilemd5: Option<String>,
    pub thumb_filesize: Option<usize>,
    pub no_need_thumb: Option<bool>,
    pub aeskey: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GetUploadUrlResp {
    pub upload_param: Option<String>,
    pub thumb_upload_param: Option<String>,
    pub upload_full_url: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SendTypingReq {
    pub ilink_user_id: Option<String>,
    pub typing_ticket: Option<String>,
    pub status: Option<i32>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SendTypingResp {
    pub ret: Option<i32>,
    pub errmsg: Option<String>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GetConfigResp {
    pub ret: Option<i32>,
    pub errmsg: Option<String>,
    pub typing_ticket: Option<String>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct NotifyStopResp {
    pub ret: Option<i32>,
    pub errmsg: Option<String>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct NotifyStartResp {
    pub ret: Option<i32>,
    pub errmsg: Option<String>,
}

#[derive(Clone, Debug)]
pub struct WeixinApiOptions {
    pub base_url: String,
    pub token: Option<String>,
    pub timeout_ms: Option<u64>,
    pub long_poll_timeout_ms: Option<u64>,
}

impl WeixinApiOptions {
    pub fn new(base_url: impl Into<String>, token: Option<String>) -> Self {
        Self {
            base_url: base_url.into(),
            token,
            timeout_ms: None,
            long_poll_timeout_ms: None,
        }
    }
}

fn encode_version(version: &str) -> u32 {
    let parts: Vec<u32> = version.split('.').map(|p| p.parse().unwrap_or(0)).collect();
    ((parts.first().copied().unwrap_or(0) & 0xff) << 16)
        | ((parts.get(1).copied().unwrap_or(0) & 0xff) << 8)
        | (parts.get(2).copied().unwrap_or(0) & 0xff)
}

pub fn build_base_info() -> BaseInfo {
    BaseInfo {
        channel_version: Some(PACKAGE_VERSION.to_string()),
        bot_agent: Some(format!("weixin-bot-sdk/{PACKAGE_VERSION}")),
    }
}

pub fn sanitize_bot_agent(raw: &str) -> String {
    let trimmed = raw.trim();
    let cleaned: String = trimmed
        .chars()
        .filter(|c| c.is_ascii() && !c.is_control())
        .take(256)
        .collect();
    if cleaned.is_empty() {
        format!("weixin-bot-sdk/{PACKAGE_VERSION}")
    } else {
        cleaned
    }
}

fn common_headers(
    req: reqwest::RequestBuilder,
    token: Option<&str>,
    json: bool,
) -> reqwest::RequestBuilder {
    let mut b = req.header("iLink-App-Id", APP_ID).header(
        "iLink-App-ClientVersion",
        encode_version(PACKAGE_VERSION).to_string(),
    );
    if json {
        b = b
            .header("Content-Type", "application/json")
            .header("AuthorizationType", "ilink_bot_token")
            .header("X-WECHAT-UIN", random_uint32_base64());
    }
    if let Some(t) = token.map(str::trim).filter(|t| !t.is_empty()) {
        b = b.header("Authorization", format!("Bearer {t}"));
    }
    b
}

fn endpoint_url(base_url: &str, endpoint: &str) -> Result<String> {
    let base = if base_url.ends_with('/') {
        base_url.to_string()
    } else {
        format!("{base_url}/")
    };
    Ok(url::Url::parse(&base)?.join(endpoint)?.to_string())
}

pub async fn api_post_fetch(
    base_url: &str,
    endpoint: &str,
    body: String,
    token: Option<&str>,
    timeout_ms: Option<u64>,
    label: &str,
) -> Result<String> {
    let client = reqwest::Client::new();
    let url = endpoint_url(base_url, endpoint)?;
    let res = common_headers(
        client.post(url).timeout(std::time::Duration::from_millis(
            timeout_ms.unwrap_or(DEFAULT_API_TIMEOUT_MS),
        )),
        token,
        true,
    )
    .body(body)
    .send()
    .await?;
    let status = res.status();
    let text = res.text().await?;
    if !status.is_success() {
        return Err(format!("{label} HTTP {status}: {text}").into());
    }
    Ok(text)
}

pub async fn api_get_fetch(
    base_url: &str,
    endpoint: &str,
    timeout_ms: Option<u64>,
    label: &str,
) -> Result<String> {
    let client = reqwest::Client::new();
    let url = endpoint_url(base_url, endpoint)?;
    let res = common_headers(
        client.get(url).timeout(std::time::Duration::from_millis(
            timeout_ms.unwrap_or(DEFAULT_API_TIMEOUT_MS),
        )),
        None,
        false,
    )
    .send()
    .await?;
    let status = res.status();
    let text = res.text().await?;
    if !status.is_success() {
        return Err(format!("{label} HTTP {status}: {text}").into());
    }
    Ok(text)
}

fn is_request_timeout(err: &(dyn std::error::Error + Send + Sync + 'static)) -> bool {
    err.downcast_ref::<reqwest::Error>()
        .map(|err| err.is_timeout())
        .unwrap_or(false)
}

pub async fn get_updates(params: GetUpdatesReq, opts: &WeixinApiOptions) -> Result<GetUpdatesResp> {
    let get_updates_buf = params.get_updates_buf.unwrap_or_default();
    let timeout = opts
        .long_poll_timeout_ms
        .or(opts.timeout_ms)
        .unwrap_or(DEFAULT_LONG_POLL_TIMEOUT_MS);
    let raw = match api_post_fetch(
        &opts.base_url,
        "ilink/bot/getupdates",
        json!({"get_updates_buf": get_updates_buf, "base_info": build_base_info()}).to_string(),
        opts.token.as_deref(),
        Some(timeout),
        "getUpdates",
    )
    .await
    {
        Ok(raw) => raw,
        Err(err) if is_request_timeout(err.as_ref()) => {
            return Ok(GetUpdatesResp {
                ret: Some(0),
                errcode: None,
                errmsg: None,
                msgs: Some(Vec::new()),
                get_updates_buf: Some(get_updates_buf),
                longpolling_timeout_ms: None,
            });
        }
        Err(err) => return Err(err),
    };
    Ok(serde_json::from_str(&raw)?)
}

pub async fn get_upload_url(
    params: GetUploadUrlReq,
    opts: &WeixinApiOptions,
) -> Result<GetUploadUrlResp> {
    let mut v = serde_json::to_value(params)?;
    v.as_object_mut()
        .unwrap()
        .insert("base_info".into(), serde_json::to_value(build_base_info())?);
    let raw = api_post_fetch(
        &opts.base_url,
        "ilink/bot/getuploadurl",
        v.to_string(),
        opts.token.as_deref(),
        opts.timeout_ms,
        "getUploadUrl",
    )
    .await?;
    Ok(serde_json::from_str(&raw)?)
}

pub async fn send_message(params: SendMessageReq, opts: &WeixinApiOptions) -> Result<()> {
    let mut v = serde_json::to_value(params)?;
    v.as_object_mut()
        .unwrap()
        .insert("base_info".into(), serde_json::to_value(build_base_info())?);
    api_post_fetch(
        &opts.base_url,
        "ilink/bot/sendmessage",
        v.to_string(),
        opts.token.as_deref(),
        opts.timeout_ms,
        "sendMessage",
    )
    .await?;
    Ok(())
}

pub async fn get_config(
    opts: &WeixinApiOptions,
    ilink_user_id: &str,
    context_token: Option<&str>,
) -> Result<GetConfigResp> {
    let raw = api_post_fetch(
        &opts.base_url,
        "ilink/bot/getconfig",
        json!({
            "ilink_user_id": ilink_user_id,
            "context_token": context_token,
            "base_info": build_base_info(),
        })
        .to_string(),
        opts.token.as_deref(),
        Some(DEFAULT_CONFIG_TIMEOUT_MS),
        "getConfig",
    )
    .await?;
    Ok(serde_json::from_str(&raw)?)
}

pub async fn send_typing(opts: &WeixinApiOptions, body: SendTypingReq) -> Result<SendTypingResp> {
    let mut v = serde_json::to_value(body)?;
    v.as_object_mut()
        .unwrap()
        .insert("base_info".into(), serde_json::to_value(build_base_info())?);
    let raw = api_post_fetch(
        &opts.base_url,
        "ilink/bot/sendtyping",
        v.to_string(),
        opts.token.as_deref(),
        Some(DEFAULT_CONFIG_TIMEOUT_MS),
        "sendTyping",
    )
    .await?;
    Ok(serde_json::from_str(&raw)?)
}

pub async fn notify_start(opts: &WeixinApiOptions) -> Result<NotifyStartResp> {
    let raw = api_post_fetch(
        &opts.base_url,
        "ilink/bot/msg/notifystart",
        json!({"base_info": build_base_info()}).to_string(),
        opts.token.as_deref(),
        Some(DEFAULT_CONFIG_TIMEOUT_MS),
        "notifyStart",
    )
    .await?;
    Ok(serde_json::from_str(&raw)?)
}

pub async fn notify_stop(opts: &WeixinApiOptions) -> Result<NotifyStopResp> {
    let raw = api_post_fetch(
        &opts.base_url,
        "ilink/bot/msg/notifystop",
        json!({"base_info": build_base_info()}).to_string(),
        opts.token.as_deref(),
        Some(DEFAULT_CONFIG_TIMEOUT_MS),
        "notifyStop",
    )
    .await?;
    Ok(serde_json::from_str(&raw)?)
}