0

I'm having problems setting up a webhook to work with a function that sends an email. I want to map the Reminder.add intent to the function 'email'. It works well with the appointmentSetter and createCalendarEvent but can't get it to work to send emails. Running the webhook on ngrok.

Here is the webhook:

const express = require('express');
const app = express();
const df = require('dialogflow-fulfillment');
const {google} = require('googleapis'); 


const PORT = process.env.PORT || 3000;


// setup calendar Id and service account json
const calendarId = "";
const serviceAccount = {
    
  };

// setup service account credentials

const serviceAccountAuth = new google.auth.JWT({
    email : serviceAccount.client_email, 
    key : serviceAccount.private_key,
    scopes : 'https://www.googleapis.com/auth/calendar'
});

const calendar = google.calendar('v3');


// const timeZoneOffset = '+03:00';

app.listen(PORT ,()=>{
    console.log(`server is running on port:${PORT}`);
});

app.post('/webhook', express.json(),(request, response) =>{
    const agent = new df.WebhookClient({
        request: request,
        response: response
    });
    

    function createCalendarEvent(email, event, startTime, endTime){
        console.log(`startTime: ${startTime}`);
        console.log(typeof startTime);
        console.log(`endTime: ${endTime}`);
        console.log(typeof endTime);

        return new Promise((resolve , reject) =>{
            calendar.events.list({
                auth : serviceAccountAuth,
                calendarId : calendarId,
                timeMin : startTime.toISOString(),
                timeMax : endTime.toISOString(),
            },(err, res) =>{
                if(err || res.data.items.length > 0){
                    reject(`The appointment conflicts with another scheduled client, can you try set another date and time please?`);
                }
                else{
                    calendar.events.insert({
                        auth : serviceAccountAuth,
                        calendarId : calendarId,
                        resource : {
                            summary : event + ':' + ' ' + 'First Contact',
                            description : 'email:' + ' ' + email,
                            start : {dateTime : startTime},
                            end : {dateTime : endTime}
                        }
                    },(err, events) =>{
                        err ? reject(err) : resolve(events);
                    });
                }
            });

        });
       
    }


    function appointmentSetter(agent){

        let event = agent.parameters.name;
        let email = agent.parameters.email;
        let date = agent.parameters.date;
        let time = agent.parameters.time;
        
        console.log(typeof date);
        console.log(` this is the date variable ${date}`);
        console.log(typeof time);
        console.log(typeof event);
        console.log(typeof email)

        let startTime = new Date(Date.parse(date.split('T')[0] + 'T' + time.split('T')[1].split('+')[0]));
        let endTime = new Date(new Date(startTime).setHours(startTime.getHours()+1));

        console.log(`StartTime: ${startTime}`);
        console.log(`EndTime: ${endTime}`);
        console.log(typeof startTime);
        console.log(typeof endTime);

        return createCalendarEvent(email, event, startTime, endTime).then(res => { 
            agent.add(`${event}, thank you for your time! The appointment has been set for ${date.split('T')[0]} at ${time.split('T')[1]}. 
            In order to facilitate the speed at which we can help you can you provide us with some more details?`)
        })
        .catch(err =>{
            agent.add(err);
        })

       

    }

    function email(agent) {
        
        const email = agent.parameters.email;
        const name = agent.parameters.name;
      
        const nodemailer = require('nodemailer');
      
        const transporter = nodemailer.createTransport({
          service: 'gmail',
          auth: {
            user: 'example@gmail.com', 
            pass: '*******'
          },
        });
      
        const mailOptions = {
          from: 'example@gmail.com', 
          to: 'example@gmail.com', 
          subject: 'Demo chatbot Customer Service', 
          html:
            '<p> Name:</p>' +
            name +
            '<p> Email:</p>' +
            email,
        };
      
        transporter.sendMail(mailOptions, function (err, info) {
          if (err) {
            console.log(err);
          } else {
            console.log('Email sent: ' + info.response);
          }
        });
       
    } 

    var intentMap = new Map();
    
    
    //setting up the email function to work with the same Dailogflow intent

type here


    intentMap.set('Reminder.add', (agent) => {
    appointmentSetter(agent).then(()=>{
            email(agent);
        }).catch(err =>{
            agent.add(err);
         });
     });   
    agent.handleRequest(intentMap);
});

`

THIS IS THE ERROR THE NODE.JS SERVER KEEPS THROWING AT ME

throw new Error(No responses defined for platform: ${requestSource}); ^

Error: No responses defined for platform: DIALOGFLOW_CONSOLE

`type here`
Selah
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 24 '23 at 03:42

0 Answers0