![]() |
In this article, we will discuss two apps and how they can be configured using python to send notifications. PushbulletPushbullet, a prominent Python package, which connects multiple devices using python code. In this article, we will discuss how to send messages or notifications through it. Using our computer and python code, we will send some messages to the pushbullet app installed on a mobile device, which has the same login id, as the one on the computer. For this first, we’ll need to create an account on Pushbullet.com and sign in to it, from computer and phone both. Setup:
Now let us move to the python code part and understand how each requirement has to be planned to realize the functionality.
# Used for sending the Push notifications. pip install pushbullet.py==0.9.1 # Used for the interface at the output window. pip install pywebio
Syntax:
Syntax:
Below is the complete implementation. Python3
There is one more way to do the same, discussed below. In this method, a predefined message is sent as a notification. For this, first import the required modules and get your Access Token. Syntax: TOKEN = 'Your Access Token' Then Make a dictionary with all the information you wish to send in the body. Syntax:
Now, to send the posts request, use the posts method specified in the requests module. Push the Pushbullet along the entire path. Now use json.dumps to dump all the data into a data variable. Now pass the dictionary to the header variable, which includes the sender’s authorization, your access token, and content-types, which is application/json in this case. Syntax:
Now look at the response status code; if it’s 200, we’ve had an error; otherwise, our message will have been sent properly. Given below is the complete implementation. Program: Python3
Output: SlackIn this part we will be dealing with posting messages from python script that will appear in slack. For this, we need to make use of webhooks. You can deliver automatic messages from one app to another using webhooks. When you create an Incoming Webhook, you’ll be given a unique URL to which you may send a JSON payload including the message text and some parameters. SetupCreate a Slack Workspace here and create your own app.
Now that we’ve created the app and obtained the webhook URL, it’s time to start coding. We first have to import all the required modules. Now, get your webhook URL and save it to the variable. In a variable, save the message and title you want to send. Now it’s time to make all the slack data we want to send. It consists of your username and in the Attachment section we have:
Now with the use of the sys module, we will get the size of the slack data and store it in a variable. Now for headers, we will define the Content-Type and Content-Length. Use the post method of requests module to post all, the data after dumping it using the dumps function of json module. At last, check whether the response is valid or not with the use of a status code. Program: Sending notifications via slack Python3
Output:
|
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |