Здесь вы можете передать 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
// }
}