ChatBot is an application that is designed to receive and send messages. ChatBot is dependent on the chat application same as a software is based on your operating system. Most chat application provides an API interface that lets an application send and receive messages. I will be going through a simple telegram chat bot application.

Telegram provides an API so that bot can interface with telegram. Telegram bot can be registered by chatting with a telegram bot called BotFather. Just search for BotFather in telegram and start chatting by sending /start text message. You can then create a new bot by sending text message /new bot. When registering process is completed, BotFather will provide you with token that will look something like 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw. This token is used to authenticate the bot. You can make requests on behalf of bot using following syntax. https://api.telegram.org/bot<token>/METHOD_NAME.

There are two methods of getting update about the messages received by bot: polling and web hooks(interupt). The difference is that in polling method the applications recurrsively checks with telegram server to get new messages recieved by the bot. While in the webhook(interupt) method telegram server makes a https request when it receives message. The polling method have advantage of not requiring a domain name while polling does not.

It is entirely possible to use an http api to create a bot but we have a python package that makes it a lot easier. Please go ahead and install the following package.

pip install pyTelegramBotAPI

Telegram bot has a concept of command. All telegram commands starts with / and it signifies that its a message for the bot. /start is the first message that needs to be send to the bot to activate it.

import telebot

bot = telebot.TeleBot(<token>)

We can now write a function that will be called when bot receives /start message.

@bot.message_handler(commands=[`start`,`help`])
def greet(message):
    bot.send_message(message.chat.id,"Hi {}, I am a assistance bot.".format(message.from_user.first_name))

In the above example we created a function that takes a single argument as a function. The function is decorated by the @bot.message_handler where bot is the object of type TeleBot and the argument specifies condition in which the function is triggered. The parameter message gives you detail of messages recieved by the bot. If you are familar with flask then it is similar to creating a request handler(function that is run in response to the http request).

For the sake of functionality lets create three commands /signin, /signout and /list. /list reports all the users who have signed in.

online = {}

@bot.message_handler(commands=["signin"])
def sigin(message):
    online[message.from_user.username] = message.from_user
    bot.send_message(message.chat.id,"signed in")

In the above code snippet we created a dictionary named online and a message handler that stores the signedin users with username as the key.

@bot.message_handler(commands=["list"])
def list(message):
    users = ""
    for username in online:
        users = users + online[username].first_name + " "
    if users == "":
        users = "Nobody has signed in"
    else:
        users += "are online"
    bot.send_message(message.chat.id,users)

The above message handler loops through the dictionary and creates a message that containing all the user`s first name that have signed in and send message back to the channel.

@bot.message_handler(commands=["signout"])
def signout(message):
    if message.from_user.username in online:
        del online[message.from_user.username]
        bot.send_message(message.chat.id,"signed out")
    else:
        bot.send_message(message.chat.id,"oops you have not signed in before")

The above message handler removes the user from the signed in list if it exists in the signed in list.