Creating a Simple Bot Server Using Python, Django and Django-channels
Basic architecture and code for creating a bot server.

This is a short post to accompany a GitHub repository I recently published. The repository (as the title suggests) helps you get started with using python to create a bot server. I also share some knowledge into the general process of creating a bot server that is not specific to python.
What is the point of the repository?
The main point of repository is to give some boilerplate code for me (and hopefully others) to get started with using Django to create a bot server. Before I get into what’s in the repository, some background on the things that go into creating a bot.
Structure wise, a bot that you interact with has three layers. There is the bot channel that you interact with: this would be the front-end like Facebook, Slack, Telegram, etc. This channel forwards messages to your bot server to which your bot server, subsequently, responds. Of course, the bot server is free to send messages to the channel even without getting a request.

Your bot server itself can have different kinds of architecture depending on the kind of task you are doing. However, at a basic level, what you would want is that there is an interaction or an API layer and the control or thinking layer that sits behind this. It is better to split these layers because both these layers have different complexities to think of. More importantly, the control/thinking layer is very specific to the particular application you are doing and depending on your application may require NLP, dialog management, etc. .
The interaction layer could be independent of all this and the channel and simply translates and relays the message to the appropriate channel. And this is what this repository helps me (and you) get started with.
WebHooks: The Standard Interface to Most Bot Channels
To get started with most bots, in most cases, all you need to do is set up a response to a WebHook. This means that you need to setup a URL that will be called when a specific event happens in the channel. More concretely, when a user sends a message to your Telegram Bot, the Telegram API looks for the WebHook that has been setup for this particular bot; It finds this URL and forwards the message in the Telegram specific format to this URL. You need to write a program that listens to messages at this URL and reacts to it. Reacting to it involves thinking about it and doing intelligent or useful things that I’ve discussed in more detail in this post; this is the job of the thinking layer. Once the thinking is done, the interaction layer is responsible for sending a message back to the bot.
For sending a message back to the bot, you will have to find the “sendMessage” function in the API of the particular channel (in this case Telegram Bot API). Then you need to package your response in the way that Telegram expects it and post it to their API. Along with this package, you will also probably have to provide your authentication to post to that API and probably the token that identifies your bot uniquely.
To Repeat, in a WebHook Based Setup What You Need to Do Is:
(1) setup a WebHook handler and url, (2) Find the API endpoint to send messages to; (3) Format your message in the API’s expected format; (4) Post the message to this endpoint. This is the setup that is used in Telegram, Facebook Messenger and Microsoft’s bot connector framework (and probably others, but these are the ones I’ve used so far.).
For a more detailed explanation of how do this, please refer to this excellent post that helped me get started as well. You can also switch to the master branch of my repository for the code to use a WebHook based setup to connect to a Telegram bot (the readme has step-by-step instructions on how to do this).
Traditionally, you had to come up with some workarounds to get WebSockets to work with Django. Recently, however, there’s a new Django library in town, Django-channels that’s made it extremely easy to get started with things
WebSockets: A More Convenient, and Arguably Better, Interface to Bot Channels

I don’t want to spend too much time discussing the pros and cons of WebSockets: There are some excellent StackOverflow discussions and posts by others more knowledgeable than me about its benefits. In simple terms it reduces the overhead of multiple http requests being sent for each message. Instead, a two-way communication channel is opened up between the two chatting entities and messages are send over this without requiring additional authentication and overhead. It’s faster, simpler and makes your interface layer code a lot easier to look through.
Traditionally, you had to come up with some workarounds to get WebSockets to work with Django. Recently, however, there’s a new Django library in town, Django-channels that’s made it extremely easy to get started with things. The lead developer (I guess), Andrew Goodwin, has also published a very useful set of examples to get started with different ways to use Django Channels as well. The documentation and the examples are very clear and pretty good even for a fairly new library. For someone actually using it I would highly recommend going through it.
Back to the point of this post, if you would like a quick implementation of django-channels in the context of a chatbot, you can make use of my repository. Again, the readme file should help you get it running quickly and I’ve put in comments that should hopefully help you understand what’s going on. Feel free to ask in comments on this post or in the repository if you have any questions.
Accompanying the backend code, I have also added some code for creating a simple chatbot interface as well to test out the whole WebSockets implementation. You can find this in the templates and static files folders.
There are additional complications you need to think about when implementing WebSockets in your production environment. I will talk about this in a later post. Please message me if you would like to know more about this.
Vaisagh is the chief of product and technology at impress.ai which uses a combination of chatbots, AI and I/O Psychology to optimize the recruitment function at companies. Follow or reach out to him or impress.ai if this sounds interesting to you.
