How to get Facebook messenger to notify you about the weather

Now, let’s get the weather forecast data.

2.

Retrieve data from AccuWeatherIn this step, we will define the threshold for the probability of rain or snow.

We will delay time between requests and messages.

Currently, we set the threshold at 10% for both rain and snow.

We will get the alert message only if the AccuWeather data shows the probability > 10%.

The scripts below will request data from AccuWeather every hour (Checking_interval_hr = 1) and will send a message every 4 hours (DelayTime_hr = 4).

Then, request the data and put in pandas DataFrame called ‘json_df’.

At this point, we can inspect the retrieved table.

Extract and save only the elements that we need.

In this example, we get the 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.

Loop through the ‘% Rain’ column.

If it is above the threshold, store % probability and time in ‘check_rain’ and break the loop.

Then, loop through ‘% Snow’ column.

If it is above the threshold, store data in ‘check_snow’ and break the loop.

Combine ‘check_rain’ and ‘check_snow’ strings to complete the alert message.

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.

3.

Automated loopFinally, 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’.

Also, we convert the defined delay time from hours to seconds.

Put ‘num_repeat’ in a for loop.

Use try and except to overcome errors (just in case).

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.

Write a 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 — in this case delay_time is 4 hours.

And add another sleep for AccuWeather request delay, ‘Sleeptime’ =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.

AccuWeather LinkThe 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 :P.

. More details

Leave a Reply