Skip to main content

WeChat iLink Bot SDK for Python — async, typed, production-grade

Project description

wechatbot-sdk — Python SDK

WeChat iLink Bot SDK for Python — async, typed, production-grade.

Install

pip install wechatbot-sdk

Requires Python ≥ 3.9. Dependencies: aiohttp, cryptography.

Quick Start

from wechatbot import WeChatBot

bot = WeChatBot()

@bot.on_message
async def handle(msg):
    await bot.send_typing(msg.user_id)
    await bot.reply(msg, f"Echo: {msg.text}")

bot.run()  # login + start in one call

Or with async control:

import asyncio
from wechatbot import WeChatBot

async def main():
    bot = WeChatBot()
    await bot.login()

    @bot.on_message
    async def handle(msg):
        await bot.reply(msg, f"Echo: {msg.text}")

    await bot.start()

asyncio.run(main())

Configuration

bot = WeChatBot(
    base_url="https://ilinkai.weixin.qq.com",   # default
    cred_path="~/.wechatbot/credentials.json",   # default
    on_qr_url=lambda url: print(f"Scan: {url}"),
    on_scanned=lambda: print("Scanned!"),
    on_expired=lambda: print("Expired..."),
    on_error=lambda err: print(f"Error: {err}"),
)

API Reference

Method Description
await bot.login(force=False) QR login (auto-skips if creds exist)
await bot.start() Start long-poll loop
bot.run() Sync: login + start
bot.stop() Stop gracefully
bot.on_message(handler) Register handler (also works as decorator)
await bot.reply(msg, text) Reply (auto context_token + stop typing)
await bot.send(user_id, text) Send to user (needs prior context)
await bot.send_typing(user_id) Show "typing..." indicator
await bot.stop_typing(user_id) Cancel typing indicator
await bot.reply_media(msg, content) Reply with media (image, file, video)
await bot.send_media(user_id, content) Send media to user (needs prior context)
await bot.download(msg) Download media from incoming message
await bot.download_raw(media, aeskey) Download and decrypt a raw CDN media reference
await bot.upload(data, user_id, media_type) Upload file to CDN (does not send)

Media Operations

Sending Media

# Reply with an image
await bot.reply_media(msg, {"image": png_bytes})

# Reply with a file
await bot.reply_media(msg, {"file": data, "file_name": "report.pdf"})

# Reply with a video
await bot.reply_media(msg, {"video": mp4_bytes, "caption": "Check this"})

# Send media proactively (needs prior context_token)
await bot.send_media(user_id, {"image": png_bytes})

Downloading Media

@bot.on_message
async def handle(msg):
    # Auto-detect and download (priority: image > file > video > voice)
    media = await bot.download(msg)
    if media:
        print(f"Type: {media.type}, Size: {len(media.data)} bytes")
        if media.file_name:
            print(f"Filename: {media.file_name}")

    # Or download a raw CDN reference directly
    if msg.images:
        raw = await bot.download_raw(msg.images[0].media, msg.images[0].aes_key)

Uploading to CDN

# Upload without sending — returns UploadResult with CDN metadata
result = await bot.upload(file_bytes, user_id, media_type=3)

Message Types

@dataclass
class IncomingMessage:
    user_id: str
    text: str
    type: Literal["text", "image", "voice", "file", "video"]
    timestamp: datetime
    images: list[ImageContent]
    voices: list[VoiceContent]
    files: list[FileContent]
    videos: list[VideoContent]
    quoted_message: QuotedMessage | None
    raw: dict

AES-128-ECB Crypto

from wechatbot import (
    generate_aes_key, encrypt_aes_ecb, decrypt_aes_ecb, decode_aes_key
)

key = generate_aes_key()
ct = encrypt_aes_ecb(b"Hello", key)
pt = decrypt_aes_ecb(ct, key)

# Decode protocol key (all 3 formats)
k = decode_aes_key("ABEiM0RVZneImaq7zN3u/w==")       # base64(raw)
k = decode_aes_key("00112233445566778899aabbccddeeff") # hex

Project Structure

python/
├── wechatbot/
│   ├── __init__.py      ← Public exports
│   ├── client.py        ← WeChatBot (login, start, reply, send)
│   ├── protocol.py      ← Raw iLink API calls
│   ├── auth.py          ← QR login + credential persistence
│   ├── types.py         ← All types (dataclasses)
│   ├── errors.py        ← Error hierarchy
│   └── crypto.py        ← AES-128-ECB encrypt/decrypt
├── examples/
│   └── echo_bot.py
├── tests/
│   ├── test_crypto.py   ← 10 tests
│   └── test_client.py   ← 8 tests
└── pyproject.toml

Testing

pip install -e ".[dev]"
pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wechatbot_sdk-0.2.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wechatbot_sdk-0.2.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file wechatbot_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: wechatbot_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wechatbot_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d94958cb9536574e57a4d3844a817b3ba7d0cd3cc97ad169a96c37cdacf7f26c
MD5 2dc88a5aa80dea0f9544007ced31ec2f
BLAKE2b-256 3e425091d783adb4f8ed61cbbc5a96bdf56aaacd9e5493a71d31659a0d51bae1

See more details on using hashes here.

Provenance

The following attestation bundles were made for wechatbot_sdk-0.2.0.tar.gz:

Publisher: publish-pypi.yml on corespeed-io/wechatbot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wechatbot_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: wechatbot_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wechatbot_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c3cb0a497837802cc9bf367fd817ca7a680ff1b098f8cf5b131e405296db1e7
MD5 f60790fdc4276d6d9ff8c978c102d6d8
BLAKE2b-256 8bf258f7a9da3a617c353162f520f9f02d229692723436dfa3097b8335bc49d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for wechatbot_sdk-0.2.0-py3-none-any.whl:

Publisher: publish-pypi.yml on corespeed-io/wechatbot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page