# Telegram Bot, this is how you do it!

I have been wondering about writing telegram bots, so while searching about it I landed on this link [Python telegram api.](https://pypi.python.org/pypi/python-telegram-bot/1.8) This fueled my curiosity to write my first telegram bot. I searched for many ideas and I was too late to implement them because others have already implemented it.

Then while having a conversation we came across the idea of having a lmgtfy bot to answer silly question , then tools met requirements and voila  lmgtfyou\_bot was born. (Yeah! you got it right, every other name was taken.)

So, yeah you need to install a pip package mentioned in the link above. Before going into details let me discuss something about ‘polling’.

There are two type of models namely polling and observer model. Polling means the function keeps on pinging the application to see if something changed while in observer model it’s like tell me if something changed.

Imagine a very irritating waiter who is disturbing you in every 10s about your order now that is polling while you calling a waiter when you are ready is observer pattern.

Here Telegram api is using a polling pattern to get change in the event , for us it is new messages.Now there is a lot of things that is happening in the code. Let me walk you through it

try:
 LAST\_UPDATE\_ID = bot.getUpdates()\[-1\].update\_id
except IndexError:
 LAST\_UPDATE\_ID = None

So let’s see whats going on here. getUpdates() function returns a list of updates which is a composite data containing various metadata about the message like text, username , date etc. Here, update\_id is taken from the latest update i.e message hence \[-1\]. update\_id keeps on increasing with every incoming message. We use this to control our script which running in an infinite loop.

 while True:
   fetch\_url(bot)

fetch\_url is being called infinite number of times which is being controlled by LAST\_UPDATE\_ID variable.

def fetch\_url(bot):
 global LAST\_UPDATE\_ID

 # Following is a dictionary of commands that the bot can use

 commands = {'/help':"You can add me in any group or text me! I don't have aceess to the group message so you need to call me by my name i.e @lmgtfyou\_bot or start your senstence with '/' , I listen to the keyword 'means' ", '/start':'I am always listening to you. Just use magical words'}

 magic\_words = \['means','mean','/means','/mean'\]

 for update in bot.getUpdates(offset=LAST\_UPDATE\_ID, timeout=10):
     chat\_id = update.message.chat\_id
     message = update.message.text.encode('utf-8')
     message\_list = message.split()

     if(message in commands):
         bot.sendMessage(chat\_id=chat\_id, text=commands\[message\])
         LAST\_UPDATE\_ID = update.update\_id + 1

     if ( list\_compare(magic\_words, message\_list)!= -1):
         search = message\_list\[list\_compare(magic\_words, message\_list)-1\]
         url='http://lmgtfy.com/?q='+search
         bot.sendMessage(chat\_id=chat\_id,text=url)
         LAST\_UPDATE\_ID = update.update\_id + 1

Most of the code is self explaining, but last line of the if block is kind of a base case which is helping to control the actions.

You can host the code on openshift for free, just set up an instance write the code in app-deployment folder and ssh into the instance. Now, in the instance run the script normally and put it in the background.That’s all for this time have a nice hack.

Feel free to fork and contribute to my repo on [GitHub.](https://github.com/farhaanbukhsh/Telegram-Bots)
