Dotnet WeChaty Getting Started
What is WeChaty?
WeChaty is a RPA SDK for WeChat Individual Account that can help you create a chatbot.
Workflow For implement WeChat Bot
- Apply For wechaty token, there are many different token types like Web, Windows and ect, you could refer Switch Protocol(Puppet) to apply based on your scenario
- After getting token, you will be able to implement your own bot with your prefer language like Node.js, Python, Go and Donet
- For Official site, you could refer Getting Started
For this guideline, we will work through the wechat bot with Dotnet and WeChaty GRPC Service, and you will know the basic workflow and knowledge.
If you prefer using existing library, you could refer dotnet-wechaty which is a offical package under development
Workflow for WeChaty Grpc and dotnet
- Create a new dotnet project like Web Application or Console Application, for simple enough, we will start with a console app
- Add Nuget Reference WeChaty.Grpc, QRCoder, Newtonsoft.Json and Grpc.AspNetCore by NuGet Windows or command below
npm install WeChaty.Grpc
npm install Grpc.AspNetCore
npm install QRCoder
npm install Newtonsoft.Json
-
Request for this flow
- Send request to
https://api.chatie.io/v0/hosties/{placeholder for your wechaty token}
var url = @"https://api.chatie.io/v0/hosties/" + "puppet_donut_xxx"; HostieEndPoint model = new HostieEndPoint(); using (var client = new HttpClient()) { var response = client.GetAsync(url).Result; if (response.StatusCode == System.Net.HttpStatusCode.OK) { model = JsonConvert.DeserializeObject<HostieEndPoint>(await response.Content.ReadAsStringAsync()); } }- Get the corresponding Ip and port for GRPC Client
string endPoint = model.IP + ":" + model.Port; endPoint = "http://" + model.IP + ":" + model.Port;- Create Grpc client based on above ip and port
var channel = Grpc.Net.Client.GrpcChannel.ForAddress(endPoint); var grpcClient = new PuppetClient(channel);- listen to grpc client process the message
var eventStream = grpcClient.Event(new EventRequest()); var stream = Task.Run(async () => { await foreach (var resp in eventStream.ResponseStream.ReadAllAsync()) { EventType eventType = resp.Type; string payload = resp.Payload; var response = JsonConvert.DeserializeObject<EventResponse>(payload); // add your custom business logic await BotResponseHandle(resp); } });- Start Bot Grpc Client
await grpcClient.StartAsync(new StartRequest());- send message with grpc client
await grpcClient.MessageSendTextAsync(new MessageSendTextRequest { ConversationId = "The id for room or contact", Text = "Your custom text" }); - Send request to
-
Complete Demo Code
-
FAQ
- If you got any error related with Http2, please make sure you append code below
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);