TLDR  :)= You can find full project on github – https://github.com/jaronphillips/MotivationalTexter

This has been a fun project. It could easily be turned into some kind of fun prank.   What started out as a joke from my sister-in-law has turned into something quite fun for me.  My wife and her sister asked me to help with some fitness goals.  Normally I would pay no attention to such a request, but this one gave me a fun idea. I wanted to randomly text them throughout the day to remind them to focus on fitness. I also wanted to automate it. I figured it would be a good python project.

First I needed to figure out if this was  possible.   I googled something like  “python text message.”  There is plenty of stuff that popped up, but most of it was for Twilio, and you had to purchase a phone number. Well, I was not that invested in this project. I did not want to pay for a new phone number…  especially when google offers free ones with google voice.   I googled something like “python google voice sms” and found this wonderful blog by Motion Design https://motiondesigntechnology.wordpress.com/2014/01/24/mass-texting-sms-messaging-via-google-voice-and-python/

Now we are off to the races! The googlevoice function made this pretty easy. You can send a text from python with just a few lines of code:

from googlevoice import Voice
gv=Voice()
gv.login()
gv.send_sms("7204369797","Hey there tubby")

Now we read from a list of phone numbers and we can send everyone on that list a text:

ListOfNumbers="PhoneNumbers.txt"
PhoneList=open(ListOfNumbers, "r")
PhoneNumbers=PhoneList.read().splitlines()
for PhoneNumber in PhoneNumbers:
     gv.send_sms("7204369797","Hey there tubby")

The next goal was to send a random message every time.  I used another text file filled with random messages. One message per line. I also imported the random function.  Then I counted how many lines were in my text file and used that number as the high threshold for my random number generation. Then I created a randomizer variable. All the variable does is picks a random number between 0 and the number of lines in my text file.  Then I use that number to return 1 random line from my text file:

import random

ListofSayings="Sayings.txt"
Myfile=open(ListofSayings, "r")
sayings=Myfile.readlines()
count=int(len(sayings)-1)
randomizer=random.randint(0,count)
randomstring = sayings[randomizer]

The last thing I wanted this to do was send these texts randomly throughout the day. I did not wan them to get a text at the same time every day. I used the time and random functions for this. The sleep attribute of the time function is in seconds, so this example is a random sleep for anywhere between 5 and 30 seconds:

import time
timer= random.randint(5,30)
time.sleep(timer)

After I got the random timer set up, I felt like I had most of the bones in place. So I organized the code a little better and put the whole thing in a while loop.:

from googlevoice import Voice
import random
import time

#GoogleVoice set up
gv=Voice()
gv.login()

#Text Files used
ListofSayings="Sayings.txt"
ListOfNumbers="PhoneNumbers.txt"

#Open text files
Myfile=open(ListofSayings, "r")
sayings=Myfile.readlines()
PhoneList=open(ListOfNumbers, "r")
PhoneNumbers=PhoneList.read().splitlines()

#Run Forever
while True:
     count=int(len(sayings)-1)
     randomizer=random.randint(0,count)
     randomstring = sayings[randomizer] 
     timer= random.randint(5,30)
     time.sleep(timer)
     for PhoneNumber in PhoneNumbers:
          gv.send_sms(PhoneNumber,randomstring)

Now this code above works fine.  But.. it cannot run unattended. The googlevoice login requires you to enter your email and password every time this runs. Luckily there are arguments for this.  But we don’t want save our gmail credentials in this script.  That is not a good way to do it.  That is where keyring comes in 🙂

Keyring lets python interface with Windows Credential Manager. Windows Credential manager is where windows keeps your saved credentials, similar to GNOME keyring or KWallet.  Using it actually pretty easy. From the python command line, (Outside of your script) you can enter  keyring.set_password(‘accountname’,’username’,’password’) to set your password.  You can also use this to set your username. I was planning on posting this project on github so I did not want my username or password in the script. instead I set both values outside of the script like this:

import keyring

keyring.set_password('1,'1','password')
keyring.set_password('2,'2','username@gmail.com')

Then I retrieved them and used them to login to google like this:

#Google voice Login
voice = Voice()
usr=keyring.get_password('2','2')
pwd=keyring.get_password('1','1')
voice.login(usr,pwd)

Now this thing is pretty much ready to go. It will run all day and all night sending random texts. But…. my wife probably does not want to receive texts all night. Now I need to add something that keeps track of the time… I used the datetime function for this.  I just created a variable and compare it to the current time before sending a text.

import datetime 
now=datetime.datetime.now().time()
if now > datetime.time(8) and now < datetime.time(22):
     print ("do something")

Now it is ready to go… here is what i finally ended up with:

import random
import time
from googlevoice import Voice
import datetime
import keyring

#How often texts will be sent in minutes.
MinIncriment=10
MaxIncriment=90

#Files used
ListOfNumbers="PhoneNumbers.txt"
ListofSayings="Sayings.txt"
LogFile="MotivationLog.txt"

#google voice login
voice = Voice()
pass=keyring.get_password('1','1')
user=keyring.get_password('2','2')
voice.login(user,pass)

#open files
PhoneList=open(ListOfNumbers, "r")
PhoneNumbers=PhoneList.read().splitlines()

Sayinglist=open(ListofSayings, "r")
sayings=Sayinglist.readlines()
count=int(len(sayings)-1)

while True:
        timer= random.randint((MinIncriment*60),(MaxIncriment*60))  
        
        randomizer=random.randint(0,count)
        randomstring = sayings[randomizer]

        now=datetime.datetime.now().time()
                      
        time.sleep(timer)
      
        if now > datetime.time(8) and now < datetime.time(22):
            for PhoneNumber in PhoneNumbers:
                voice.send_sms(PhoneNumber, randomstring)

You can find full project on github – https://github.com/jaronphillips/MotivationalTexter

It will likely be a different version if you look at it there. I am planning on moving all the settings and text file information into a mySQL server. There will be more to come on this bad boy.

These are all the resources I used to help me do this project:

https://motiondesigntechnology.wordpress.com/2014/01/24/mass-texting-sms-messaging-via-google-voice-and-python/
https://googlevoice.readthedocs.io/en/latest/?badge=latest
https://docs.python.org/2/library/random.html
https://stackoverflow.com/questions/20170251/how-to-run-the-python-program-forever
https://stackoverflow.com/questions/415511/how-to-get-the-current-time-in-python
https://stackoverflow.com/questions/5068731/run-command-only-between-certain-times-else-run-other-command?rq=1
https://stackoverflow.com/questions/12330522/reading-a-file-without-newlines
https://www.esri.com/arcgis-blog/products/product/analytics/scheduling-a-python-script-or-model-to-run-at-a-prescribed-time/
https://alexwlchan.net/2016/11/you-should-use-keyring/
https://stackoverflow.com/questions/15235139/how-do-i-retrieve-a-username-with-python-keyring
https://www.quora.com/How-do-you-round-numbers-in-Python
https://www.w3schools.com/python/showpython.asp?filename=demo_mysql_connection
https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
https://www.w3schools.com/python/python_tuples.asp
https://www.w3schools.com/python/python_mysql_insert.asp
http://www.mysqltutorial.org/mysql-unique/
https://stackoverflow.com/questions/7696924/way-to-create-multiline-comments-in-python
https://stackoverflow.com/questions/574730/python-how-to-ignore-an-exception-and-proceed
https://stackoverflow.com/questions/19326004/access-a-function-variable-outside-the-function-without-using-global