Sending notifications

To send a push notification to a mobile device, we need to use Zapier (https://zapier.com/) and Pushbullet(https://www.pushbullet.com/). Zapier helps you integrate various online services and Pushbullet lets you send and receive push notifications. Create an account in both of these sites.

In Zapier, create a new Zap. Zapier accepts a trigger for a Zap and then performs a predefined action when that Zap is triggered. Add a Webhook as a trigger which can be triggered by sending an API request to the API endpoint provided by Zapier.

In the next screen, choose Catch Hook and click on Continue to get the API endpoint. Now, we need to send a GET request to this endpoint so that Zapier can correctly identify the parameters we send.

To send http requests in Python, you need a module called requests.

pip install requests

Let’s install it and then import it.

import requests

Then, let’s use the following function to send a GET request.

def notify(status,desc,now):

    print("Sending you a notification!")

    url="zapierRestURL"

    if status:

        print(requests.get(url=url, params={"status":"Backup successful!","body":"A new backup was made to Google drive on %s successfully. The backup name is: %s."%(now,desc)}))

    else:

        print(requests.get(url=url,params={"status":"Backup failed!","body":"Backup failed on %s. Error: %s."%(now,desc)}))

The status parameter denotes whether the backup process was a success or not. The desc parameter gives you either the error message or the name of the backup file. The now parameter gives you the current date and time.

Paste your Zapier API endpoint in place of “zapierRestURL”. Now, to configure our Zap, we need to run this method with mock data. So, call this method setting status to true, desc, and now to a random string.

notify(True,"file name","today")

You should get the following output.

Now, go to your Zapier page and click on Ok, I did this.

On the next screen, you should see your status parameter value and the body parameter value. Now, that we have configured our trigger, it’s time we configure our action.

Click on Continue and then add an action. Search for Pushbullet and select Send a Note.

Click on Connect an Account and connect your Pushbullet account to your Zapier account.

Click on Test to see if it succeeds and then move to the next screen.

Set a notification title by clicking on the button to the far right of the title text box. Choose Querystring Status. Choose the body string for the notification message option. Click on Continue.

Now, download and install Pushbullet on your phone from the Play Store and log into your account. Click on Send Test to Pushbullet on the webpage. You should get a push notification on your phone now.

Click on Finish on the webpage. Now, your notification flow is complete.

Now, call the functions in the following order in your Python script to take a backup.

  1. createDriveService
  2. archive
  3. clearPastBackups
  4. upload
  5. clean
  6. notify

I have created a little more complex script that lets you pass the token file and the directory to be backed up as arguments when running the python script.

if (len(sys.argv)>2):

    try:

        token=sys.argv[2]

        service=createDriveService(token)

        if(len(sys.argv)==4 and sys.argv[1]=="backup"):

            token=sys.argv[2]

            service=createDriveService(token)

            fileName=archive(Path(sys.argv[3]))

            clearPastBackups(service)

            upload(fileName,service)

            clean(fileName)

            date=datetime.datetime.now().strftime("%d %B, %Y (%A) at %I:%M %p")

            notify(True,fileName,date)

        elif(sys.argv[1]=="clean"):

            printFiles(service)

            removeAll(service)

        else:

            print("Argument format incorrect. The only arguments that can be used are 'backup' and 'clean'. Pass the token file as the second argument. If you choose backup, please specify the directory to back up as the third argument.")

    except Exception as e:

        date=datetime.datetime.now().strftime("%d %B, %Y (%A) at %I:%M %p")

        print(e)

        notify(False,e,date)

else:

    print("Error: The arguments passed are not enough. You can choose either 'clean' or 'backup'. Pass the token file as the second argument. If you choose backup, specify the directory to backup as the third argument.")

You can obtain my complete script here. My script also includes an additional Clean method that removes all the backup files from your Google Drive just in case you need it.

Once done, you can create a cron job to run the script at specified intervals. I created a bash script file that executed the following command:

sudo python backup.py backup <token> <dir>

Then, I opened the crontab file using the crontab -e command and appended this file to it to run on the first of every month.

Now, my blogs will be backed up every month and uploaded to Google Drive and at the completion of the process, I will receive a notification to my mobile phone.

Pages: 1 2 3 4

Pages ( 4 of 4 ): « Previous123 4