WeChatBot 0.1.1

dotnet add package WeChatBot --version 0.1.1
                    
NuGet\Install-Package WeChatBot -Version 0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WeChatBot" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WeChatBot" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="WeChatBot" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WeChatBot --version 0.1.1
                    
#r "nuget: WeChatBot, 0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WeChatBot@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WeChatBot&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=WeChatBot&version=0.1.1
                    
Install as a Cake Tool

WeChatBot

一个面向 .NET 的微信 Bot SDK,封装了登录、长轮询收消息、文本回复、媒体上传下载、输入中状态等常用能力。

当前项目目标框架为 netstandard2.1,适合被 .NET Core / .NET 5+ / ASP.NET Core 项目引用。

功能特性

  • 二维码登录与凭据缓存
  • 长轮询接收消息
  • 文本消息发送与回复
  • 图片、视频、文件消息发送
  • CDN 媒体上传与下载
  • 输入中状态控制
  • 原始消息结构与较友好的消息模型

安装

发布到 NuGet 后可以这样安装:

dotnet add package WeChatBot

如果你还在本地开发,也可以直接以项目引用方式接入。

快速开始

using System;
using System.Threading;
using System.Threading.Tasks;
using WeChatBot;

class Program
{
    static async Task Main()
    {
        var bot = new WeChatBot.WeChatBot(new BotOptions
        {
            LogLevel = "info",
            OnQrUrl = url => Console.WriteLine($"请扫码登录: {url}"),
            OnScanned = () => Console.WriteLine("已扫码,请在微信中确认"),
            OnExpired = () => Console.WriteLine("二维码已过期,正在刷新"),
            OnError = ex => Console.WriteLine(ex.Message)
        });

        await bot.LoginAsync();

        bot.OnMessage(async message =>
        {
            Console.WriteLine($"收到来自 {message.UserId} 的消息: {message.Text}");

            if (!string.IsNullOrWhiteSpace(message.Text))
            {
                await bot.ReplyAsync(message, $"你刚才说的是: {message.Text}");
            }
        });

        await bot.RunAsync(CancellationToken.None);
    }
}

常见用法

发送文本

await bot.SendAsync(userId, "你好");

回复文本

await bot.ReplyAsync(message, "收到");

发送图片

var data = await File.ReadAllBytesAsync("demo.jpg");
await bot.SendMediaAsync(message.UserId, SendContent.FromImage(data, "这是一张图片"));

发送文件

var data = await File.ReadAllBytesAsync("report.pdf");
await bot.SendMediaAsync(message.UserId, SendContent.FromFile(data, "report.pdf", "请查收"));

下载收到的媒体

var media = await bot.DownloadAsync(message);
if (media is not null)
{
    await File.WriteAllBytesAsync(media.FileName ?? "download.bin", media.Data);
}

控制输入中状态

await bot.SendTypingAsync(message.UserId);
await Task.Delay(1500);
await bot.StopTypingAsync(message.UserId);

凭据缓存

默认会把登录凭据保存到当前用户目录下:

~/.wechatbot/credentials.json

你也可以通过 BotOptions.CredPath 指定自定义路径。

主要类型

  • WeChatBot:主入口,负责登录、收发消息、媒体处理
  • BotOptions:配置项
  • IncomingMessage:接收到的消息模型
  • SendContent:媒体或文本发送模型
  • DownloadedMedia:下载后的媒体内容
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.1 105 3/25/2026