24

I'm running some scripts and looking for an easy way to ping my phone once the script has finished running.

Doing some research on the web, I've seen ways of sending messages using Slack, Push bullet, twilio, email etc.

I am looking for recommendations for an easy way to send a ping/message from python to my phone.

Easy in the sense it dose not require considerable configuring of outside accounts or pay services.

F.D
  • 767
  • 2
  • 10
  • 23
  • If you're running Linux, and staying on the same WiFi network is acceptable, KDE Connect has a very nice command line client you can use to ping your phone. `kdeconnect-cli -d --ping-msg 'Script complete!'` – Different55 Apr 17 '18 at 14:27
  • Unfortunately i'm not! But thanks for the suggestions! – F.D Apr 17 '18 at 14:31
  • Hmm... the other thing that comes to mind is https://ifttt.com/. It's been years since I've used it, and never with Python, but I did find this to interact with it, and IFTTT should be capable of doing what you want. https://github.com/briandconnelly/pyfttt – Different55 Apr 17 '18 at 14:36

8 Answers8

12

I've tried a Twilio, but it's complicated and I don't think it can send you messages for free (anymore). Telegram seems an easy solution. To extend to the answer of @Nick PV, here are the steps I, as a Telegram beginner, took:

1) Get a Telegram account (free) using your phone number

Web: https://web.telegram.org Also download the Telegram Andriod. Of course, you want the notifications on your phone after all

2) Go into settings (web or app) and set a username

This is needed to obtain an id which your bot will use to send messages to

3) Send a message to RawDataBot to get your id

Just search for RawDataBot and send any message (hi will do). Take a note of your id.

4) Create your bot (which you'll command with HTTP requests)

Now search for BotFather and send the message /start. Help is displayed. Send the message /newbot and follow the instructions. Take a note of your token to access the HTTP API

5) Send the API request using Python

You could install and use telegram-send, but if you are like me and you prefer the generic library requests which will give you the experience to handle any HTTP API, this is how to do it:

import requests
token = "token_from_step_4"
url = f"https://api.telegram.org/bot{token}"
params = {"chat_id": "id_from_step_3", "text": "Hello World"}
r = requests.get(url + "/sendMessage", params=params)

Links:
Telegram bots: https://core.telegram.org/bots
API Docs sendMessage https://core.telegram.org/bots/api#sendmessage

arjobsen
  • 313
  • 2
  • 12
  • 1
    I would just add that you need to first send message to bot in order to make it work. https://stackoverflow.com/questions/41174831/telegram-bot-chat-not-found – Michal Palko Apr 04 '22 at 11:18
  • Couldn't edit (too many pending edits). The request needs to be a POST request: r = requests.post(url + "/sendMessage", params=params) – trex Aug 06 '23 at 06:21
11

U can try cmd: pip install telegram-send and just send a message to your Telegram bot. Create your telegram bot at BotFather, take a token from there, paste it to

cmd: telegram-send --configure

Usage:

import telegram_send
telegram_send.send(messages=["Hello world"])

I found this much better than any other push notification.

For more info: Link1 Link2

Nick PV
  • 415
  • 6
  • 14
  • works on my phone, but how can i add second person to it ? i want to push to another person too – ikel Feb 11 '21 at 02:08
  • 1
    @ikel just give a link to your bot to that person, or create a private group and set your bot to redirect everything to that group, then follow that group and accept everyone who should see be there. (million ways to do this) – Nick PV Mar 13 '21 at 06:20
  • I keep getting this error ImportError: cannot import name 'MAX_MESSAGE_LENGTH' from 'telegram.constants' (C:\Users\RBI\AppData\Local\Programs\Python\Python311\Lib\site-packages\telegram\constants.py) And there is no solution available for it. I have tried pip install -r requirements.txt but to no avail – Ankit Jul 02 '23 at 05:01
10

I have found a much easier way, but it doesn't works on Linux. Here is a link for more details.

First you have to install notify_run:

pip install notify_run

Then you have to register:

notify-run register

