0

I'm working on a dialogflow chatbot. It retrieves the user's name and email at the start of the conversation, and I have a sessions-vars output context with a lifespan of 60 to keep these parameters throughout the conversation. I know they are kept by checking diagnostic info and referring to them in chatbot replies as #context.parameter.

Near the end of my conversation path there in an intent called 110checklistemail.sendemail where my chatbot asks the user if they want information emailed to their email or sent in the chat. If the user says "Email it to me" I have webhook call enabled, where it redirects to fulfillment code, pasted below.

I followed a guide to integrate the chatbot with sendgrid, and the email is sent and does work if I prompt for the email at that specific intent. However, if I don't prompt for the email address (eg. the user says 'email it to me') then the agent is not able to send the email because the email parameter is now blank, despite being populated before in a context with a long lifespan.

Fulfillment code is the following:


'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const sgMail = require('@sendgrid/mail');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
process.env.SENDGRID_API_KEY = 'SG._APIKEYHERE';
  
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
  
    function sendEmail(agent) {
    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
    const emailParam = agent.parameters.email;

    const msg = {
      to: emailParam,
      from: 'tyler@mailfence.com',
      subject: 'Just a quick note',
      text: 'Just saying Hi ${agent.parameters.given-name} from Dialogflow...',
      html: 'Just saying <strong>Hi Hi ${agent.parameters.given-name} from Dialogflow</strong>...',
    };
    console.log(msg);
    sgMail.send(msg);

    agent.add(`What a beauty!`);
  }
  
  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('110checklistemail.sendemail', sendEmail);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

Snippet from Diagnostic info:

{
  "responseId": "130742d9-7453-41c6-8b27-ee8f91d9d02d-5a74d3f9",
  "queryResult": {
    "queryText": "Email it to me",
    "parameters": {
      "email": ""
    },
    "allRequiredParamsPresent": true,
    "fulfillmentText": "What a beauty!",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "What a beauty!"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/tyler-vhyo/locations/global/agent/sessions/aef22896f9/contexts/1await_checklist_type",
        "lifespanCount": 4,
        "parameters": {
          "Jobtype.original": "custom shower",
          "email.original": "",
          "Jobtype": "custom shower",
          "email": ""
        }
      },
      {
        "name": "projects/tyler-vhyo/locations/global/agent/sessions/aef3d36-18d696f9/contexts/session-vars",
        "lifespanCount": 54,
        "parameters": {
          "last-name.original": "",
          "email.original": "",
          "email": "",
          "given-name.original": "Tim",
          "Jobtype": "custom shower",
          "Jobtype.original": "custom shower",
          "given-name": "Tim",
          "last-name": ""
        }

I was wondering why it wasn't working unless I prompt for the email in that specific intent, and I realised it's because the email parameters are now all blank (and definitely populated before!) I don't want to reprompt for the email when the user already has to input it at the start of the conversation.

How can I solve this issue and get the email to send off the pre-existing email parameter?

Also, how can I get the ${agent.parameters.given-name} code to reference the name parameter correctly in the email body? It hasn't been working for me and I don't know if there is a better way to pull parameters in the email.

I'm an extreme novice at coding at the code above is something I made following a guide. I really do have no idea what is happening so any advice is very welcome and appreciated. Thank you in advance!

timtam222
  • 11
  • 2

1 Answers1

1

Managed to solve the issue. Steps that I think led to it working:

  • Added all the parameters to 'action and parameters' of the intent
  • Checkmarked 'Is list'
  • Added a default value for each parameter referring to itself from the earlier context in the form #session-vars.parametername

I don't understand 100% why it wouldn't work without the above steps, as the intent shouldn't modify or clear the parameters, but it seems to have done the trick.

timtam222
  • 11
  • 2