Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
A complete DIY guide to build your own weather alert bot.
The morning routine is always stressful. Wouldn’t it be wonderful if you had one less thing to worry about in the morning?
What if you had a customizable weather alert bot that sent you a short message ONLY when there was a chance of rain above your pre-defined threshold?
Don’t waste your time checking the weather in a separate app. It can be live on your Facebook messenger chatbox!
What do you need?
- Python 3.6 (or earlier) with pandas and fbchat packages installed
pip install fbchat
- AccuWeather developer account, the free package should be enough. It provides 50 calls/day with 1 key/developer account.
Let’s get started!
At the end of this how-to, you will have 3 files in the scripts folder:
keys.py : to store your facebook email, password, and accuweather API key
params.py : to store the threshold and weather forecast location id
main.py : this is the main script, it will call the keys.py and params.py
1. Setup Facebook account and AccuWeather APIÂ key
First, let’s put your account detail in the keys.py file.
2. Setup parameters
In this step, we will define the threshold for the probability of rain or snow, delay time between each request and message, and also location.
Currently, we set the threshold at 25% for both rain and snow. We will get the alert message only if the AccuWeather data shows the probability ≥ 25%.
The scripts below will request data from AccuWeather every 1 hour ( UPDATE_INTERVAL_HR= 1) and will send a message every 4 hours ( DELAY_TIME_HR= 4).
These parameters will be stored in the params.py file.
3. Retrieve data from AccuWeather
Now here comes the fun part. We will now work on the main script.
If you plan to run it locally, setup your directory and import keys and params. Make sure you put keys.py and params.py in the same folder as this main.py script.
Import required modules.
Define ‘url_page’ to be requested, in this example, we will retrieve 12-hour hourly forecast. Convert our update/delay time into seconds.
Then, request the data and put in pandas DataFrame called ‘json_df’.
At this point, we can inspect the retrieved table. Extract and rename the elements that we need. In this example, we will need AccuWeather link, % rain, % snow, date, and time in the desired format.
If we look closely, the ‘DateTime’ column is a bit tricky to extract and needs some work. After clean up, save it in ‘current_retrieved_datetime’ variable.
Next, write an if-else condition to customize the alert message. The retrieved table provides us a 12-hr forecast. We will check each element of both the rain and snow columns and return a message if the probability is above the threshold.
First, initialize the alert message for each case.
Check ‘Percent_Rain’ and ‘ Percent_Snow’ columns, flag with 1 if the % probability is above threshold (or 0 otherwise).
Sum the columns and modify the ‘rain_msg’ and ‘snow_msg’.
Initialize ‘alert_msg’, modify the messages if there is any ‘rain_msg’ or ‘snow_msg’.
Add the link to variable ‘ link_for_click’ this will be attached to the message when we send later on.
Up until this point, we can now wrap them into a function. Don’t worry if you get lost, I have put them together below.
4. Automated loop
Finally, for the last part, we will automate the process by using loops. The scripts below are putting the number of loops as ‘num_repeat = 999’.
Use try and except to overcome errors (just in case something goes wrong with connections). Call ‘func_get_weather’ function and assign to variables.
Then, check if there are any changes in the weather. If nothing has changed, print the message to the screen. No chat message will be sent.
The message will be sent only if there is any change in the weather.
We can finalize our message at this point. Fetch your user id of your friends and store in ‘friend_list’. Loop to send the message to each friend one-by-one. We put sleep time = 2 seconds between each message and logout after finish.
Execute delay time for the next message. Already defined in params.py file — in this case, it is 4 hours. And another one for AccuWeather request delay is 1 hour.
Again, don’t worry if you get lost. I have put the complete loop together below.
Ta-da! After all our hard work, here is a snapshot of the message we will get.
Facebook chatbox msg. The location id in this example is 135564.
In case we need to know more detail, we can directly click on the link. It will navigate to the AccuWeather mobile website.
The completed script for this how-to is also documented on GitHub.
Thank you for reading. Please give it a try, have fun and let me know your feedback!
If you like what I did, consider following me on GitHub, Medium, and Twitter. Make sure to star it on GitHub :D
How to get Facebook messenger to notify you about the weather was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.