Is Today a Recyling Day?
Is what I ask myself every Wednesday morning. On Wednesday's garbage is collected but every other week it's Garbage and Recyling day, but I can never remember and end up having to check the calendar on a website. So inconvenient, right?! Let's automate with Python and Telegram. Telegram is a text messaging service kind of like WhatsApp but it's completely free, and they have an API for things just like this. Head on over to Telegram.org for more info.
What we want to accomplish is this: a Telegram bot to text me every Wednesday morning, indicating if it's a recycling and garbage day, or just garbage. But how do I schedule a task to run every morning? This is a tricky task (sorry,—I had to) especially for beginners. Enter PythonAnywhere, an incredible source for developing and programming with Python. It offers full-blown Python environments and website hosting with your own subdomain, and a helpful blog and forum if you're just getting started. One of it's cool features and a co-subject of this post are scheduled tasks, in which you can write a script to execute at a set time every day.
With the time this has saved me from having to manually check recycling calendars, I'm able to share how I did it with the world!
Are you ready?
We'll need the following:
- A Pythonanywhere.com account - a free account will work.
- The Telegram app installed on your phone.
- Pictures of garbage/recycling (optional).
Download the telegram app on your smartphone and start a chat with the @BotFather. Create a new bot with the /newbot command, and the @BotFather will chat with you for the details. He'll ask you for a general name for your bot and then a unique username. Once finished you'll be given an HTTP API token—keep that handy as we'll need it later.
Back in the main chat menu of Telegram, create a new group. Tap the search bar, and search for the username of your bot. What you'll end up with is a "group" chat, but it's only yourself and your bot. Now we need one additional credential before we can jump into the Python script, and it's the group chat ID. To get the ID, go to https://web.telegram.org and follow the steps to get into your account. Once in, click the group chat you created with your bot. In the address bar, you'll see a parameter that begins with the letter 'g'. Take note of this ID. If you get stuck obtaining the chat ID, see this SO question and answers:
Let's start by writing the Python script, garbage.py
import telegram from datetime import date bot = telegram.Bot(token='YOUR_UNIQUE_TOKEN')
Import the necessary libraries and create our bot. Remember to grab your own token that @BotFather gave you.
recycling_days = ['2018-01-10', '2018-01-24', '2018-02-07','2018-02-21', '2018-03-07', '2018-03-21', '2018-04-07', '2018-04-18', '2018-05-02', '2018-05-16', '2018-05-30', '2018-06-13', '2018-06-27', '2018-07-11', '2018-07-25', '2018-08-08', '2018-08-22', '2018-09-05', '2018-09-19', '2018-10-03', '2018-10-17', '2018-10-31', '2018-11-14', '2018-11-28', '2018-12-12', '2018-12-26']
Create a simple list of strings of the recyling days.
today = date.today()
Assign today's date to the variable
if today.weekday() == 2: if str(today) in recycling_days: bot.send_photo(chat_id=-<YOUR_CHAT_ID>, photo=open('garbage_recycling.jpg', 'rb')) bot.send_message(chat_id=-<YOUR_CHAT_ID>, text="Recycling!") else: bot.send_photo(chat_id=-<YOUR_CHAT_ID>, photo=open('garbage.jpeg', 'rb')) bot.send_message(chat_id=-<YOUR_CHAT_ID>, text="Today is just a garbage day.")
Note the '-' before your chat id so an example would be
Pythonanywhere's scheduled task feature will run every single day at the time you tell it to run. For free accounts, you cannot specifiy what day you want it to run, or at what hour. So we have to decide in our code what day of the week it is, since Garbage/Recycling days are only on Wednesdays (and we don't want to get a Telegram message every day). We just evaluate if
today.weekday() == 2 where Monday is the first day of the week starting at 0, Tuesday = 1, Wednesday = 2, and so on.
The rest of the code checks to see if todays date (converted to a string) is in our list of
recycling_days. If it is, we send a photo of recycling cans and a message "Recyling!"
Otherwise, we send a garbage only image and message.
Go to your Pythonanywhere account, navigate to the Files tab and upload our file, garbage.py, and the images. Then go to the Consoles tab and under "Start a new console:" select Bash.
Once the terminal fires up, you'll need to install the Telegram python package.
In the console, type:
pip3.6 install --user python-telegram-bot
And, since the default Python version in Pythonanywhere is 2.7, you'll need to add a shebang on the first line of garbage.py so that Python 3.6 is used to run the script.
#!/bin/env python3.6 import telegram # The rest of the code...
Finally, navigate to the Tasks tab of our Pythonanywhere account. Under scheduled tasks, set the UTC time for when you want the script to execute. I set mine for 10:00 which is 6:00AM NYC time and right around the time my recycling confusion sets in.
In the command text box, enter
python 3.6 /home/YOUR_USERNAME/garbage.py and create the task.
And now, wait and see if everything worked!