updated on January 8, 2020
Before working as a professional web developer, I have to admit I really did not know what the hype of chat bots was all about. Until then, I only had contact with those annoying company bots that want to help you when you visit the website of a company. But then, suddenly, it clicked.
Disclaimer: This article is focused on the advantages of using chat bots and will equip you with some arguments for the next discussion with your boss. It is not a programming tutorial. Those will follow in the near future.
When my parents told me that they want to start using WhatsApp, a cold shiver came down my back. As the son of the family that is studying computer science, it was inevitable that every problem they encounter will fall back to me on my next visit.
A few weeks later, my parents got the hang of it. While using WhatsApp or basically any other messenger is not a big achievement for younger people who grew up using this kind of technology, it certainly is for somebody who got introduced to smartphones only a short time ago.
Many of you who have parents which are not into modern technology know that each new applications can be a burden for them. That’s why chatbots are such a great invention. They take an interface that even people with very little technical experience understand and use daily and extend its features. For me, that is the single most advantage of a chat bot.
It might be tempting to just create a basic app for the service or platform you are currently working on. Especially if you have experience creating mobile applications before, a basic app with no technically concerning features seems like no big deal. And you (or your boss) go for it. I mean, the user can just download it, right?
Sure. However, in times where there is such a multitude of digital services, this can grow into a burden for the user. One app for this, one app for that, and after a look at the App Store, you download another because it looks cool. All of the sudden you have so many icons on your phone and really click on a few on a regular basis. Even I, and I see myself as somebody who knows how to handle a phone, do not really like this approach.
In contrast to that, if you decide a bot is sufficient, the user does not have to add anything to his or her phone. The favorite messenger is already installed and from here to the user interacting with your services are only a few tapes and no time.
To me, there is another big advantage of using chatbots. Some of the services I have programmed are just not complex enough that a mobile application or really any kind of fully-fledged application could be filled with sense. If your service is just providing some data from somewhere else in a nicer form, a chatbot might be fully sufficient.
There is another advantage of chatsbots compared to a website or an application: the setup is extremely easy as you will see below. While a homepage or API requires you to have a server with a static ip, the telegram servers takes that part if you are using a chat bot.
If you follow the tutorial for the
python-telegram-bot client (which is the one I strongly recommend), your flow will look like this. The user writes messages to the telegram server as usual. For him/her, there is no difference to texting a friend or family member. However, the telegram server will store the message. Your bot can then authenticate itself using a simple token and ask for the message.
And that’s why the setup is so easy. There is no need to have a static ip or any kind of SSL certificate to do a web request. While I am not quite sure how the polling is handled by the python client, it is very likely that it uses long polling or something similar to keep the latency low here.
After retrieving the users message, you can then come up with an answer, send it back to the telegram server which will then forward it to the user. Done!
What does it look like?
I will not go into very much detail here on how to write a telegram bot. But stay tuned, as I will write some other articles in the future about some specific bots I am using which are also suitable beginner projects.
To get a feeling how
python-telegram-bot works, let’s have a look at a basic ping-pong bot. First you have to get a token for your bot. Just talk to the BotFather, a telegram bot that you ask for new bot and which will guide you through the setup (how ironic). Have a look at the following code and feed in your fresh bot token.
from telegram.ext import Updater, CommandHandler TOKEN = 'myTelegramBotToken' def pong(update, context): update.message.reply_text('Pong') def main(): updater = Updater(TOKEN, use_context=True) updater.dispatcher.add_handler(CommandHandler('ping', pong)) updater.start_polling() updater.idle() if __name__ == '__main__': main()
In just eleven lines of code, you have a fully functioning services that is online and everybody can start using it in a matter of seconds 🚀