You are on page 1of 11

❏ Twilio setup:

1- Sign up for a Twilio account: ​https://www.twilio.com/try-twilio​ (no credit card required)

2- Activate the Twilio Sandbox for WhatsApp:


https://www.twilio.com/console/sms/whatsapp/sandbox

It allows you to prototype with WhatsApp immediately using a shared phone number,
without waiting for a dedicated number to be approved by WhatsApp.

To get started, select a number from the available sandbox numbers to activate your
sandbox.
Be sure​ to take note of the phone number you choose in the Sandbox. You will need
this later when we're ready to send some messages. (In the image above the number is
+14155238886).

3- Go to your whatsapp application on your phone and send a message to twilio


sandbox as described here in your account:
https://www.twilio.com/console/sms/whatsapp/sandbox

Basically, you need to send “​join <your sandbox keyword>​” to your Sandbox number in
WhatsApp to join your Sandbox, and twilio will reply with a confirmation that you’ve
joined.

When this is completed, you should be able to see your number here:
https://www.twilio.com/console/sms/whatsapp/sandbox​ ​under​ ​Sandbox Participants​.
4- Gather your Twilio account credentials. You can find these in the Twilio console:
https://www.twilio.com/console
● Account SID​ - Used to authenticate REST API requests
● Auth Token​ - Used to authenticate REST API requests

❏ AWS setup:

1- Open your AWS account and create an IAM role to give permissions to lambda to
write to cloudWatch logs:

2- Create a lambda function as shown below: select python 3.7 for runtime and the role
we created above.
3- Go to AWS API Gateway and create a new API:
4- Select the API you created and create a new resource called “message” as shown
below:

5- Select the resource you created in the step above and create a POST method.
Select the lambda function we created in step 2:
6- Deploy the API:

Create a new “dev” stage:


7- Take note of the URL in the dev Stage Editor (top of the screen).

Then run this curl command in a terminal:

curl -X POST
https://iqxlrv7fq1.execute-api.us-east-1.amazonaws.com/dev/message​ (to be
replaced with ​YOUR URL. ​Do not forget to add the resource​ /message ​at the
end)

You should get this response back:

{"statusCode": 200, "body": "\"Hello from Lambda!\""}


❏ New relic Setup:

1- Open your new relic account, click on Alerts then on Notification channels.
Click on add New notification channel and select webhook:

Give the channel a name like “whatsapp integration” and then put the URL we created
with API Gateway (in step 7 in AWS setup) in base Url field.

Click on “use custom payload” and add ​“to”​ attribute the the payload to include the
whatsapp number you would like to notify.​ This number is the number you have
added in Twilio setup in step 3.

{
"account_id": "$ACCOUNT_ID",
"account_name": "$ACCOUNT_NAME",
"closed_violations_count_critical": "$CLOSED_VIOLATIONS_COUNT_CRITICAL",
"closed_violations_count_warning": "$CLOSED_VIOLATIONS_COUNT_WARNING",
"condition_family_id": "$CONDITION_FAMILY_ID",
"condition_id": "$CONDITION_ID",
"condition_name": "$CONDITION_NAME",
"current_state": "$EVENT_STATE",
"details": "$EVENT_DETAILS",
"duration": "$DURATION",
"event_type": "$EVENT_TYPE",
"incident_acknowledge_url": "$INCIDENT_ACKNOWLEDGE_URL",
"incident_id": "$INCIDENT_ID",
"incident_url": "$INCIDENT_URL",
"open_violations_count_critical": "$OPEN_VIOLATIONS_COUNT_CRITICAL",
"open_violations_count_warning": "$OPEN_VIOLATIONS_COUNT_WARNING",
"owner": "$EVENT_OWNER",
"policy_name": "$POLICY_NAME",
"policy_url": "$POLICY_URL",
"runbook_url": "$RUNBOOK_URL",
"severity": "$SEVERITY",
"targets": "$TARGETS",
"timestamp": "$TIMESTAMP",
"violation_callback_url": "$VIOLATION_CALLBACK_URL",
"violation_chart_url": "$VIOLATION_CHART_URL",
"to" : "+15005550006"
}

Create the notification channel.

Send a test notification and make sure you get:

{
response: 200,
{"statusCode": 200, "body": "\"Hello from Lambda!\""}
}

❏ Update the lambda to connect to twilio (twilio deps):

1- Download the lambda zip file from: ​https://github.com/aminoz007/Whatsapp-lambda

2- Unzip the archive file.


Open the lambda_function.py file and make these updates:
a. “from_”: Use the phone number you choose in the Sandbox when you created
your twilio account (step 2 in twilio setup instructions, this is different than your
personnel number)
b. “auth_token”: Your twilio auth token (step 5 in twilio setup instructions)
c. “account_sid”: Your twilio account sid (step 5 in twilio setup instructions)
​ Zip again all files, compress all files under “Archive” so that the file you
updated above is in the top level with the other files and folders.

3- Open the lambda function we created previously in AWS. Then change the code
entry type to “upload a .zip file”

4- Upload the zip file and save your lambda function (wait until the file is uploaded).

You are all set!!!

Go to NR webhook and make sure you have put your phone number in the “to” field as
described in the NR setup paragraph, then send a notification test.
You should receive this message from twilio sandbox (second message in the screen
shot):

You might also like