Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is your deepest secret? The API #34

Closed
5 of 7 tasks
counselorbot bot opened this issue Apr 11, 2022 · 1 comment
Closed
5 of 7 tasks

What is your deepest secret? The API #34

counselorbot bot opened this issue Apr 11, 2022 · 1 comment

Comments

@counselorbot
Copy link
Contributor

counselorbot bot commented Apr 11, 2022

Week 3 Step 6 ⬤⬤⬤⬤⬤⬤◯◯◯ | 🕐 Estimated completion: 10-20 minutes

Can you keep a secret?

This week, you will be going through steps to set up a Twilio account and create an Azure function that texts your Twilio number.

✅ Task:

  • 1: Create a new branch named deepsecrets
  • 2: Create an HTTP Trigger Azure Function and add its URL to a repository secret named DEEPSECRETS_ENDPOINT
  • 3: Fill in the Azure Function URL as a webhook
  • 4: Code the Function so that it returns the message it was sent
  • 5: Test your function by texting your Twilio number and seeing output texted back to yourself.
  • 6: Commit your function code to deepsecrets/index.jsto the deepsecrets branch
  • 7: Create a pull request that merges deepsecrets to main, and only merge the pull request when the bot approves your changes!

🚧 Test Your Work

To test your work, try texting a message to your Twilio number - you should receive a text message back that repeats your own message.

❓ How do I find my Twilio number?

See your phone numbers.

❓ Example output?

194569969_1687986154736022_4227041675617722938_n

Note: You might not get a text back on your phone because Twilio sometimes won't allow you to return a single variable as the entire body, but as long as the code passes the test, you will be okay!

💡 Yay! This means you've successfully configured your Twilio webhook.

Set Up a Twilio Account

Since you should already have a trial Twilio Account from Week 2, you will only need to reconfigure the webhook URL. You will use the same phone number as before.

1: Create an Azure Function

Next, we'll want to create an Azure Function that will eventually output the content of texts that your Twilio number receives. This will just be a simple HTTP trigger function.

Finally, we need to install the npm package qs to use in our function code later.

2: Configure Your Webhook URL

When someone sends a text message to your Twilio number, Twillio can call a webhook: your Azure Function. You can send a reply back simply by returning your message in the request body.

❓ Webhook? Twilio? I'm lost!

Fear not!

Webhooks are essentially just HTTP callbacks that are triggered by an event - in our case, this event is receiving an SMS message. When that event occurs, Twilio makes an HTTP request to the URL configured for the webhook.

We'll configure the Webhook URL by filling in the Azure Function URL as a webhook.

❓ How do I find my Azure Function URL?

Navigate to your Function page (Overview), and click Get Function URL.

image

❓ How do I configure my Twilio webhook URL?
  1. Go to the Twilio Console's Numbers page

image

  1. Click on the phone number you'd like to modify

  2. Scroll down to the Messaging section and the "A MESSAGE COMES IN" option.

  3. Paste in your Azure Function URL. Make sure to click Save afterwards!!

image

3. Write your Azure Function

Now, we'll want to write our Azure Function. Go to the code for the function you created previously in this step.

First, we'll need to instantiate our qs npm package in order to use it.

❓ How do I use the `qs` package?
const querystring = require('qs');

Next, we'll use the querystring object to parse our request's body.

❓ How do I parse the request body?
const queryObject = querystring.parse(req.body);

From this output, we'll find its Body attribute and return it.

❓ How do I return the output?
context.res = {
   body: queryObject.Body
};

Since we are returning queryObject.Body as the entire body, you might not receive a text back on your phone. This is Twilio's issue, but the counselor bot's test should pass fine if your code is correct still!

@counselorbot
Copy link
Contributor Author

counselorbot bot commented Apr 12, 2022

⏰ Time to merge!

Go ahead and merge this branch to main to move on. Great work finishing this section!

merge

⚠️ If you receive a Conflicts error, simply press Resolve conflicts and you should be good to merge!

@counselorbot counselorbot bot closed this as completed Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants