Интеграция DIalogflow и Gupshup - PullRequest
1 голос
/ 09 мая 2020

Я разрабатываю чат-бота в Dialogflow. Теперь мне нужно интегрировать его с Whatsapp.

Я хотел бы выполнить официальную интеграцию с WhatsApp с помощью Gupshup. Но я не мог найти способ сделать это. Кто-нибудь когда-нибудь делал эту интеграцию? Мог бы поделиться кодом и как это сделать?

1 Ответ

0 голосов
/ 18 мая 2020

Здесь вы можете передать sessionId и запрос, в моем случае передавая источник (номер телефона) и сообщение, полученное с URL-адреса обратного вызова gupshup. Надеюсь, вы знаете настройку API диалогового потока.

 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 
       }
   })   

/ * ------------ 'Код диалогового потока' --------------- * /

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

  // }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...