ニュース・ブログ

ニュース・ブログ一覧APIの記事情報Use lambda to send message to WhatsApp

Use lambda to send message to WhatsApp

lambda and whatsapp

Let’s learn how to send messages periodically to a WhatsApp contact with Lambda.

We would need an API for sending message to WhatsApp which will be provided by Twilio. 

(We will be using a sandbox for this time, not a real number to send message, because a real number API is not provided at the time of opening account, one have to apply and wait for a while)

Then using that API, we will send message to WhatsApp with a few lines of code in Lambda. 

This article will be divided in 2 main parts :

  • Twilio : Get tokens/API for WhatsApp
  • Lambda : Use Lambda to send message

1. Twilio Setup :

Okay, let’s go to the official website of Twilio and create a free tier account and log in.

After logging in, go to this WhatsApp-section, which will take you to the following page :

Twilio WhatsApp Section

As you can see from the image, this is a sandbox. So do the following to send message from this sandbox to your WhatsApp account. ( If you have a production ready API then, you don’t need to do this. But for Sandbox which is what we are using, we need to do this verification of Sandbox)

  • Save the number, marked in yellow in the image above, in your phone as Sandbox (or whatever name you like). Example image below :
  • Then send join tongue-in message to that saved contact number (Sandbox Twilio in our case). Example image below :
  • With this sandbox activated with our number. Now we can send messages to our number using account SID and token. Go to this link and get these ids. Example given below :
Location of getting the Twilio token

  🎉

With this, our Twilio Setup is complete. Now let’s go the coding section where all the action is 🍆


2. Lambda

I have divide this section in two parts:

  • Python Code : Which will be sending the message using the API
  • Lambda : Lambda is where we will put our code, and Lambda will run it for free for us.

Python Code : 

Download the code from this repository, and after downloading you will see :

  •  There is a file named send_message_whatsapp.py which contains the main source code
  • And a zip file named aws_lambda_deploy.zip which contains the library needed when uploading the file to Lambda.

Main code example :

Now change the source code according the 👇 following instructions :

  • This is where you have to include your Twilio Sid , and token which you have gotten in the Twilio section.
  • Normally this number (same on which you have saved in you mobile as Sandbox Twilio )is same for everybody, so no need to change. But if changes in the future, you have to change it.
  • Now in here, Change the name and number in which you will be sending the message. (This is the contact you used to send the message to Twilio Sandbox)

After doing the above changes, extract the zip file aws_lambda_deploy.zip and then copy this source file (which you have changed )to the extracted folder and replace it there.
Then, zip it again with the latest source code. Now it’s ready to be deployed to Lambda.

Lambda :

We have done the hard work, now only the fun part is left to do. I am considering you have already an AWS free tier account and logged in right now.
(If you don’t then, please a google search, there should be a plenty of resource which teach you to do this )

  • Create a function in the AWS console. Example image 👇
    – Choose run time as Python 3.6 and give the function a name.
    – Then just click on the button create function

(Optional : If you get any IAM related error, you have to get full access of IAM)

  • After creating the function, you will be taken to the dashboard.
    – Here select the option of uploading a zip file. Then upload the aws_lambda_deploy.zip ( Image given below : )
  • After successfully uploading the zip file, select the edit option in run time settings.
  • Ok, now we are ready to test our Lambda Function. Go to the test section and invoke the lambda.
  • If the result is successful , you can see the following images :

You can see, I have received a text message in WhatsApp. 

Yay Mission Successful 🎉

Troubleshoot :

When invoking the Lambda if you see the following error of time out  :

Go the Lambda Configuration and change lambda time out from 3 -> 5 seconds. And it will work 😃


If you want to a little challenge for yourself, you can create a CloudWatch event and then trigger the function at that specified time. If you want an article on how to set up, let me know.

 

 

週間ブログランキング