Dialogflow Context - dialogflow-es

I got some problem, I can't access my parameters from context on dialogflow, i just trying using agent.getContext and agent.context.get but still not work.
there is my code for set the context
function noTelp(agent){
const telp = agent.parameters.phoneNumber;
let query = db.collection('pelanggan').where('no_telp','==',telp);
return query.get().then(snapshot => {
if (snapshot.empty) {
agent.add('Mohon Maaf data no telepon '+telp+' tidak ditemukan');
agent.add('untuk menambahkan data kamu silahkan tuliskan nama kamu');
agent.setContext({ >set the context
name : 'tambahData',
lifespan : 2,
parameters : {noTelp : telp}
});
console.log('No matching documents.');
return;
}
}
and this for the calling the context
function tambahData(agent){
const context = agent.getContext('tambahData'); >get the context
const telp = context.parameters.noTelp; >get the parameters from context
const nama = agent.parameters.nama;
agent.add(nama+telp); >test calling parameters
}

Used a consistent method either from V1 or V2. You can modify the code as below, it will work. I managed to work like this only.
Setting context:
agent.context.set({
name: 'global_main_context',
lifespan: 5,
parameters: param
});
Getting Context
let globalContext = agent.context.get('global_main_context');
I would suggest to keep updating the context in each of transaction because it as lifespan that will automatically kill that context if you cross a number of transactions.

Related

undefined parameter received in Bixby function

I'm trying to process an utterance in the format "Get News from Impeachment Sage" where Impeachment Sage corresponds to an enum of publication names. Bixby is successfully understanding the utterance and trying to call my goal (GetNewsByName) but the trained Value is not arriving at the function. (This is based off the user persistence data example).
The operative portion of the function is thus:
function getNewsByName(altBrainsNames) {
// const name = "Impeachment Sage" //hard coded for testing
const url = properties.get("config", "baseUrl") + "altbrains"
console.log("i got to restdb.js and the url is ", url);
console.log("AltBrainsNames is", altBrainsNames)
const query = {
apikey: properties.get("secret", "apiKey"),
q: "{\"" + "name" + "\":\"" + name + "\"}"
// q: "{}"
}
console.log("query", query)
const options = {
format: "json",
query: query,
cacheTime: 0
}
const response = http.getUrl(url, options)
if (response) {
const content1 = response
// const altBrainsData = response[0][properties.get("config", "altbrainsName")]
// altbrainsData.$id = response[0]["_id"]
console.log('content1', content1);
console.log('identifier', content1)
return content1
} else {
// Doesn't exist
console.log('doesnae exist');
return
}
}
What is happening here where the Value is not reaching the function?
The Action model is:
action (GetNewsByName) {
description ("Get news data from remote Content db by searching on AltBrain name")
type (Calculation)
output (Content)
collect {
input (altBrainsNames) {
type (AltBrainsNames)
min (Required) max (One) //this means js must catch error when multiple names offered
}
}
}
We resolved this offline, just wanted to follow up on the public channel to any fellow Bixby developers seeing this question posted. The function that calls 'getNewsByName' needs to receive the input parameter. Once populated, the action worked successfully.

ES6 : Object restructuration for mailchimp api

I want to construct a object base on an array and another object.
The goal is to send to mailchimp api my users interests, for that, I've got :
//Array of skills for one user
const skillsUser1 = ["SKILL1", "SKILL3"]
//List of all my skills match to mailchimp interest group
const skillsMailchimpId = {
'SKILL1': 'list_id_1',
'SKILL2': 'list_id_2',
'SKILL3': 'list_id_3',
}
//Mapping of user skill to all skills
const outputSkills = skillsUser1.map((skill) => skillsMailchimpId[skill]);
console.log(outputSkills);
The problem is after, outputSkill get me an array :
["ID1", "ID3"]
But what the mailchimp api need, and so what I need : :
{ "list_id_1": true,
"list_id_2": false, //or empty
"list_id_3" : true
}
A simple way would be this (see comments in code for explanation):
// Array of skills for one user
const skillsUser1 = ["SKILL1", "SKILL3"]
// List of all my skills match to mailchimp interest group
const skillsMailchimpId = {
'SKILL1': 'list_id_1',
'SKILL2': 'list_id_2',
'SKILL3': 'list_id_3',
}
// Create an output object
const outputSkills = {};
// Use `Object.entries` to transform `skillsMailchimpId` to array
Object.entries(skillsMailchimpId)
// Use `.forEach` to add properties to `outputSkills`
.forEach(keyValuePair => {
const [key, val] = keyValuePair;
outputSkills[val] = skillsUser1.includes(key);
});
console.log(outputSkills);
The basic idea is to loop over skillsMailchimpId instead of skillsUser.
But that is not very dynamic. For your production code, you probably want to refactor it to be more flexible.
// Array of skills for one user
const skillsUser1 = ["SKILL1", "SKILL3"]
// List of all my skills match to mailchimp interest group
const skillsMailchimpId = {
'SKILL1': 'list_id_1',
'SKILL2': 'list_id_2',
'SKILL3': 'list_id_3',
}
// Use `Object.entries` to transform `skillsMailchimpId` to array
const skillsMailchimpIdEntries = Object.entries(skillsMailchimpId);
const parseUserSkills = userSkills => {
// Create an output object
const outputSkills = {};
// Use `.forEach` to add properties to `outputSkills`
skillsMailchimpIdEntries.forEach(([key, val]) => {
outputSkills[val] = userSkills.includes(key);
});
return outputSkills;
}
// Now you can use the function with any user
console.log(parseUserSkills(skillsUser1));

dialogflow with node.js how do you swtich intents

I have an intent that has a required parameter and a fulfilment. When you answer it takes you to my node.js application which currently looks like this:
app.intent(QUESTIONS_INTENT, (conv, params) => {
let data = conv.data;
let categoryId = data.categoryId;
let formulas = data.formulas;
let options = {
'method': 'PUT',
'url': apiUrl + 'questions/filter',
'body': {
categoryId: categoryId,
organisationId: organisation,
formulas: formulas
},
'json': true
};
return request(options).then(response => {
// We need to change how we get these
var questionText = params.questions;
var questions = response.filter(item => item.text === questionText);
data.questions = questions;
conv.ask(questions[0].text);
conv.contexts.set('colour');
}, error => {
conv.ask(JSON.stringify(error));
});
})
Currently it gets the questionText and finds the question that matches what you said. What I want it to do is to swap to a new intent. I have tried by conv.contexts.set('colour') but that doesn't appear to work.
I have an intent setup with input context as "Colour" so I would expect that when my fulfilment completes, it should swap to that intent, but it doesn't.
Can someone help me with this?
You need to make sure that you have a colour intent setup in DialogFlow that will handle the input from the user and respond to them. The question should be asked after you set the context.
return request(options).then(response => {
// We need to change how we get these
var questionText = params.questions;
var questions = response.filter(item => item.text === questionText);
data.questions = questions;
// Sets the context to colour.
conv.contexts.set('colour', 1);
// Now that the context is in control you'll be able to ask a question.
conv.ask(questions[0].text);
You'll also want to provide a lifespan after the context arg. This will make the context active for that many prompts, in the example above I set it for one so your question context will only be active for that specific response.
conv.contexts.set('colour', 1);

Google Cloud Talent Solution fetch a job by requisitionId

I am wondering if it is possible to fetch a job by requisitionId in Google Cloud Talent Solution. requisitionId has to be unique across jobs so it seems like a natural candidate for looking a job up.
When a job is created the api returns a job name that can be used to look the job up:
You can retrieve the details of a previously inserted job by sending a GET request to the Cloud Talent Solution. The URI should include the previously inserted job name returned by the original create request, as a URL parameter.
I'd like to avoid storing these names if possible. In my view storing them adds unnecessary complexity since I already have a unique requisitionId. To be clear the API does not let you add jobs with a duplicate requisitionId:
Job projects/{my_app_id}/jobs/{google_assigned_id} already exists. Request ID for tracking: ... Related Job requisition ID: ...
So can I look up jobs by requisitionId?
I could parse the error message that's returned to get the job name..but that seems pretty brittle.
It turns out the list method takes requisitionId so for a full, read-create-update cycle we can do:
const listRequest = {
parent: `projects/${projectId}`,
'filter': `companyName="${companyName}" AND requisitionId="${requisitionId}"`
}
const listResult = await jobService.projects.jobs.list(listRequest)
const existingJobs = listResult.data.jobs || [];
let existingJob = null
if (existingJobs && existingJobs.length > 0) {
existingJob = existingJobs[0]
}
let googleJob
if (!existingJob) {
const createRequest = {
'parent': `projects/${projectId}`,
'resource': {
'job': {
companyName,
requisitionId,
title,
description,
applicationInfo: {
emails: ['email#example.com']
}
}
}
}
googleJob = await jobService.projects.jobs.create(createRequest)
.then(result => result)
.catch(resp => {
console.error("ERROR")
console.error(resp)
})
} else {
const patchRequest = {
'name': existingJob.name,
'resource': {
'job': {
companyName,
requisitionId,
title,
description,
applicationInfo: {
emails: ['email#example.com']
}
}
}
}
googleJob = await jobService.projects.jobs.patch(patchRequest)
.then(result => result)
.catch(resp => {
console.error("ERROR")
console.error(resp)
})
}
Docs: https://cloud.google.com/talent-solution/job-search/docs/reference/rest/v3/projects.jobs/list?authuser=0&hl=de
Notes:
The double quotes in the filter parameter are important. It will not accept single quotes and will give a cryptic error message.
The patch request cannot take a parent parameter even though everything else requires a parent parameter...
one can add it as custom attribute:
Map<String, CustomAttribute> attributes = new HashMap<>();
attributes
.put("requisitionId", new CustomAttribute().setStringValue(requisitionId)
.setFilterable(true));
Job job = new Job()
...
.setCustomAttributes(attributes);
Job jobCreated = createJob(job);
String jobName = jobCreated.getName();
and then search for requisitionId with a custom attribute filter:
JobQuery jobQuery = new JobQuery().setCustomAttributeFilter(filter);
this is a little redundant, but JobQuery has no method .setRequisitionId().
here's the documentation.

How to pass params from one intent to another dialogflow

how does one pass the params from a intent in the fulfillment editor to another followup intent.
Here is my code
// original intent
// the params defined here are the ones I want to pass to the app.intent below.
app.intent('Book Appointment', (conv, params) => {
// the params that i want to access in the next intent
const date = params['date'];
const time = params['time'];
const firstName = params['given-name'];
const email = params['email'];
// what i dont get is what to put in here is it
// how am i formatting this?
const parameters = {'date':params.date,}
// is the 'welcome-context' replaced with this one from the JSON? like so?
conv.contexts.set('projects/myproject-120c2/agent/sessions/c2c1c9e0-2ccf-0cf0-d7ce-e52561d44de3/contexts/bookappointment-followup"', 5, parameters);
// or is it
conv.contexts.set('bookappointment-followup', 5, parameters); // ??
// like it is in Dialogflow
})
// follow-up intent
app.intent('Book Appointment - yes', (conv, params) => {
// this is where I want to access the ones above
// then im accessing them like this?
const conv.contexts.get('bookappointment-followup').parameters['whatever it is above'];
})
The variables in the email return undefined. I reckon it's something to do with the contexts but I dont know if its the same code like #book.appointment.follow.date etc.
Help is appreciated, cheers
You may check my answer here
Use the output context to save parameters
{
"fulfillmentText":"This is a text response",
"fulfillmentMessages":[ ],
"source":"example.com",
"payload":{
"google":{ },
"facebook":{ },
"slack":{ }
},
"outputContexts":[
{
"name":"<Context Name>",
"lifespanCount":5,
"parameters":{
"<param name>":"<param value>"
}
}
],
"followupEventInput":{ }
}
If you are using NodeJS client
You can save context with parameters like
let param1 = [];
let param2 = {};
let ctx = {'name': '<context name>', 'lifespan': 5, 'parameters': {'param1':param1, 'param2': param2}};
agent.setContext(ctx);
and get it like
let params = agent.getContext("<context name>").parameters;
let param1 = params.param1;
let param2 = params.param2;
If using conv, you may also try like this:
app.intent('<INTENT>', conv => {
conv.ask('<RESPONSE>');
const parameters = {'param1':param1, 'param2': param2}};
conv.contexts.set('welcome-context', 5, parameters);
});
and access it like here :
const conv.contexts.get(<context>).parameters[<param>];
If via context doesn't work, you may try with data like
conv.data.someProperty = 'someValue'
as stated here
Thanks this worked.
const firstName = conv.contexts.get('bookappointment-followup').parameters['given-name'];
:)

Resources