Teacher's Little Helper
Tags: python, twiliohackathon, aws
Published: 2020-04-22 16:51:18

My wife Teaches 5th grade, and is currently e-Teaching from home due to Covid-19. They use lots of Google Sheets for various assignments. She needed a way for her students to let her know they need help. She can’t monitor them all simultaneously, so I used some Google Apps Script, Twilio, AWS API and AWS Lambda to help. The students click on a button in a Google sheet if they need help, and she gets a text message letting her know.

**Goal**:
This is actually really easy to build, so I want to show you how I did it, in order for you to recreate yourself, or for any of the Teachers in your life.

![lets begin](https://media.giphy.com/media/WZ4M8M2VbauEo/giphy.gif)

**Tech Stack**:
[Twilio SMS] (https://www.twilio.com/sms): To send a text message.
[Google Sheets] (https://www.google.com/sheets/about/): What the Students see, the User Interface.
[Google Apps Script] (https://developers.google.com/apps-script): The "trigger" that will execute this app.
[AWS API] (https://aws.amazon.com/api-gateway/): The "front door" to the cloud that connects the Google Sheet to the Lambda.
[AWS Lambda] (https://aws.amazon.com/lambda/): The "logic", also known as the serverless environment to hold our Python code.
[Python version 3.8] (https://www.python.org/downloads/release/python-382/): The language we will write our code in.

**Create a Twilio account and select your phone number**
The first thing to do is sign up for a [Twilio] (www.twilio.com/referral/872AjC) account. Sign up for free using this [link](www.twilio.com/referral/872AjC) and receive a $10 account credit. Once you've signed up, buy a [phone number] (https://www.twilio.com/console/phone-numbers/search). *Make a note of the phone number you’ve bought, your Account SID, and your Auth Token. You’ll need them all later.*

![phone number](https://twilio-cms-prod.s3.amazonaws.com/images/23aM_64fcvL3L1v_8Qabb3EDaSwB9AUJ4IdZtrvZo5nGJe.width-800.png)

**Create an AWS Account**
Even if you already have an Amazon account or Prime membership, you will need to create a new [AWS login](https://aws.amazon.com), because it is separate from the rest of Amazon.

![AWS Account](https://twilio-cms-prod.s3.amazonaws.com/images/2JM2NaG8wOF_yq3spSjQBNQiOF5413TK4Q1A6iSdyJrJ1q.width-800.png)

Once you've created your AWS account, you will need to create a Lambda function. Don't worry if you've never done this before, it's actually pretty simple. Just go to the [Lambda](https://aws.amazon.com/lambda/) service, and click on "Create Function."

![teacher lambda](https://teacher-stuff.s3-us-west-2.amazonaws.com/lambdasample.PNG)

- Select "Author From Scratch"
- Type a "Function Name" (this is whatever you want to call your function, it can be anything)
- Enter a "Runtime" (I used Python 3.8, but any 3.x will work) 
- Go to "Permissions" and click "Create a new role with basic Lambda permissions." This lets AWS set it up for you, which is a good idea, unless you're an experienced user.
- Click on "Create Function" on the bottom right hand side of the page.

![teacher lambda1](https://teacher-stuff.s3-us-west-2.amazonaws.com/1.PNG)

This will bring you into the Lambda configuration. Paste the code below to your Lambda. You can also find this code on [Github](https://github.com/Rkauff/twilio-dev-hackathon/blob/master/lambda_handler.py):

```python
    #import the twilio module
    from twilio.rest import Client

    #define your lambda function
    def lambda_handler(event, context):

        #enter your account details here
        account_sid = "paste your twilio account SID here"
        auth_token = "paste your twilio auth token here"

        client = Client(account_sid, auth_token)
        
        message = client.messages.create(
            to="+The phone number you want to send the message to here",
            from_="+The twilio phone number you bought earlier here",
            body="Mrs. Kauffman, I need some help.")
```

It should look like this when you're done: 

![teacher lambda2](https://teacher-stuff.s3-us-west-2.amazonaws.com/lambdasample3.PNG)

*Make sure to add your own Twilio Account SID, Twilio Auth Token, Twilio phone number that you bought earlier, and the phone number that you want the text message to go to in your code.*

Click "Save" in the top right hand corner. Your Lambda is done, but there is one more thing you need to do here. Since we are editing the code inline, we need to handle the Twilio dependency. There are essentially two main ways to use Lambda. We could build our own zip file, details on how to do that [here](https://www.twilio.com/blog/help-button-python-aws-iot-aws-lambda-twilio-wireless). Alternatively, we can write the code inline, and use Lambda Layers for dependencies. For simplicity, we are going to Layer our Lambda function.

Click on "Layers" below "kristins-lambda-function" in my example. Then click "Add a layer."

![lambda layer 2](https://teacher-stuff.s3-us-west-2.amazonaws.com/lambdalayer2.png)

Click "Provide a layer version ARN" and paste [Keith Rozario's](https://github.com/keithrozario/Klayers/blob/master/deployments/python3.8/arns/us-west-2.csv) Twilio layer:

aws_lambda_us-west-2_770693421928_layer_Klayers-python38-twilio_7

This ARN is specific to the us-west-2 region, so if you have difficulty here, double check that you're region in the "Oregon" region.

![region](https://teacher-stuff.s3-us-west-2.amazonaws.com/Capture2.PNG)

 Click "Add."

![lambda layer](https://teacher-stuff.s3-us-west-2.amazonaws.com/lambdalayer.png)

Congrats! Your Lambda function is ready to go!

![yay](https://media.giphy.com/media/pa37AAGzKXoek/giphy.gif)

Now we need a way to trigger the Lambda function. That's where the AWS API Gateway comes in.

In "Services", find the "API Gateway," then click "Create API."

![teacher api](https://teacher-stuff.s3-us-west-2.amazonaws.com/apisample.PNG)

Scroll down to find "REST API," and click "Build." 

![rest api](https://teacher-stuff.s3-us-west-2.amazonaws.com/4.PNG)

From here, select "REST," "New API," Give your API a name, Description, and use "Regional" for Endpoint Type, and click "Create API."

![api3](https://teacher-stuff.s3-us-west-2.amazonaws.com/Opera+Snapshot_2020-04-21_154557_us-west-2.console.aws.amazon.com.png)

Next, you need to create a method, so under the Actions dropdown menu, click on "Create Method," then use "Post," and then click the check mark.

In the "Post" method, you want to select "Lambda Integration," your region (I'm using US-West-2 for this tutorial), and the ARN (Amazon Resource Number), or the *name* of the Lambda Function you just created. Either way works. I'm using the name of my Lambda function here. Hit Save.

![api 3](https://teacher-stuff.s3-us-west-2.amazonaws.com/Opera+Snapshot_2020-04-21_215828_us-west-2.console.aws.amazon.com.png)

It will give you a warning: "You are about to give API Gateway permission to invoke your Lambda function:" 

Click OK.

It should look like this:

![teacher api2](https://teacher-stuff.s3-us-west-2.amazonaws.com/apisample2.PNG)

Nice work! You just built your API, now you just need to Deploy it. You do this by clicking on "Actions", then "Deploy API." You can use "New Stage" as your Deployment Stage, then come up with a Stage Name (I usually call it "Prod", but feel free to call it anything you want, it's your API). Then hit "Deploy."

![deploy API](https://teacher-stuff.s3-us-west-2.amazonaws.com/Opera+Snapshot_2020-04-21_222328_us-west-2.console.aws.amazon.com.png)

Awesome, your API is live!

![nice job](https://media.giphy.com/media/l3UcA1HpL5kLuiWOc/giphy.gif)

The only thing left to do is build your trigger in the Teacher's Google Sheet. 

In the Google Sheet, click on "Tools," then "Script Editor." 

![google doc](https://teacher-stuff.s3-us-west-2.amazonaws.com/googledoc.png)

Paste this simple function into your Google App Script Editor. *You will need to change it to be the API endpoint you just created, otherwise your Google sheet will be trying to call my API.*

    function doPost() {
      UrlFetchApp.fetch("https://git4xk3aif.execute-api.us-west- 
      2.amazonaws.com/Prod/kristins-lambda-function", {muteHttpExceptions: 
      true});
    }

It should look something like this (modified with your API):

![google app script](https://teacher-stuff.s3-us-west-2.amazonaws.com/googleappscript.png)

Save your script by hitting Ctrl + S. Then you can close out of the script editor.

In your Google Sheet, you need a button that will serve as the trigger for your students to press, in case they need your assistance. 

![inserting a picture](https://teacher-stuff.s3-us-west-2.amazonaws.com/Opera+Snapshot_2020-04-22_122707_docs.google.com.png)

And then upload any picture you like. I've used one of my favorite pictures of my wife.

![kristin](https://teacher-stuff.s3-us-west-2.amazonaws.com/Capture5.PNG)

Way to go, you just inserted a picture into your Google Sheet. Now you need to turn that picture into a button that *does something*. 

You can do this by right-clicking anywhere in the image, and then left-clicking in the top right hand corner of the picture (where the red arrow is in the image below). Then click on "Assign Script," and assign the script that you created in your Google Script above.

![assign script](https://teacher-stuff.s3-us-west-2.amazonaws.com/Opera+Snapshot_2020-04-22_231322_docs.google.com.png)

Type "doPost" in the box, and click OK.

![assign script 2](https://teacher-stuff.s3-us-west-2.amazonaws.com/Capture6.PNG)

The first time you click your new button, you will most likely be prompted to give your Google script access, details on how to do that are [here](https://developers.google.com/apps-script/guides/services/authorization). It's pretty straightforward.

Now, when her students click anywhere on that picture, she receives this text message from Twilio.

![teacher text](https://teacher-stuff.s3-us-west-2.amazonaws.com/teacher+text.jpg)

You're done!

![done](https://media.giphy.com/media/xUOwG28DNal5A63KEg/giphy.gif)

![done2](https://media.giphy.com/media/26u4lOMA8JKSnL9Uk/giphy.gif)

If you have any questions, feel free to reach out to me on Twitter [@ryankauffman80](https://twitter.com/RyanKauffman80), [LinkedIn](https://www.linkedin.com/in/ryan-kauffman-mba-2108227/), or send me an email at [koontz2k4@gmail.com](koontz2k4@gmail.com). 

I'm a Twilio Champion, so if you want to check out any of the blog posts I've written for the Twilio blog, you can see them [here](https://www.twilio.com/blog/author/rkauffman)! We can't wait to see what you [build](www.twilio.com/referral/872AjC)!