# Set up the WhatsApp Echo Bot

To test out the echo bot, we assume you have a Meta developer app with the WhatsApp product added already. Follow the [getting started workflow](https://developers.facebook.com/docs/whatsapp-business-platform/get-started) to onboard as a developer and send your first test message.

After you send your test message, [configure webhooks](https://developers.facebook.com/docs/whatsapp/getting-started/signing-up#configure-webhooks) with the WhatsApp Echo Bot:

| Configure the WhatsApp Echo Bot                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Using Glitch                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| <ol><li>Go to our starter webhook project on Glitch:<a href="https://glitch.com/edit/?fbclid=IwAR38A_HzMvtaY8IITOGvJKTwiVrYUDLfT1vftbDYOx5pyGH7CJs4vkNwiXo#!/whatsapp-cloud-api-echo-bot">Remix on Glitch<img src="https://scontent.fcgk6-2.fna.fbcdn.net/v/t39.2365-6/21624459_135531593756336_210820161941274624_n.png?_nc_cat=107&#x26;ccb=1-7&#x26;_nc_sid=e280be&#x26;_nc_ohc=kxbrEmR6cCgAX888H1Z&#x26;_nc_ht=scontent.fcgk6-2.fna&#x26;oh=00_AfD5NGBFTp-HTVailDfcTYu6K-WwJYPgDV4qZaUBfm2bEA&#x26;oe=65711340" alt=""></a></li><li>Click Remix to edit. A new copy of the project will be created for you.</li><li>Back in App Dashboard > WhatsApp > Getting Started, copy your access token from the developer portal: <img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/281757444_734168554403466_603911146048087076_n.png?_nc_cat=103&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=p4ThbTEKal4AX_9I2TD&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfBIdLWcEgnuBMQpnhQHkWs8BdIkIB1pMZhAdnxqW0ggKQ&#x26;oe=655AD4B5" alt=""><br><br></li><li>Save it into the <code>.env</code> file in the Glitch app under <code>WHATSAPP\_TOKEN</code>.</li><li>Now, set the other <code>.env</code> variable called <code>VERIFY\_TOKEN</code>. This can be any string you choose. In our case, we picked <code>"blue\_panda"</code>.</li><li>At this point, the <code>.env</code> file in the Glitch app will look like this: <img src="https://scontent.fcgk6-2.fna.fbcdn.net/v/t39.8562-6/281695235_401119071881754_5550375008968485033_n.png?_nc_cat=106&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=237dSvEUBzcAX9muzoz&#x26;_nc_ht=scontent.fcgk6-2.fna&#x26;oh=00_AfAFkM4VSY-Gbe17VzDgcIOE_yOJeprgqrbHpESVyBmjWA&#x26;oe=655CB565" alt=""><br></li><li>To get the webhook url from Glitch click the Share button (you need to be logged in to see it) copy the live site link. Make sure to append <code>/webhook</code> to the end of the URL. <img src="https://scontent.fcgk6-2.fna.fbcdn.net/v/t39.8562-6/281447181_1146363302880718_9175083359341520870_n.png?_nc_cat=106&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=uwQLuj2dAhQAX8bjGLd&#x26;_nc_ht=scontent.fcgk6-2.fna&#x26;oh=00_AfDhBRyD_Fyf2ZCkW1tJCP_bNyx3I8Q2KueRxDrnmqsxyw&#x26;oe=655BCF14" alt=""></li><li>Use this URL as the Webhook URL when setting up webhooks on the Meta Developer portal (also enter matching verify token, <code>blue\_panda</code> in our case): <img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/281508579_340408481494631_2117088423930796511_n.png?_nc_cat=109&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=UgS6Erp5Tw0AX9M_CFG&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfBpOYSA5KgwJk_GluXumI09tAaXFuJrMlFm5RFSGDH-rw&#x26;oe=655CBD18" alt=""></li><li>Click Verify and Save. Back in the Configuration screen, click Manage and subscribe to the messages webhooks field:<br><img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/281510144_360890122685772_3740430755168333724_n.png?_nc_cat=109&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=l6hhJgYhBfUAX8AnnP7&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfAOS2hkcH_Wkxr05iCZXjob4KzermOkzjbl4MnfwjRjiQ&#x26;oe=655B30F9" alt=""></li><li>Now, when you send a message to the API number on WhatsApp, it should send back your message in the following format "Ack: {your message}".</li></ol> |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| <p><br><em>Before you start, make sure to</em> <a href="https://l.facebook.com/l.php?u=https%3A%2F%2Fportal.aws.amazon.com%2Fbilling%2Fsignup&#x26;h=AT3Mf7FWObQeavKWAPb9j6-eiEvDh_Zra86MWmcUIcpbE6c-kkO7iR_cBLfhZchygqUHXHBkLBDoU9-FCEARsgX7Wr7NXmS1KKc23vulLzPiAa4-xN-rhpuwqQLRBBEHQk2l0w"><em>register and sign into an AWS development account</em></a><em>.</em></p><p><br>In AWS:</p><ol><li>Open the <a href="https://l.facebook.com/l.php?u=https%3A%2F%2Faws.amazon.com%2Flambda&#x26;h=AT2Y-tZcizJ56iXsUtvpBCwdJ7L4bpInc-ssphrC2pDxfSX26bX1VyElmvx87gVLFgQRiwJSCP_jsJU0NQPTGLcubwyqD1BGHZ_OWgiFzl18M2Z02scf1Cv72sK5leOlBNsYww">AWS Lambda</a> console. AWS operates in several regional data centers and this example is set up in the US East (N. Virginia) <code>us-east-1</code> region. So the actual link is <code><https://us-east-1.console.aws.amazon.com/lambda/home>.</code></li><li><p>Click Create Function > Author from scratch and create your function. For this example, we have added and/or selected the following options:</p><ul><li>Function name: <code>cloudEcho</code></li><li>Runtime: <code>Node.js 16.x</code>, since we're implementing Javascript-based Lambda function</li><li>Execution role: <code>Create a new role with basic Lambda permissions</code></li><li>Advanced Settings: <code>Enable function URL</code> has been checked and <code>Auth type</code> was set to <code>None</code>.</li></ul><p><img src="https://scontent.fcgk6-2.fna.fbcdn.net/v/t39.8562-6/285341837_722709605641441_1567898070320340109_n.jpg?_nc_cat=101&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=TJ0ZhIBMAigAX88k-G1&#x26;_nc_ht=scontent.fcgk6-2.fna&#x26;oh=00_AfBUsNI_5asbkCjPp_yQDKsBt-ldFdYG-hwXTyrLBtyGgA&#x26;oe=655B0C05" alt=""><em>The Advanced Settings look like this:</em><img src="https://scontent.fcgk6-2.fna.fbcdn.net/v/t39.8562-6/292326682_3218865795023663_194500405978298967_n.png?_nc_cat=107&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=q7XZ_LZLaugAX9yHJvs&#x26;_nc_ht=scontent.fcgk6-2.fna&#x26;oh=00_AfATAzHZGOCnj8gEJe6R1dOIfA5riOlz7PpSVHQF10fd5Q&#x26;oe=655C418D" alt=""></p></li><li>Click Create Function.</li><li><p>Once the function is created, you are redirected to the function page. There, you can see the following information:</p><ul><li>The function's URL ending in <code>.lambda-url.AWS\_REGION.on.aws/</code>. Save that for later use.</li><li>The Code source tab for that function.</li></ul></li><li><p>Inside your function's Code Source, you need to add code that does the following:</p><ul><li>Only accepts <code>GET</code> and <code>POST</code> requests</li><li>Verifies using the <code>GET</code>request described <a href="https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests">here</a>.</li><li>Replies to the message based on the message payload structure described <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages">here</a>.</li></ul><p>The code sample to be copied and pasted is:</p><pre><code>const https = require("https"); |

exports.handler = async (event)  => {
const VERIFY\_TOKEN = "Set\_your\_verify\_token\_value\_here";
const WHATSAPP\_TOKEN = "Paste\_whatsapp\_access\_token\_here";

let response;
if (event?.requestContext?.http?.method === "GET") {
// <https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests>
// to learn more about GET request for webhook verification
let queryParams = event?.queryStringParameters;
if (queryParams != null) {
const mode = queryParams\["hub.mode"];
if (mode == "subscribe") {
const verifyToken = queryParams\["hub.verify\_token"];
if (verifyToken == VERIFY\_TOKEN) {
let challenge = queryParams\["hub.challenge"];
response = {
"statusCode": 200,
"body": parseInt(challenge),
"isBase64Encoded": false
};
} else {
const responseBody = "Error, wrong validation token";
response = {
"statusCode": 403,
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
}
} else {
const responseBody = "Error, wrong mode";
response = {
"statusCode": 403,
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
}
}
else {
const responseBody = "Error, no query parameters";
response = {
"statusCode": 403,
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
}
} else if (event?.requestContext?.http?.method === "POST") {
// process POST request (WhatsApp chat messages)
// <https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages>
// to learn about WhatsApp text message payload structure
let body = JSON.parse(event.body)
let entries = body.entry;
for (let entry of entries) {
for (let change of entry.changes) {
let value = change.value;
if(value != null) {
let phone\_number\_id = value.metadata.phone\_number\_id;
if (value.messages != null) {
for (let message of value.messages) {
if (message.type === 'text') {
let from = message.from;
let message\_body = message.text.body;
let reply\_message = "Ack from AWS lambda: " + message\_body;
sendReply(phone\_number\_id, WHATSAPP\_TOKEN, from, reply\_message);
const responseBody = "Done";
response = {
"statusCode": 200,
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
}
}
}
}
}
}
} else {
const responseBody = "Unsupported method";
response = {
"statusCode": 403,
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
}

return response;
}

const sendReply = (phone\_number\_id, whatsapp\_token, to, reply\_message) => {
let json = {
messaging\_product: "whatsapp",
to: to,
text: { body: reply\_message },
};
let data = JSON.stringify(json);
let path = "/v12.0/"+phone\_number\_id+"/messages?access\_token="+whatsapp\_token;
let options = {
host: "graph.facebook.com",
path: path,
method: "POST",
headers: { "Content-Type": "application/json" }
};
let callback = (response) => {
let str = "";
response.on("data", (chunk) => {
str += chunk;
});
response.on("end", () => {
});
};
let req = https.request(options, callback);
req.on("error", (e) => {});
req.write(data);
req.end();
} </code></pre><p><a href="https://developers.facebook.com/docs/whatsapp/cloud-api/guides/set-up-whatsapp-echo-bot">Copy Code</a></p></li><li>Once you paste the code, you will see the Changes not deployed banner pop up. <em>Do not deploy your function yet! We need to set verify token and access token before that.</em></li></ol><p>In Meta for Developers:</p><ol><li>Open Meta for Developers > <a href="https://developers.facebook.com/apps">My Apps</a> and select the app you have created while following the <a href="https://developers.facebook.com/docs/whatsapp-business-platform/get-started">getting started workflow</a>. Click WhatsApp > Getting started.</li><li>Copy the temporary access token: <img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/281757444_734168554403466_603911146048087076_n.png?_nc_cat=103&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=p4ThbTEKal4AX_9I2TD&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfBIdLWcEgnuBMQpnhQHkWs8BdIkIB1pMZhAdnxqW0ggKQ&#x26;oe=655AD4B5" alt=""><br></li></ol><p>In AWS:</p><ol><li>Paste the access token into your Lambda function. Then, set up the <code>VERIFY\_TOKEN</code> field in your Lambda function. For this example, we're using <code>blue\_panda</code>. <img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/286078607_371569038313883_8206283804632562837_n.png?_nc_cat=102&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=nA8OhK2rFhgAX-UnS_c&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfCorJwAH0iLSOLuTsfyUh1y835qvunRHoANnHuCrh9ntw&#x26;oe=655B933E" alt=""></li><li>Click Deploy.</li></ol><p>In Meta for Developers:</p><ol><li>Open Meta for Developers > <a href="https://developers.facebook.com/apps">My Apps</a> and select the app you have created while following the <a href="https://developers.facebook.com/docs/whatsapp-business-platform/get-started">getting started workflow</a>. Click WhatsApp > Configuration > Configure a webhook.</li><li>Under Callback URL, paste your Lambda function URL (this is the URL that was generated immediately after the function was created). Under Verify Token, paste the token you created after pasting your access token (for our example, we're using <code>blue\_panda</code>): <img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/286356523_572748934268377_3236110523873785159_n.png?_nc_cat=111&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=8nW-wcHwJ-gAX-kbnZ7&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfDJClb-aDr69S_3wmsTNe63vknjUEHkRY3jNCYViDWkMQ&#x26;oe=655AD918" alt=""></li><li>Click Verify and Save.</li><li>Once verification is successful, click Manage back in the Configuration page: <img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/292764351_1069382513688257_4510917661209645760_n.png?_nc_cat=105&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=qEZhqBKx_YkAX8d90U3&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfB4YSvbqtUpKRjoMEKve1zDVtsV956ytlVYjHS5YZLCvQ&#x26;oe=655C6A1E" alt=""></li><li>A new dialog module opens up with all available objects you can subscribe to. Click Subscribe for the <code>messages</code> object. Then, click Done.<img src="https://scontent.fcgk6-3.fna.fbcdn.net/v/t39.8562-6/281510144_360890122685772_3740430755168333724_n.png?_nc_cat=109&#x26;ccb=1-7&#x26;_nc_sid=f537c7&#x26;_nc_ohc=l6hhJgYhBfUAX8AnnP7&#x26;_nc_ht=scontent.fcgk6-3.fna&#x26;oh=00_AfAOS2hkcH_Wkxr05iCZXjob4KzermOkzjbl4MnfwjRjiQ&#x26;oe=655B30F9" alt=""></li><li>You are done setting up the echo bot! Now, when you send a message to the API number on WhatsApp, it should send back your message in the following format "Ack from AWS lambda: {your message}".</li></ol> |
