我正在 Dialogflow 中开发一个聊天机器人。现在我需要将它与 Whatsapp 集成。
我想使用 Gupshup 与 whatsapp 进行官方集成。但我找不到办法做到这一点。有人做过这种整合吗?可以分享代码以及如何做到这一点?
我正在 Dialogflow 中开发一个聊天机器人。现在我需要将它与 Whatsapp 集成。
我想使用 Gupshup 与 whatsapp 进行官方集成。但我找不到办法做到这一点。有人做过这种整合吗?可以分享代码以及如何做到这一点?
在这里,您可以传递 sessionId 和查询,在我的情况下传递源(电话号码)和从 gupshup 回调 url 获取的消息。我希望你知道 dialogflow 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
// }
}