weixin-bot-sdk
Python SDK for the WeChat iLink Bot API.
Install
cd python
pip install .
Quick Start
from weixin_bot import WeixinBot
bot = WeixinBot()
bot.login()
@bot.on_message
async def handle(msg):
print(f"{msg.user_id}: {msg.text}")
await bot.send_typing(msg.user_id)
await bot.reply(msg, f"你说了: {msg.text}")
bot.run()
API Reference
WeixinBot(base_url=None, token_path=None, on_error=None)
Creates a bot client.
base_url: Override the iLink API base URL.token_path: Override the credential file path. Default:~/.weixin-bot/credentials.jsonon_error: Receive polling or handler errors.
bot.login(force=False)
Starts QR login if needed, stores credentials locally, and returns the active session.
@bot.on_message
Registers an async or sync message handler. Each inbound user message is converted into:
IncomingMessage(
user_id: str,
text: str,
type: Literal["text", "image", "voice", "file", "video"],
raw: dict,
_context_token: str,
timestamp: datetime,
)
await bot.reply(msg, text)
Replies to an inbound message using that message's context_token. It also triggers stop_typing() in the background after the reply is sent.
await bot.send_typing(user_id)
Shows the typing indicator in the WeChat chat. The SDK fetches the required typing_ticket through getconfig.
await bot.stop_typing(user_id)
Cancels the typing indicator.
await bot.send(user_id, text)
Sends a proactive text message using the latest cached context_token for that user. This only works after the SDK has seen at least one inbound message from that user.
bot.run()
Starts the long-poll loop, dispatches incoming messages to registered handlers, reconnects on transient failures, and forces a fresh QR login if the session expires.
bot.stop()
Stops the long-poll loop gracefully.
How It Works
login()fetches a QR login URL, waits for WeChat confirmation, and saves the returned bot token.run()performs long polling againstgetupdates.- Each inbound message is normalized into
IncomingMessageand sent to your callbacks. reply()andsend()reuse the internally managedcontext_tokenrequired by the protocol.- On
errcode = -14, the SDK clears saved credentials, requests a fresh QR login, and resumes polling with exponential backoff.
Notes
- Credentials are stored at
~/.weixin-bot/credentials.jsonwith mode0o600. - The package tries to render the QR code in the terminal if
qrcodeis installed, otherwise it prints the URL to stderr. - Long-poll requests use a 40-second timeout and send operations use a 15-second timeout.
- Text messages are split into 2000-character chunks automatically.
License
MIT
Metadata
Release files for weixin-bot-sdk 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| weixin_bot_sdk-0.2.0.tar.gz | 10.4 kB | Without provenance | Details for weixin_bot_sdk-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| weixin_bot_sdk-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for weixin_bot_sdk-0.2.0-py3-none-any.whl |
Total release size: 21.0 kB
Release files / weixin_bot_sdk-0.2.0.tar.gz
| Download URL | weixin_bot_sdk-0.2.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b87faa42e82c7b195350447db32029c157e2a7064864f9a0881d70d38627cae
|
|
BLAKE2b-256 checksum How to use checksums |
3cf59e14d92674be756fc2436609d9ffc997a52467608e1921bcb60d51f0d108
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / weixin_bot_sdk-0.2.0-py3-none-any.whl
| Download URL | weixin_bot_sdk-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3fbb50b9140c33ce4282e68047361153ff781cf7a018e34ae71dd71cb9077904
|
|
BLAKE2b-256 checksum How to use checksums |
3844f55830a0a6fd2d771b6cbab1fe7512c5f9048eb4a393a34bb397c0da0363
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for weixin-bot-sdk 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.