It will give you a QR code (on windows the QR code doesn't works) and a link, which will take you to a website, there press the "Subscribe on this device" (Maybe you will have to refresh the site)

Then use this code:

from notify_run import Notify

notify = Notify()
notify.send('any message you want')
david
  • 153
  • 2
  • 9
4

In the end, the easiest way I found was using Slack. It takes one Python function (about 6 lines) and an Slack account.

More details can be found here on medium

F.D
  • 767
  • 2
  • 10
  • 23
3

I tried pushsafer and notify-run without success on my computer (Linux Mint 19.3 tricia)

On my computer, only Slack works properly. This post comes from this Youtube video where you will see the full process in Video.

What you need first is Slack application on your device (iOs, Android, ...) and create an Slack account (if not done)

Tuto :

  1. Create a workspace and an application (see details in this video. Sorry, I did not put the substeps here but I put this answer as a Community wiki in order to let you finish what I started :) )

  2. Install the python library pip3 install sandesh

    pip3 install sandesh

  3. Get your webhook => https://hooks.slack.com/services/blablabla

  4. Use this sample to send a message to Slack :

python code:

import sandesh
sandesh.send("This is my test", webhook="<put here the https link of your webhook>")

You can find the open source sandesh python module here in gitHub

sangorys
  • 1,161
  • 10
  • 16
  • There's also a `slack` python library which I use instead of `sandesh` and I've found it to be really easy to use -- Tech With Tim has a [good tutorial](https://www.youtube.com/watch?v=KJ5bFv-IRFM&ab_channel=TechWithTim) on it. No need to configure web hooks -- just supply your API token to the `WebClient` and call `chat_postMessage(channel, message)` – Bilbottom Apr 10 '21 at 08:43
2

You already mentioned it in your answer but Pushbullet is pretty easy:

  1. Create a pushbullet account on their website using a gmail (I recommend not using your main one).
  2. Install the android app and login with the same gmail. (Despite what their instructions say, you do not need to give any permissions to the Android app. Not even notifcations.)
  3. From the website go to settings -> Account -> Create Access Token. Copy this token to use in your python script.

From python (yes you need the "py" at the end of the name)

pip install pushbullet.py (see project on pypi)

import pushbullet
pb = pushbullet.PushBullet(YOUR_ACCESS_TOKEN)
push = pb.push_note('Some Title', 'Some message you want to send')

This will pop up a notification on your Android phone with the message you specified. Works from Windows or Linux.

User
  • 62,498
  • 72
  • 186
  • 247
1

Using firebase fcm

One of the easiest and reliable way is to use Firebase Cloud Messaging for sending push notifications.

You can get server key from the firebase console after creating your project. You can get device token by generating it in your device see: https://firebase.google.com/docs/cloud-messaging/android/client (iOS, android and web)

## Install request module by running ->
#  pip3 install requests

# Replace the deviceToken key with the device Token for which you want to send push notification.
# Replace serverToken key with your serverKey from Firebase Console

# Run script by ->
# python3 fcm_python.py


import requests
import json

serverToken = 'your server key here'
deviceToken = 'device token here'

headers = {
        'Content-Type': 'application/json',
        'Authorization': 'key=' + serverToken,
      }

body = {
          'notification': {'title': 'Sending push form python script',
                            'body': 'New Message'
                            },
          'to':
              deviceToken,
          'priority': 'high',
        #   'data': dataPayLoad,
        }
response = requests.post("https://fcm.googleapis.com/fcm/send",headers = headers, data=json.dumps(body))
print(response.status_code)

print(response.json())

Hope you found helpful

Arnav Singh
  • 194
  • 2
  • 4
0

Might not be the easiest way, but seems to be the most open one:

Similar to the Telegram bot, this will use a Matrix bot.

Automation through one of the available Python packages; matrix-nio seems to be the best one as of now; although there are packages building up on it possibly making the usage easier. Examples how to set it up can be found at: https://matrix-nio.readthedocs.io/en/latest/examples.html#a-basic-client

Another alternative would be email, but I prefer instant messaging over that for the purpose of these notifications. The Matrix protocol offers a high amount of flexibility at the same time.

clel
  • 175
  • 1
  • 3
  • 8