Skip to content

Instantly share code, notes, and snippets.

@dadinugroho
Last active August 6, 2021 02:22
Show Gist options
  • Save dadinugroho/a7743331db9b086924acc0e87a00920e to your computer and use it in GitHub Desktop.
Save dadinugroho/a7743331db9b086924acc0e87a00920e to your computer and use it in GitHub Desktop.
simple forwarded whatsapp message using twilio functions
exports.handler = function(context, event, callback) {
const fromNo = event.From;
const message = event.Body;
let client = context.getTwilioClient();
client.messages
.create({
from: `whatsapp:${context.TWILIO_NO}`,
body: `Forwarded message *${message}* from *${fromNo}*.`,
to: `whatsapp:${context.AGENT_MOBILENO}`,
})
.then(function (message) {
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message('Thank you for contacting us, our agent will reply to you shortly.');
return callback(null, twiml);
});
};
/**
* Steps to use this script:
* 1. Create a new functions in Twilio and named it as 'welcome'
* 2. Create environment variables
* a. TWILIO_NO with value '+14155238886' (do not enter the single quote)
* b. AGENT_MOBILENO with your agent phone number, for example '+6281212345678' (do not enter the single quote)
* 3. Paste above code in the function editor.
* 4. Save the script and do not forget to deploy it by clicking 'Deploy all'.
* 5. Copy the URL and put it on the Whatsapp webhook setting.
*
* Enjoy the service.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment