1

I'm developping a chatbot in Dialogflow. Now i need to integrate it with Whatsapp.

I would like to do the official integrations with whatsapp using Gupshup. But i couldnt find a way to do this. Someone have ever did this integration? Could share the code and how to do this?

TT bots
  • 11
  • 1

1 Answers1

0

Here you can pass sessionId and query, in my case passing source(phone no.) and message which is getting from gupshup callback url. I hope you know dialogflow api setup.

 await executeQueries(sessionId , query).then(async (result): Promise<any> => {
      if (!result.intent.isFallback) {
         // Here you get dialogFlow fulfillmentText
        response.send(result.fulfillmentText)
        }
      else {
         // here you can handle Fallback 
       }
   })   

/*------------'Dialogflow Code'--------------- */

const serviceAccountAPI = require('./service-account.json');
const dialogflow = require('dialogflow');
const project_Id = serviceAccountAPI.project_id;

const sessionClient = new dialogflow.SessionsClient({ credentials: serviceAccountAPI });

async function detectIntent(
  sessionId: string,
  query: string,
  contexts: string | any[] | undefined,
  languageCode: string
) {
  // The path to identify the agent that owns the created intent.
  const sessionPath = sessionClient.sessionPath(project_Id, sessionId);

  // The text query request.
  const request: any = {
    session: sessionPath,
    queryInput: {
      text: {
        text: query,
        languageCode: languageCode,
      },
    },
  };

  if (contexts && contexts.length > 0) {
    request.queryParams = {
      contexts: contexts,
    };
  }

  const responses = await sessionClient.detectIntent(request);
  return responses[0] ? responses[0] : true;
}

async function executeQueries(sessionId: string, query: string) {
  const languageCode = 'en-US';
  // Keeping the context across queries let's us simulate an ongoing conversation with the bot
  let context;
  let intentResponse;
  // for (const query of queries) {
  try {
    console.log(`Sending Query: ${query}`);
    intentResponse = await detectIntent(
      sessionId,
      query,
      context,
      languageCode
    );
    // console.log('Detected intent: ' + JSON.stringify(intentResponse.queryResult, null, 4));
    context = intentResponse.queryResult.outputContexts;

    console.log('Is Fallback: ' + intentResponse.queryResult.intent.isFallback);
    // console.log(`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`);
    // Use the context from this response for next queries
  } catch (error) {
    console.error(error);
  }
  return intentResponse.queryResult

  // }
}
Alam
  • 303
  • 1
  • 3
  • 14
  • Thanks Alam! Did you developed a Weebhook to integrate in this way? or are you sing the Gupshup IDE option? – TT bots May 18 '20 at 18:30
  • Actually did by using gupshup whatsapp access api and implemented nodejs cloud function https://www.gupshup.io/developer/docs/bot-platform/guide/whatsapp-api-documentation – Alam May 19 '20 at 03:03
  • Ohh. Perfect! I'm using the same tools. But i'm having a problem with sintax... I changed the static type fo Flow but i'm still having problems when i'm deployng to Firebase functions... My code can't understand some configuration like: Promise, const request: any = {...}, async function detectIntent(sessionID: string,...) .... Why are you using this configuration and how can i use it too? – TT bots May 19 '20 at 13:39