首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DIalogflow和Gupshup集成

DIalogflow和Gupshup集成
EN

Stack Overflow用户
提问于 2020-05-10 00:12:29
回答 1查看 494关注 0票数 1

我正在用Dialogflow开发一个聊天机器人。现在我需要将它与Whatsapp集成。

我想使用Gupshup与whatsapp进行官方集成。但是我找不到这样做的方法。有人做过这样的集成吗?可以分享代码,如何做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2020-05-18 20:07:33

在这里,您可以传递sessionId和查询,在我的示例中,传递的是源(电话号码)。以及从gupshup回调url获得的消息。我希望你知道dialogflow api的设置。

代码语言:javascript
复制
 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 
       }
   })   

/*-‘对话流代码’- */

代码语言:javascript
复制
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

  // }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61699865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档