Fun with Raspberry Pi - Notifications

April 23, 2018

I really like the Raspberry Pi project, I remember buying my first one and to hold it in my hands, waiting to be hacked around - a cheap, quiet server at my very own home.

As it supports Python (both 2.7 and 3.5) out of the box, I quickly started to write and run Python scripts. Since then I've automated some tasks for automated notifications, scraping websites, serving content like the daily menu, etc.

Most of the scripts have some kind of notification in common, in different forms like Email, SMS or Whatsapp. In this post I'd like to point out the simplest way to send each of those message types using Python.

 Email

It all starts with simple emails, which can be done using smtplib. The simplest way is to open a Gmail account for sending automated mails from it, and then log in and send mails using that account.

import smtplib

def send_email(user, pw, recipient, subject, message):
    """ Send a simple email with subject and message body. """
    session = smtplib.SMTP('smtp.gmail.com', 587)
    session.ehlo()
    session.starttls()
    session.login(user, pw)
    headers = "\r\n".join(["from: " + user, "subject: " + subject, 
       "to: " + recipient, "mime-version: 1.0", "content-type: text/html"])
    content = headers + "\r\n\r\n" + message
    session.sendmail(user, recipient, content)
    session.quit()

Part of this is from Nael Shiab's tutorial. To be able to send emails via Gmail, you'll have to allow less secure apps.

You can then send an email with:

send_email('from@gmail.com', 'pw', 'to@...', 'subject', 'body')

For a version of the script with attachments see my Github Repository.

SMS

In Switzerland, the easiest way I found to date is eSMS (Swisscom and Sunrise only). You can register your own phone number on esms.ch, confirm that it's your number and then send emails to to (recipient)@esms.ch which will be forwarded as SMS. Basically you'll need the @send_email@ from above and send an email to esms.ch:

def send_sms(msg):
    send_email(gmail_user, gmail_pw, tel_nr + '@esms.ch', '', msg)

The downside is obviously that you'll pay the "normal" rate (CHF 0.20) per SMS sent, which will be invoiced on the number you used to register. Also, you can only send as long as the number you entered is used and valid.

Whatsapp

Whatsapp is by far the most complicated to set up, but nowadays maybe one of the most popular messaging and notification systems. I wrote a Pokemon Go Notifier some time ago which also used Whatsapp, and I'd like to show how it's done.

First of all, you'll need a separate SIM-card for sending Whatsapp messages. This should not be one you're using by yourself, as you'll only be able to either use it in a script or on your real phone.

As library to send the messages, you'll need yowsup, which you'll have to download and build:

git clone https://github.com/tgalal/yowsup
cd yowsup
python setup.py install

After building (this may take a while), the yowsup folder should now be a subfolder and contain a file named yowsup-cli, which we're gonna need for registration and sending messages.

Registration

The registration is needed to send messages. You'll need to first send a request for registration, this will send you a SMS with a code, and then send that code for confirmation. --cc is the country code flag, you'll need to set it accordingly (41 is Switzerland).

# Request registration SMS
yowsup-cli registration --requestcode sms --phone 41791234567 --cc 41

# Register with code:
yowsup-cli registration --register xxx-xxx --phone 41791234567 --cc 41

You'll then receive a confirmation:

status: ok kind: free pw: 3jHkhGHVIJF4dDIBIkhbjg87jkbb= price:
CHF 1.00 price_expiration: 1472454114 currency: CHF cost: 1.00 
expiration: 4444444444.0 login: 41791234567 type: new

The important part is the pw. Test if it worked by sending a sample text to the number specified with -s:

yowsup-cli demos -l 41791234567:3jHkhGHVIJF4dDIBIkhbjg87jkbb= -s 41793214567 'Hello Whatsapp!'

If everything went well you should receive a message and you can save the number and password string and use them to send messages. I python, you can use @subprocess@ to make a call to the yowsup-library, like this:

import subprocess

def send_whatsapp(to, msg):
    command = 'yowsup/yowsup-cli demos -l ' + sender + ':' 
    command += pw + ' -M -s ' + to + ' "' + msg + '"'
    subprocess.call(command, shell=True)
    print 'Whatsapp sent.'
    time.sleep(2)

send_whatsapp('41791234567', 'Hello Whatsapp!')

The additional @-M@ flag changes something with the encryption and helped me send messages to groups more reliable, see next section.

Sending to a group

You can also send to a group. To do this, first add your sender nr. to a new group, and then find the id of that group. You'll need to open the yowsup-cli, log in and list the groups you're a member of:

# open yowsup client
yowsup-cli demos --yowsup -l 41791234567:3jHkhGHVIJF4dDIBIkhbjg87jkbb=

# login
/L

# list all groups
/groups list

Now you should see all the groups like this:

The group number will show up as id, something like 41791234567-1442956787, which is the number you can use to send messages to.

Thanks for reading and have fun sending messages!