In the first Python tutorial I was able to get and sort our weatherdata as you can see below.
{
"Temperature": "26.74",
"Feels like": "24.44",
"Wind Speed": "8.6",
"UV Index": "6.2",
"Weather": "Sunny"
}
But it's quite inconvenient if you always have to login and execute the script, e.g. on your virtual machine. So it would be very useful to create a cronjob that does the task for us - since it allows us to execute any script automatically at any given time. I want to show you how to get this done!
Choose the amazing Vi-Editor in order to edit your cronjob.
root@raspberrypi:/home/pi select-editor
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed
Choose 1-4 [1]: 2
Then you can edit your cronjobs with the following command.
root@raspberrypi:/home/pi crontab -e
We don't want to exceed the limits of our API key, so it is more than enough to retrieve our weaterdata every hour on every single day. To achieve this, add the following command below all the comments in the crontab file.
0 **** <location_of_your_script> # e.g. /home/pi/getWeatherData.py
So now every hour the weatherdata script is executed. To get the most out of it, I can send the data to our Telegram bot so we're able to retrieve weather data as often as we want to. To set up your bot, you don't even need any computer - your Telegram app is enough. Just follow the next steps, it is really easy!
After doing so, you should see a new message from your bot that contains your API Token, e.g. <123456789:ABC123DEF456>.
You now have to enter the following URL into your browser in order to retrieve your Chat-ID:
https://api.telegram.org/botXXXX:YYYY/getUpdates
Simply replace the XXXX:YYYY with your API Token. So in our case the URL would look like this:
https://api.telegram.org/bot123456789:ABC123DEF456/getUpdates
The request returns a json response and contains your Chat-ID. It should look something like this:
{
"chat": {
"id": 789789789,
...
}
}
I now have everything I need in order to send messages to our Telegram Bot. That's stunning - you can test the functionality via URL as well, just enter the following into your browser:
https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={CHAT_ID}&text={NOTIFICATION_TEXT}
In our case the URL would look like this:
https://api.telegram.org/bot123456789:ABC123DEF456/sendMessage?chat_id=789789789&text=HiThere
Now you should get a notification from your bot that contains your message - so I can finally start to code in order to send data to our bot!
There's also an important information I want to tell you about. It is possible that you want to send other data as well to your bot, which is the reason why it is a good idea to create a function that can be used whenever it has to. So I create a new file that contains our telegram function - and I import that function in our first python script. Let's begin writing our function!
#!/usr/bin/env python3
# We import requests to send data to our bot.
import requests
# We import json in order to retrieve/send the data in a json format.
import json
# We import sys to exit the script as clean as possible, e.g. if the data can't be sent to our bot.
import sys
def telegramBotSendText(bot_message):
bot_token = "123456789:ABC123DEF456" # your API Token
bot_chatID = "789789789" # your Chat-ID
send_text = "https://api.telegram.org/bot" + bot_token + "sendMessage?chat_id=" + bot_chatID + "&text=" + bot_message # sends data to your bot
try:
response = requests.get(send_text)
return response.json()
except:
print("Not possible to retrieve data")
sys.exit()
if __name__ == "__main__":
print(telegramBotSendText("This is a test"))
If I execute this script, the message "This is a test" should show up in my bot.
So now I just have to import the function in our first python script. You just have to add the following lines to the weatherdata script - but don't forget that these scripts have to be in the same directory or you have to append the path to it.
#!/usr/bin/env python3
# We import requests to call the OpenWeatherMap API.
import requests
# We import json in order to retrieve the data in a json format.
import json
# We import sys to end the script as clean as possible, e.g. if the API is unavailable.
import sys
Import the function from the telegram script - first put the name of your script and then the name of the function.
from telegramBot import telegramBotSendText
After doing so, add the following line to your weatherdata script as well.
if __name__ == "__main__":
print(weatherdata())
telegramBotSendText(weatherdata) # send data to telegram with the telegramBotSendText function
And I'm done! Now you will get your weatherdata hourly via Telegram - how cool is that! It's truly amazing what you can achieve with just a few lines of code.