Creating a chatbot with Amazon Lex, AWS Lambda, and Integrating with WhatsApp (Part 1).
Hello techies,
Applying for a new job with a referral?
Not knowing how to ask for a referral?
Irritated to write the same referral message again and again?
If you answer “YES” to any of the above questions, then here is a solution for it!
Think of a WhatsApp bot, where you only need to give the details of the Name of the person, Job ID, company name, past experience, achievements, and of course we can't send without our resume to get a personalized referral message.
We are going to make a referral chatbot and integrate with whatsapp in this tutorial.
Check the final product demo here:
Follow all the steps carefully.
Letzz goo!
Steps:
- Create a Lambda function.
- Create a bot.
- Build and test the bot.
- Integrate with Whatsapp.
Step1 — Create a Lambda function
- Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/
- Choose to Create function.
- Select Author from scratch.
- Type the name ReferralProcessor.
- For the Runtime, choose the latest version of Node.js.
- For the Role, choose Create new role from templates.
- Enter a new role name ReferralRole.
- Choose Create function.
- In the Function code section, choose Edit code inline, and then copy the following Node.js function code and paste it in the window.
'use strict';
// Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
// --------------- Events -----------------------
function dispatch(intentRequest, callback) {
console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);
const sessionAttributes = intentRequest.sessionAttributes;
const slots = intentRequest.currentIntent.slots;
const nameOfPerson = slots.nameOfPerson;
const isStudent = slots.isStudent.toLowerCase();
const degree=slots.degree;
const course=slots.course;
const collegeName=slots.collegeName;
const position=slots.position;
const companyName=slots.companyName;
const hadPastExperience=slots.hadPastExperience.toLowerCase();
const roles=slots.roles;
const companies=slots.companies;
const resumeLink=slots.resumeLink;
const jobIdOrjobLink=slots.jobIdOrjobLink;
const yourName=slots.yourName;
if(isStudent=="yes" && hadPastExperience=="yes"){
callback(close(sessionAttributes, 'Fulfilled',{'contentType': 'PlainText', 'content': `Hello ${nameOfPerson}, Hope you are doing well.
I am a ${course} student pursuing ${degree} in ${collegeName}.
I wish to propose my candidacy for ${position} at ${companyName}.
I love improving systems and want to apply my learnings into real world applications.I have been an ${roles} with ${companies} respectively.
Here's my resume.
Resume Link: ${resumeLink}. If you find me a deserving candidate, I would be glad to be referred for ${jobIdOrjobLink} at ${companyName} and implement the propesed improvements.
Hoping to hear from you soon.
Warm Regards
${yourName}`
}));
}
else if(isStudent=="yes" && hadPastExperience=="no"){
callback(close(sessionAttributes, 'Fulfilled',{'contentType': 'PlainText', 'content': `Hello ${nameOfPerson}, Hope you are doing well.
I am a ${course} student pursuing ${degree} in ${collegeName}.
I wish to propose my candidacy for ${position} at ${companyName}.
I love improving systems and want to apply my learnings into real world applications.
Here's my resume.
Resume Link: ${resumeLink}. If you find me a deserving candidate, I would be glad to be referred for ${jobIdOrjobLink} at ${companyName} and implement the propesed improvements.
Hoping to hear from you soon.
Warm Regards
${yourName}`
}));
}
else if(isStudent=="no" ){
callback(close(sessionAttributes, 'Fulfilled',{'contentType': 'PlainText', 'content': `Hello ${nameOfPerson}, Hope you are doing well.
I had worked as ${roles} with ${companies} respectively.
I completed my graduation in ${course} from ${collegeName}.
I wish to propose my candidacy for ${position} at ${companyName}.
I love improving systems and want to apply my learnings into real world applications.
Here's my resume.
Resume Link: ${resumeLink}. If you find me a deserving candidate, I would be glad to be referred for ${jobIdOrjobLink} at ${companyName} and implement the propesed improvements.
Hoping to hear from you soon.
Warm Regards
${yourName}`
}));
}
}
// --------------- Main handler -----------------------
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
try {
dispatch(event,
(response) => {
callback(null, response);
});
} catch (err) {
callback(err);
}
};
- Choose Save.
- Test the Lambda Function Using Sample Event Data.
- On the function page, in the list of test events, choose Configure test events.
- Choose Create new test event.
- In the Event name field, enter a name for the event Test.
- Copy the following Amazon Lex event into the window.
{
"messageVersion": "1.0",
"invocationSource": "FulfillmentCodeHook",
"userId": "user-1",
"sessionAttributes": {},
"bot": {
"name": "ReferralBot",
"alias": "$LATEST",
"version": "$LATEST"
},
"outputDialogMode": "Text",
"currentIntent": {
"name": "ReferralBot",
"slots": {
"nameOfPerson":"NAME OF PERSON WHO IS GONNA REFER YOU",
"isStudent": "Yes",
"degree": "DEGREE",
"course": "COURSE",
"collegeName": "COLLEGE",
"position": "SEEKING ROLE",
"companyName": "COMPANY NAME",
"hadPastExperience": "yes",
"roles": "ROLES",
"companies": "COMPANIES LIST",
"resumeLink": "YOUR RESUME LINK",
"jobIdOrjobLink": "JOB ID",
"yourName": "NAME"
},
"confirmationStatus": "None"
}
}
- Choose to Create.
- Choose Test and Lambda runs your Lambda function.
- In the result box, choose Details. The console displays the following output in the Execution result pane.
Next steps continued in further posts.
Link of Part 2: https://ravuri-poojitha123.medium.com/creating-a-chatbot-with-amazon-lex-aws-lambda-and-integrating-with-whatsapp-part-2-7cf110b7ae7
Link of Part 3: https://ravuri-poojitha123.medium.com/creating-a-chatbot-with-amazon-lex-aws-lambda-and-integrating-with-whatsapp-part-3-8fab2e5ec441