Telegram Bot, this is how you do it!

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. 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.

Advertisements

One thought on “Telegram Bot, this is how you do it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s