I have been trying to get input contexts to flow into dialogflow via node.js but i cant seem to get it working in any way. Here is what I have so far and sorry I am not at node so any pointers would be a massive help.
It doesnt fail or say there is an error I just never see the input contexts hit the back end.
Thanks for you time
Steve
async function createContext(sessionId, contextId, parameters, lifespanCount = 1) {
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
const contextsClient = new dialogflow.ContextsClient();
const contextPath = contextsClient.contextPath(
projectId,
sessionId,
contextId
);
const request = {
parent: sessionPath,
context: {
name: contextPath,
parameters: struct.encode(parameters),
lifespanCount: lifespanCount
}
};
const [context] = await contextsClient.createContext(request);
return context;
}
// hook it up to the question and answer through the chatbot api
async function detectIntent(
projectId,
sessionId,
query,
contexts,
languageCode
) {
// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
queryParams: {
contexts: [contexts],
}
};
const responses = await sessionClient.detectIntent(request);
return responses[0];
}
async function requestChatBot(textdata) {
let intentResponse;
let context = "";
const parameters = {
welcome: true
};
context = await createContext(callId, 'welcome-context', parameters);
intentResponse = await detectIntent(
projectId,
callId,
textdata,
context,
languageCode
);
}
modify your "request" to
const request = {
session: sessionPath,
queryParams: {
contexts: [{
name: 'projects/' + projectId + '/agent/sessions/' + sessionId + '/contexts/' + "Name of the context you want to pass",
lifespanCount: provide life span of your context (Integer Value),
}]
},
queryInput: {
text: {
// The query to send to the dialogflow agent
text: userQuery,
// The language used by the client (en-US)
languageCode: 'en-US',
},
},
};
This works.
Related
I would like to know how to create a context dialog, but I don't think anything about how to make this request... can anyone help me?
i'm using a lib sdk dialogflow
I searched how to do this, but I don't find anything, I don't know how to implement this..
me code
class DialogFlowController {
public dialogID: string
public sessionID: string
public sessionClient: SessionsClient
constructor () {
this.dialogID = 'chatbot-asxj'
this.sessionID = v4()
this.sessionClient = new dialogflow.SessionsClient({
credentials: {
type: config.gooogleType,
client_id: config.googleClientId,
private_key: config.googlePrivateKey,
client_email: config.googleCLientEmail,
token_url: config.googleTokenUrl
}
})
}
public async interactor (req: Request, res: Response): Promise<Response> {
console.log(teste)
const messagem = req.body.goMessage
try {
// Create a new session
const sessionPath = this.sessionClient.projectAgentSessionPath(this.dialogID, this.sessionID)
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: messagem,
// The language used by the client (en-US)
languageCode: 'en-US'
}
}
}
const response = await this.sessionClient.detectIntent(request)
Socket.io.emit('returnMessage', response[0].queryResult?.fulfillmentText)
return res.status(200).json()
} catch (err) {
return res.status(500).json(err)
}
}
}
When I call createClickwrap method, I got 404 error Not Found. If I run this method through Quickstart generated demo project, I don't get this error. However, if I run it through my project I get the error. If I debug the demo app and my app, the functions parameters are the same.
This is the code in my app:
docusign controller:
const docuSignService = require('./docusign_esign_service');
const demoDocumentsPath = path.resolve(__dirname, '../demo_documents');
const { createClickwrap } = require('./createClickWrap');
async getDocusignRecieptService() {
const authResponse = await docuSignService.authenticate();
if (authResponse) {
const docTermsPdf = 'Term_Of_Service.pdf';
const docFile = path.resolve(demoDocumentsPath, docTermsPdf);
const { basePath, accessToken, apiAccountId } = authResponse;
const { clickwrapId } = await createClickwrap({ docFile, basePath, accessToken, accountId: apiAccountId });
const res = await activateClickwrap({ clickwrapId, basePath, accessToken, accountId: apiAccountId });
console.log({ res });
}
}
docuSignService.js
const SCOPES = ['signature', 'impersonation', 'openid', 'click.manage', 'click.send'];
const fs = require('fs');
const docusign = require('docusign-esign');
class DocusingService {
async authenticate() {
const jwtLifeSec = 10 * 60, // requested lifetime for the JWT is 10 min
dsApi = new docusign.ApiClient();
dsApi.setOAuthBasePath(process.env.dsOauthServer.replace('https://', '')); // it should be domain only.
let rsaKey = fs.readFileSync(process.env.privateKeyLocation);
try {
const results = await dsApi.requestJWTUserToken(
process.env.dsJWTClientId,
process.env.impersonatedUserGuid,
SCOPES,
rsaKey,
jwtLifeSec
);
const accessToken = results.body.access_token;
// get user info
const userInfoResults = await dsApi.getUserInfo(accessToken);
// use the default account
let userInfo = userInfoResults.accounts.find((account) => account.isDefault === 'true');
return {
accessToken: results.body.access_token,
apiAccountId: userInfo.accountId,
basePath: `${userInfo.baseUri}/restapi`
};
} catch (e) {
let body = e.response && e.response.body;
// Determine the source of the error
if (body) {
// The user needs to grant consent
if (body.error && body.error === 'consent_required') {
if (this.getConsent()) {
return this.authenticate();
}
} else {
// Consent has been granted. Show status code for DocuSign API error
this._debug_log(`\nAPI problem: Status code ${e.response.status}, message body:
${JSON.stringify(body, null, 4)}\n\n`);
}
}
}
}
getConsent() {
var urlScopes = SCOPES.join('+');
// Construct consent URL
var redirectUri = 'https://developers.docusign.com/platform/auth/consent';
var consentUrl =
`${process.env.dsOauthServer}/oauth/auth?response_type=code&` +
`scope=${urlScopes}&client_id=${process.env.dsJWTClientId}&` +
`redirect_uri=${redirectUri}`;
throw new Error(`Open the following URL in your browser to grant consent to the application: ${consentUrl}`);
}
getArgs(apiAccountId, accessToken, basePath, signerEmail, signerName, id, agreementData, redirect_uri) {
const envelopeArgs = {
signerEmail: signerEmail,
signerName: signerName,
status: 'sent',
signerClientId: id,
dsReturnUrl: redirect_uri,
agreement: agreementData
};
const args = {
accessToken: accessToken,
basePath: basePath,
accountId: apiAccountId,
envelopeArgs: envelopeArgs
};
return args;
}
}
module.exports = new DocusingService();
createClickWrap.js
const createClickwrap = async ({ docFile, clickwrapName = 'clickwrapName', basePath, accessToken, accountId }) => {
// Step 3. Construct the request Body
// Create display settings model
const displaySettings = docusignClick.DisplaySettings.constructFromObject({
consentButtonText: 'I Agree',
displayName: 'Terms of Service',
downloadable: true,
format: 'modal',
hasAccept: true,
mustRead: true,
requireAccept: true,
documentDisplay: 'document'
});
// Create document model
// Read and encode file. Put encoded value to Document entity.
// The reads could raise an exception if the file is not available!
const documentPdfExample = fs.readFileSync(docFile);
const encodedExampleDocument = Buffer.from(documentPdfExample).toString('base64');
const document = docusignClick.Document.constructFromObject({
documentBase64: encodedExampleDocument,
documentName: 'Terms of Service',
fileExtension: 'pdf',
order: 0
});
// Create clickwrapRequest model
const clickwrapRequest = docusignClick.ClickwrapRequest.constructFromObject({
displaySettings,
documents: [document],
name: clickwrapName,
requireReacceptance: true
});
// Step 4. Call the Click API
const dsApiClient = new docusignClick.ApiClient();
dsApiClient.setBasePath(basePath);
dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + accessToken);
const accountApi = new docusignClick.AccountsApi(dsApiClient);
// Create a clickwrap
let result = null;
try {
result = await accountApi.createClickwrap(accountId, {
clickwrapRequest
});
} catch (e) {
debugger;
console.log(e);
}
debugger;
console.log(`Clickwrap was created. ClickwrapId ${result.clickwrapId}`);
return result;
};
module.exports = { createClickwrap };
Parameters look like this in the demo app and it works:
and these are the parameters in my app:
The first parameter accountId is the same. Why I am getting this issue in my app if function gets the same parameters?
"Error: Not Found
at Request.callback (/Users/and/test/node_modules/docusign-click/node_modules/superagent/lib/node/index.js:696:15)
at IncomingMessage.<anonymous> (/Users/and/test/node_modules/docusign-click/node_modules/superagent/lib/node/index.js:906:18)
at IncomingMessage.emit (node:events:539:35)
at IncomingMessage.emit (node:domain:475:12)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)"
Thanks to pointing out in the comments, when I changed basePath ${userInfo.baseUri}/restapi to ${userInfo.baseUri}/clickapi, it works now.
I want to set different session for different users in dialogflow.
https://www.npmjs.com/package/dialogflow
I referred to the following site for this, but the program is not returning any output.
const dialogflow = require('dialogflow');
const uuid = require('uuid');
/**
* Send a query to the dialogflow agent, and return the query result.
* #param {string} projectId The project to be used
*/
async function runSample(projectId = 'your-project-id') {
// A unique identifier for the given session
const sessionId = uuid.v4();
// Create a new session
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: 'hello',
// The language used by the client (en-US)
languageCode: 'en-US',
},
},
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log('Detected intent');
const result = responses[0].queryResult;
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
}
const functions = require('firebase-functions');
const dialogflow = require('dialogflow');
const cors = require('cors')({origin: true});
const admin = require('firebase-admin');
admin.initializeApp();
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.connectChat = functions.https.onRequest((req, response) => {
cors(req, response, () => {
const projectId = '******';
const query = req.query.text;
const session= req.query.session;
const languageCode = 'en-US';
var result="";
let privateKey = "-----BEGIN PRIVATE KEY-----******\n-----END PRIVATE KEY-----\n";
// as per goolgle json
let clientEmail = "*****#.gserviceaccount.com";
let config = {
credentials: {
private_key: privateKey,
client_email: clientEmail
}
}
const sessionClient = new dialogflow.SessionsClient(config);
// Define session path
const sessionPath = sessionClient.sessionPath(projectId, session);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
// Send request and log result
sessionClient
.detectIntent(request)
.then(responses => {
result = responses[0].queryResult;
response.send(result);
return null;
})
.catch(err => {
console.error('ERROR:', err);
response.send('ERROR');
return null;
});
});
});
projectId,clientEmail,privateKey are get from JSON file downloaded from https://console.cloud.google.com/iam-admin
create your session id (req.query.session) from client side in javascript
I'm using dialogflow v2 using npm. I just want call a welcome event in dialogflow. How can I do it in the nodejs. I'm pretty new to it.
This is my code
const projectId = "xxxxxx";
const LANGUAGE_CODE = 'en-US';
const sessionId = req.body.sessionId;
var query = req.body.query;
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId,sessionId);
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: LANGUAGE_CODE,
},
},
};
sessionClient.detectIntent(request).then(response => {
console.log('intent detected');
const result = response[0].queryResult;
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if(result.fulfillmentText) {
console.log(result.fulfillmentText);
return res.json({reply: result.fulfillmentText})
}
// if(result.intent) {
// console.log(` Intent: ${result.intent.displayName}`)
// }
else {
console.log('no intent found');
}
}).catch(err => {
console.log('error '+err);
})
As I open the chat page I just want to throw a welcome message. In order to do that, i read that i have to call the event. How can I do that? I have taken the reference from here
The Request body should be like this:
let eventName='WELCOME'; //name of the event
let request = {
session: sessionPath,
queryInput: {
event: {
name: eventName,
languageCode: 'en-US'
},
},
};
checkout- https://github.com/googleapis/nodejs-dialogflow/blob/master/samples/detect.js#L96
let me know if you find any difficulties :)
I would suggest using actions-on-google as it is much easier to build nodejs backend for dialogflow link : actions on google
And for sample project refer Number genie project
Hope this would help you.
How to trigger an intent in Dialogflow?I need to trigger an intent without response from user. I know we need to call an event here, but don't know how to do the same in V2 api?
You trigger event from dialogflow, it's as easy to detectIntent
const dialogflow = require('dialogflow');
const config = require('../config');
// Import the JSON to gRPC struct converter
const credentials = {
client_email: config.GOOGLE_CLIENT_EMAIL,
private_key: config.GOOGLE_PRIVATE_KEY,
};
const sessionClient = new dialogflow.SessionsClient(
{
projectId: config.GOOGLE_PROJECT_ID,
credentials
}
);
module.exports = {
async sendEventToDialogFlow(event, params = {}) {
const sessionPath = sessionClient.sessionPath(config.GOOGLE_PROJECT_ID, sessionId);
const request = {
session: sessionPath,
queryInput: {
event: {
name: event,
languageCode: config.DF_LANGUAGE_CODE,
},
}
};
const responses = await sessionClient.detectIntent(request);
return responses[0].queryResult;
}
}