Introduction
About Python
Python is a popular programming language used for many fields such as web development, data science, machine learning, scripting, robotics, and finance. It is appreciated for its simplicity and readability, and is particularly popular in data science and machine learning due to its dedicated libraries.
Check Python official websiteAbout HookNotifier
HookNotifier is a push notification inbox, it allows you to receive push notifications from webhooks.
Check Hook.Notifier official websiteOur first push notification from Python
Objective
As the purpose of this guide is to introduce you to the basics of Hook.Notifier, we will keep it very simple, and we will try to receive a test notification from a basic python script.
The Requests library to the rescue
To communicate with Hook.Notifier, we will need to set up a webhook (http request). To do this we will use the "Requests" library which is widely used by the Python community. You can make a request in another way but the following example will be based on this library.
The following command allows you to install the "Requests" library.
[[bash]]pip install requests
1. A small Python script
We will create a simple python script that includes all the parameters that Hook.Notifier provides. For more information, we refer you to our article Get Started.
Get started with Hook.Notifier
This article aims to introduce you to Hook.Notifier, including its benefits, recommended introductory articles, and developer documentation.
[[python]]import requests
import urllib
# Required
identifier = '%_YOUR_IDENTIFIER_%'
key = '%_YOUR_KEY_%'
object = 'My notification from Python'
body = 'Hooray, my notification from my python script has been sent!'
# Optionnal
tags = 'python,test'
color = urllib.parse.quote('#7C86B4')
image = ''
redirectUrl = 'https://hooknotifier.com'
sound = true
sendToTeam = false
preventData = false
innerData = {
'author': '...',
'message': '...',
'receiver': '...'
}
parameters = f'object={object}&body={body}&tags={tags}&color={color}&image={image}&redirectUrl={redirectUrl}&sendToTeam={sendToTeam}&sound={sound}&preventData={preventData}'
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(f'https://hooknotifier.com/{identifier}/{key}?{parameters}', json=innerData, headers=headers)
This script simply executes an HTTP request to our Hook.Notifier server sending the parameters of your notification. Remember to change your identifiers "%_YOUR_IDENTIFIER_%" and "%_YOUR_KEY_%" and to vary the different parameters of your notification to adapt it to your needs.
2. Triggering and sending
All you have to do is to send your notification by executing your Python script. You should receive your notification within a second!
Conclusion
You should now be able to send notifications from your Python environment. This solution fits perfectly with all Python technologies like Flask for example.
Send notifications when you run server scripts, inject data in the object or body of your notification, get statistics... The possibilities are unlimited with Hook.Notifier. Be creative!
If you like this article, feel free to connect with us on Twitter.
If you have any questions or special requests, such as needing clarification on the terms discussed in this article, more information on a specific part, or additional articles and tutorials, please feel free to provide feedback through the widget below.