Well, I’m not sure how to call this process, so I decided to call it a “bot”.

Here is what happened: Every morning, before going out of the house, I have to send a message to TTC or check Google Maps to see when the bus will come.

I’m bored with this mundane task, so I wrote a bus scheduler to send me an iMessage to notify when the bus will come, so I can have time to enjoy life — not just because I’m lazy.

If you want to try it, here is what you need: python and a MacOS machine.

In order to fulfill my “laziness”, I also created a python package name: bustracker — it will help to get the predictions of the bus schedule. Please check the Agency List to see if the package supports your local bus system.

Getting started

Install bustracker python package

pip install bustracker

Prepare the message

Create a bus_reminder.py file to handle the bus schedule request

# bus_reminder.py from bustracker import BusTracker agency = 'ttc'

bus = BusTracker(agency) # get prediction for bus stops

stops = [

{'routeTag': 506, 'stopTag': 3292}

] result = bus.get_predictions(stops) incomings = result['predictions']['direction']['prediction'] bus_name = result['predictions']['routeTag'] next_buses = '' for bus in incomings:

next_buses += str(bus['minutes']) + ',' message = bus_name + ' will come in ' + next_buses + ' minutes'

If you print the message, you will get something like this

print(message)

# 506 will come in 4,7,9,15,21, minutes

Send the message through iMessage

You have a message ready, if you don’t want to send it through iMessage, you can find something that suit you.

The sendiMessage script is from aktau. You can read the detail from the gist.

# bus_reminder.py """

Start sending bus reminder

""" import subprocess

args = ['your-phone-number', message] scpt = '/Users/dnguyen/Documents/projects/bus-reminder/sendiMessage.scpt' p = subprocess.Popen(

['/usr/bin/osascript', scpt] + [str(arg) for arg in args],

stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() if p.returncode:

print ('ERROR:', err)

else:

print (out) # 4

The script will call the sendiMessage.scpt to send the message to your phone number. Make sure that you have iMessage on your Mac and you already had the conversation with that number — otherwise your message won’t be sent.

The schedule part

Now, I want my “bot” to send me a message from Monday to Friday at 7:30AM, so I don’t have to do it. And it’s simply a cron job ;)

Start to edit your cron jobs: crontab -e

30 7 * * 1-5 /usr/local/bin/python3 /Users/dnguyen/Documents/projects/bus-reminder/bus_reminder.py # bus reminder

From now, I will get a message every workday to show when the next bus will come, so I can “rush” — I don’t even need to take the phone out, my Fitbit always shows me the message.

In case you are lazy, here is the full working project from github.