Use the Rust libraries
Use these crates to talk to WeChat from your own Rust program, without the daemon or an official WeChat executable. To call a running daemon, use the HTTP API instead. Each crate’s full API is on docs.rs.
| Crate | Use it for | API |
|---|---|---|
wechat-protocol | Login, transport, sync and message codecs | docs.rs |
wechat-profile | Reading and checked writing of official profiles | docs.rs |
wechat-calls | Call session and media building blocks, PCM mixing and optional Opus | docs.rs |
wechat-media | Image inspection, decoding and sticker preparation | docs.rs |
wechat-platform | Private files and OS support | docs.rs |
Add dependencies
Use the same version of every crate. Find the current one on
crates.io and replace VERSION:
cargo add wechat-protocol@=VERSION wechat-profile@=VERSIONYou need Rust 1.93.1 or later, a C compiler, Make and Perl. Native Opus also needs
CMake and a C++ compiler. In wechat-protocol, the voice-silk and
openssl-crypto features are on by default. To skip the C SILK codec or the
vendored OpenSSL, turn off default features and enable pure-rust-crypto (or
openssl-crypto). Add voice-silk if you need silk::encode or silk::decode.
Read a stopped profile
Stop the official app that writes the profile, or give the reader a consistent copy.
Then load the protected keys file. The stopped_or_consistent_snapshot flag records
your promise; it does not stop another process.
use wechat_profile::{KeyFile, ListQuery, ProfileLocation, ProfileReader, ReadOptions};
fn contacts(profile: &str, keys_path: &str) -> wechat_profile::Result<()> { let reader = ProfileReader::open( ProfileLocation::new(profile)?, KeyFile::load(keys_path)?, ReadOptions { stopped_or_consistent_snapshot: true, ..Default::default() }, )?; println!("{} contacts", reader.contacts(&ListQuery::default())?.items.len()); Ok(())}contacts lists current rows. Use reader.contact(username) for one exact stored
row, including deleted contacts and groups that are still kept. Check the flags on a
row before you change a contact or group. Being stored does not make it eligible.
Send one text message
Protocol operations are methods on NativeClient. Your application stores
credentials, handles recovery and writes back to the profile.
-
Create a client with
NativeClient::with_transport(options, ProtocolTransport::native()?). Callstart_qr_login_async, thenpoll_login_asyncuntil the login is approved, thencomplete_login_async. Pass the returned account state toNativeClient::from_account_with_transport. Cloned clients share one account owner. -
Before you submit, record the operation, a unique task ID and the Unix time in seconds. Use the recipient’s exact WeChat username:
use wechat_protocol::{wire, NativeClient, TextSendOptions, TextSendResult};async fn send_once(client: &NativeClient, recipient: &str, text: &str,task_id: &str, created_at: u32,) -> anyhow::Result<TextSendResult> {let client_id = wire::client_message_id(task_id.as_bytes());anyhow::ensure!(client_id != 0, "Choose another task ID");client.send_text(recipient, text, TextSendOptions {created_at, client_message_id: client_id, source: None,}).await} -
Check
accepted,returned_destination_matchesandreturned_client_id_matchesin the result.server_message_idis the server’s receipt. It does not show that the recipient received or read the message, or that it reached your profile’s history.
From a checkout, an example program shows a login QR code and sends once:
cargo run --manifest-path packages/native-client-rust/Cargo.toml -p wechat-protocol \ --example send_text -- RECIPIENT UNIQUE_TASK_ID "Hello from Rust"A successful send prints "accepted": true. Run it with --help to see usage
without making network requests. The qr_login, sync_once and protobuf examples
show login, one sync page and local codecs. sync_once does not save a cursor. A
receiver that keeps running must store each command before it moves the cursor on.
Send images and videos
Upload the copies first, then pass the result to the send method.
NativeClient::send_image(destination, client_id, &UploadedImage) and
send_video(destination, client_id, &UploadedVideo, play_seconds) send what you
uploaded. UploadedImage::original is optional: leave it as None to offer only
the standard and thumbnail copies, or set it to include the full original.
Applications that use the daemon choose the quality in the request instead.
Use calling media
wechat-calls opens no devices and starts no runtime, so you can use it in your own
call code. Its feature flags select what you get:
| Features | Use |
|---|---|
Default native-runtime | Native session and media integration, protocol and Opus |
No defaults, plus native-opus | Codec and PCM utilities without account code |
| No defaults | Portable PCM queues, pacing and mixing, including WebAssembly |
For complete call operations today, use the Calls API. The crate architecture explains how the crates depend on each other.
Stickers
wechat-media prepares owned images, GIFs and videos, with optional trim and fit,
and exports GIFs and packs. NativeClient provides the Saved and pack calls. Your
application handles the submission journal and profile writeback. Full signatures
are on docs.rs. To use stickers over HTTP instead, see
the sticker guide.
Profile writes
stage_* functions prepare bytes. apply_* functions apply logical updates.
publish_* functions install bytes at profile paths. These names are the library’s
own, not the official client’s. apply_* and publish_* do not give you a single
atomic operation over the whole profile. MMKV staging writes encrypted data and the
matching .crc file. Publication goes through temporary files. Set
WriteOptions::durable_intent_id to the ID of a recovery record you saved. The library
cannot check that the record exists.