Saturday, October 23Digital Marketing Journals

Building a chatbot for Angel & Mortal | by Kingston Kuan | Jun, 2021


Loading players

While it might be possible to use our system to generate Angel-Mortal mappings, organizers often have a more in-depth understanding of social dynamics within the group. I chose to load provided mappings from a CSV file, as opposed to using something more complex (but efficient at scale), as it was simpler for organizers to modify the data. After loading, players can be stored in a dictionary or hash map in memory, similar to an adjacency list

Loading players

Forwarding messages

Note: I reference parts of the python-telegram-bot API without explaining them, please refer to their tutorials for this.

You might have noticed that our Player class includes a username and chat_id. The username is used to identify players and is provided by the organizers while chat_id is what is used to send messages. We cannot send messages directly to a username as it would be unclear how we want the bot to communicate such as in a private message or group chat. Each user needs to start a chat with the bot first in order to get the chat_id that is generated by Telegram. I also went one step further to reject users that do not appear as a key in the dictionary as they are not registered players in the game.

Get chat_id upon start

Conversation handlers can be used to create simple menus to guide users through your bot flow. For this bot the user must first choose either their Angel or Mortal before sending their messages across. They may cancel the conversation at any time to switch between the two. Thanks to the class created in previous steps, it is not difficult to find the reference to their Mortal or Angel and get their respective chat_id to forward messages.

Conversation for sending messages to Mortal

The result should look something like this (with custom names for Angel and Mortal):

Example of a conversation with the bot

Deployment

So far you might have been testing your bot locally but it is probably not a good idea to keep to run this on your own machine for the entire duration of such events. I personally made use of Microsoft Azure free student credits to host but there are many options available.

Improvements

As this was built in a few days with little time for testing there are many improvements you may want to make.

  1. Common feedback from players was that they could not send pictures or emojis through the bot. I had forgotten to account for this which might have to do with the Filters module.
  2. For long term games that require modifications or continuity you may also consider building a proper backend database such as using SQL or NoSQL instead of a simple CSV.
  3. Using the Player class and directed graph design, you can develop advanced algorithms to generate, validate or even modify the player graph for larger games.

Leave a Reply