WebSocket Based Chatbot

This is a continuation of the JavaScript Based Chatbot project

As the owner and moderator of a 100+ member group chat, I quickly found it necessary to develop some means of automating certain processes, such as giving certain information, keeping track of members, and provoking new discussion. While the original JavaScript based version did the job, its reliance on a constantly running Chrome tab proved troublesome. HTML elements would not load properly, or connections would drop, causing the code to fail randomly. Furthermore, updates to the web version of HelloTalk rendered the bot broken on many occasions.

In order to develop a more long-term solution, I began looking into WebSockets and how to establish my own connection to an existing WebSocket server.


My first step was to use the Network tab on Chrome’s developer tools to monitor the WebSocket connection that was established. I made notes of all the data exchanged, the fields included, and the frequency of the messages. As a result, I was able to draft some mock messages to send to the server.

One key behaviour I was attempting to emulate was the heartbeat message that was sent to and from the server every 30 seconds.

Console Application

Once I had the correct message format, I created a C# Console App and used a WebSocket library in order to establish a connection to the server. I was then able to send all the mock messages I had written before to surprising success.

I then wrote code to handle receiving messages.

Subscribing to the MessageReceived event. Each red line covers a different numerical message ID.

Each message from the server had a different numerical ID, which made differentiating between them easy. This particular approach of simply checking if the overall string contains the ID is not secure, as if a user sent a message to the chat containing that ID, the code would pick it up, but since this was a personal project and the risk was minimal, I took this approach.

The major benefit of switching from the previous JavaScript solution to a .NET solution was that I was able to completely cut out the API middleman. I could simply store the group members as their own class objects in a list, and use the Google Sheets API to update the spreadsheet directly instead of relying on my website as a middleman.

Simple Member Class

One challenge with this was that in the WebSocket messages, a user’s country is represented by a 2 letter code, and while it mostly conforms with the ISO 3166 country codes, HelloTalk for whatever reason chose to vary this slightly. One example is instead of using “GB” to represent the United Kingdom, “UK” is used.

For most country codes, I was able to use System.Globalization to convert the country code into the country name for display, but due to the quirk of the name differences I had to write some special cases to counteract this.

System.Globalization usage

Ultimately, this new iteration of the bot works much better than the JavaScript version, and easily runs in the background with no intervention on my part.