DGPLUG SUMMER TRAINING

DGPLUG SUMMER TRAINING

This is  a place where you learn how to do stuff, I got to know about summer training when I was going through Twitter feeds. Bhibhas and Sayan tweeted about it and I thought why not give it a try. And now when I look back I made a wise decision joining it. Training generally take place over IRC so anyone can attend it irrespective of where are they from who are they and what is there background. I met a lot of amazing people I got to realize that how much more I need to do.

I started the training as a newbie , I tried to attend as many sessions as possible and most importantly I tried to implement what ever I learnt. I made amazing friends and found peers who can look into my code and we can have a discussion about issues. We had sessions form Kushal which was not only challenging but also fun.

We even had a session from mbuf which was enthralling and makes you ponder over your existence. He is one gem of a person. Sayan use to take class on python and basics of web development, he is one of the person whom I have bugged a lot and I still do but he has never given up on me.

Once a while there are guest sessions from people who are contributing to the upstream, these session actually motivate you towards contributing because they make you feel that all these that you are doing is not without a reason.

At the end people stop attending session I don’t know the reason but actually the doubles during the end because then only comes the real reason for the whole training. The end turns the trainees into contributors and that feeling cannot be converted into words. This training has not only taught me technologies but has given a direction to my life. Thank You DGPLUG.

Advertisements

FEDORA-INFRA: A contributor friendly repo

FEDORA-INFRA: A contributor friendly repo

This all started with zeal to contribute to open source. Seeing your code work is awesome but your code being used by zillions of people out there is not comparable.So it all started when Kushal gave the link to fedora-infra repo and Trishna shared the link to me. I went there and with the limited knowledge of python I have I thought I can contribute to the community.

I took up the ownership to fix an easyfix in the repo, pingou was the mentor for this bug and a super friendly one.I cloned the repo and set up my development environment now since was late but I was so damn interested to fix that bug that I worked for about two hours on it and I was able to get some result . All this while I was on irc on #fedora-apps channel. I pushed it and asked pingou to review it , but he slept. I thought I have to wait till the next day but then threebean and prth came to my rescue.

The reviewed my code and guided me and made me tweaked a little bit. Till now my commit count has reached 5. The next day pingou came online and made me tweak it a little more wait not a little but like crazy more after one more hour of work, the fix was done.

Now I have 8 commits , now the challenge was to squash 8 commits into 1 , I have never done it before and I ran into various issues but to my rescue me pingou was always there he guided me with every step. We tried for 3 hours approx and failed every time.

After that I tried one more time this time very slowly and steadily and there I was  SWEET SUCCESS , I sent a final PR and I think pingou was also online and he accepted it the very moment.The next day I sent another PR and it also got merged.I am really enjoying contributing to fedora-infra.Its just amazing .

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.