After having many good sessions in free open source software workshop………We successfully wrapped it up and its time for workout on some interesting and challenging tasks for beginners to enter the club…….
Tasks were given in 3 categories viz Easy ,Medium,Hard ….out of which we can do as many as possible……moving into my works …
Level : Easy
As many of us play this game with our friends for time pass….our task is to play this game with com …here’s how I made it
step 1: I start game by taking the winning score at first as input…hence who ever reaches the score first will be the winner ….
step 2: Next i will initiate two scores each for player and computer ….which are equal to zero before starting the game.
step 3: Next i will run a while loop to start the game and end it when score of either player and computer reaches to scoring point…..In while loop ….at each iteration player inputs his choice and then computer takes any one of rock,paper and scissors randomly….to execute this I imported random module…
step 4: Based on both outputs score changes at each iteration and when when anyone reaches the scoring point…the while loop ends and winner will be declared…here’s the link to checkout my work….
after playing a game……
Here I made a telegram bot by using telegram API which gives you latest flash news happening in India… for this I took skeletal code from the blog “https://www.codementor.io/garethdwyer/building-a-telegram-bot-using-python-part-1-goi5fncay” moving into my work…..
step 1: first of all we need to create our telegram account and from it we should get out bot token from Botfather….then we must be able to access our bot’s browser page i.e..”https://api.telegram.org/bot<yourtoken>/getme”…… that’s the page where you would be able to see your bot details and then if you send a message to your bot from your telegram account you must be able to see that details of message in the page “https://api.telegram.org/bot<your token>/getUpdate” page…and our code will be based on these pages ….
step 2:In our code we will first download the content in our telegram bot’s url by requests module and from then we will extract json data from our bot’s page and similarly get the details of message send to our bot from url “https://api.telegram.org/bot<your token>/getUpdate” and get out chat Id and “text” (text is nothing but the message that we send to our bot)
step 3: In this step we will send the desired reply to the message we received.As we get a list of messages sent to our bot….we will design our code as only to respond for the last received message by using offset concept and we will reply to the highest chat Id we got message from…
step 4: Next we will scrap the required data from a website.As I mentioned above about latest news…I am scrapping data from “Indiatoday” website as it allows large number of url requests…..For data scraping I used beautiful soup module.
step 5:Now we have to send this data as a reply….we can do this by updating our url by adding text and chat ID content to the url.we import time module and make it to run and check for new messages for each 0.5 seconds.For all the steps mentioned above I have used functions which you can easily understand from skeletal code..
my news bot :
1.Twitter API to send status update from CLI:
To complete this task we require four tokens of our Twitter API which gives access to our API from our code…..We can get this by creating a twitter app from developer of API twitter for which you have to apply for permission which takes sometime to be granted..After getting token we can proceed to our code…..
step 1:First of all we will import a module named “tweepy” which is a library of twitter for python to handle different properties of twitter.
step 2: Next I stored all my tokens viz consumer key,consumer token,access token and access secret in four variables.
step 3:The next step is creating an OAuthHandler instance. Into this we pass our consumer token and secret to authenticate ourself. And similarly our acess tokens.we then authenticate by giving command “api=tweepy.API(auth)”
step 4:Next we update our status by just giving a simple command “api.update_status”.That’s it , we have successfully updated our tweet…
meanwhile in twitter:
2.Sending gmail from CLI:
Instead of using GMAIL API …I have used a simple module viz “smtplib” ….a python library to send emails to different websites based on portals.
step 1: First of all I have imported smtplib which is crucial one…..
step 2:Next I will establish a connection by command “smtplib.SMTP(‘smtp.gmail.com‘,587)”,where 587 is the port of gmail.
step 3:Next ill input my gmail id and password and store it….as password is confidenital,I use getpass method to made it invisible while entering password.
step 4:next ill take to address and description and store it in variables.By using sendmail command..Ill send an email to the respective account..
sending an email:
That’s the end of the tasks done by me.. 🙂 Hope these helps me pulling out further… 🙂