Joann is a software developer who specializes in automating repetitive tasks.
In a previous article, I showed you how to create boards, lists, and cards in Trello using Python and the Trello API. We read text files that contain to do lists and automatically exported them to our Trello board.
In this article, I will show you how we can apply this automation to real work scenarios. Work usually involves meetings and minutes are often sent via email. Action items are discussed and later on distributed to the attendees this way but in a sea of emails and heavy workloads, we sometimes:
- Forget to read it
- Find it tedious to transfer them to our to do lists manually
- Have trouble keeping track of which date those minutes are for
To solve these problems, we'll use the Gmail API together with the Trello API. We'll search for emails that have a specific subject, setup a template to identify where the action items are, and export those action items to Trello. This will allow us to manage our tasks efficiently.
I'm using Python 3.8.2 but you can use other versions. Some syntax could be different especially for Python 2 versions.
Trello API Key And Token
You need the key and token to connect and make requests to your Trello account. Sign in to your Trello account from the browser and follow the instructions to get your key and token. Take note of the your key and token.
Gmail API Client Configuration
Sign in to your Google account and go to Python Quickstart. Click the "Enable the Gmail API" button, select "Desktop app", and click the "Create" button. Download the client configuration as "credentials.json".
Before we dive into writing code, I want to show you how our project structure looks like so we can avoid confusion on where each script should go.
- The main.py file is the main script that we will run.
- The modules folder contains three files:
- The credentials.json file is downloaded from the Google Developers website.
- The gmail.py file contains the methods that will assist us in accessing, searching, and reading the emails we need from our Gmail account.
- The trello.py file contains the methods that will assist us in creating boards, lists, and cards in our Trello board.
- The requirements.txt file contains the libraries we need to make things work
- The settings.py file contains the configurations such as key, token, etc.
Create a "settings.py" file with similar contents as in the sample code below.
- email_address - Replace this with your Gmail email address.
- scopes - We'll only be reading emails so we can keep this as is.
- key - The key you get from Trello following the steps in the "Requirements" section above.
- token - The token you get from Trello following the steps in the "Requirements" section above.
- subject - The subject of the email we'll be looking for.
- item_start and item_end - The action items between these two will be retrieved and copied over to Trello.
Here's a list of the libraries we're gonna need. To install them, simply enter "pip install -r requirements.txt" in the command line.
Read More From Owlcation
Using the Trello API
The "trello.py" script will be used to create boards, lists, and cards. For a full explanation on this script, you may refer to the previous tutorial.
Using the Gmail API
The "gmail.py" script will be used to access the emails in our Gmail account.
Below are the sample emails we will use. Notice that the words we are looking for are in bold text - Date:, Action Items, and Other Notes. Gmail wraps words in asterisks (*) to indicate that they are in bold text. This is why in our "settings.py" file, we look for "*Action Items*" instead of simply "Action Items".
The email samples can be downloaded from here.
Writing the Main Script
Now that we created the modules needed to access both Trello and Gmail, we will bring them together in a main script.
In line 8, we query the mailbox for any emails that match the subject in the "settings.py" file. In this case, the subject it will look for is "Minutes of the Meeting".
From line 11, we loop through the emails that matched our query and read their contents. Inside this loop, the following steps are performed:
- In lines 20 to 21, we split the body of the email line by line, look for the line that contains the date label specified in the "settings.py". In this case, it's "*Date:*". We retrieve only the part that contains the actual date and use it later on to name our Trello board.
- In line 22, we retrieve all the texts in the body from the item_start to item_end. In our "settings.py" file, these are "*Action Items*" and "*Other Notes*"
- In line 25, we create a board with the subject and date combination as the title and in the same line, we also create a list with "Action Items" as the title.
- From line 26, we read the lines under "Action Items", clean them up, and create a card for each of them.
When you first run the code, a window will pop up asking you to grant access to your email. If you have multiple Google accounts logged in, simply select the account you indicated in the email_address variable in the "settings.py" file.
After this, you will notice that a "token.pickle" file is created in your modules folder. The next time you run the script, you will no longer be asked to grant access. If you want to use a different email address, simply change the email_address value, replace the "credentials.json" file, and delete the "token.pickle" file so that you will be prompted to grant access again where you can select a different account.
When you access your Trello, you will find that two boards are created with different dates. Each board has a list named "Action Items" and under it are the actual items. You can modify the code to suit your needs. Maybe you only want one board with multiple lists where each list represents one date or you want to use the actual date the email was sent instead of what's in the body.
© 2020 Joann Mistica