How to create parameters in dialogflow using node.js - javascript

I want to create a parameter in node js that stays in the same intent for a life spam of 2.
It should ask for the first value then validates it, then second value and so on
what I have done
function test(agent) {
var first = agent.parameters.first;
var second = agent.parameters.second;
agent.context.set({
name: 'test',
lifespan: 2,
parameters: {
'first': first,
'second': second
},
});
agent.add("first parameter then second parameter");
}
Should I create the parameters in dialogflow as follows and implement it in node js or it should be done all in node js. If so how can I do it?
appreciate your help

I would say that in general the best option is to set all the intent's parameters via Dialogflow and then interact via Node.js client library.
Actually it would be also possible to do everything with Node.js, but in case you are using (or may use in the future) one of the integration options using fulfillment to manage agent’s responses you should set a webhook service and manage responses via API.
Therefore, I think setting the parameters in Dialogflow before interacting with Node.js would be the simplest and straightforward approach for any use case.

The code works fine, incase anyone facing the same issue, the request being sent must have the same session id to avoid overriding the values.

Related

Jumping over to another intent by using the 'agent.setFollowupEvent(targetIntentEventName)' method in the DialogFlow Messenger fulfillment

I'm trying to find a solution to launch another DialogFlow Intent (via its event) from a webhook (node.js) server after collecting users email address and checking their registration status by making a POST API call to a remote organization server.
Here's a basic schema:
User: Hello!
Chatbot: Hello, I'm an assistance bot. Can I have your email address to verify your registration status, please?
User: funnyname#email.com
Chatbot: Thank you, let me check your status ...
---> Fulfilment code makes a POST API call and gets users status back from a server if there's a match.
Here based on the users status I want to launch a new intent: either Registered_Users_Intent or New_Users_Intent. I tried to use the agent.setFollowupEvent(({ "name": "targetIntentEventName" }) method, but it doesn't trigger a desired event[intent] all the time. I'm not sure how to set it up properly in the fulfilment code to make it work all the time.
Would anyone have any suggestions or reference materials to make this set-up work, please?
When you make a webhook fulfillment request, your API should respond with a JSON response that Dialogflow understands. Instead of calling dialogflow APIs to trigger an event from within your webhook API, the JSON response of the API can contain the name of the event to be triggered next and Dialogflow will do the rest.
Here's the reference for the fulfillment response to trigger a follow-up event: https://cloud.google.com/dialogflow/es/docs/fulfillment-webhook#event
Also, the most likely reason that your current approach works sometimes and doesn't the others, is because triggering an event using dialogflowv2 APIs AND sending back a JSON response (which probably doesn't have the name of the follow up event mentioned) creates a race condition.
Since you are using the Dialogflow Fulfillment Library, make sure to use the latest version in your package.json file. Also, when using the agent.setFollowupEvent() method, make sure to include an agent.add() method for the response. This response will not be returned but is a requirement to be able to use the agent.setFollowupEvent() method.
Here’s an example:
function nameOfFunction(agent) {
agent.add(`sample response`);
agent.setFollowupEvent('event_name');
}
For more information on Dialogflow Fulfillment Library, see here. Note that this library is not maintained.
Otherwise, you may create your own code and host it on your chosen web server. From there, you can invoke events by setting the followupEventInput field of the WebhookResponse. Check here for more information.

Hide/Disable firebase functions for client

I am new to firebase and wondering how to disable specific functions for the client to manually put in the browsers console.
Example:
function createRoomDB(roomID, name, mode, start, length, aname, opcount, secrettoken) {
firebase.database().ref('rooms/' + roomID).set({
name: name,
mode: mode,
start: start,
length: length,
aname: aname,
opcount: opcount,
secrettoken: secrettoken
});
}
(The names have nothing to do with my question.)
Long story short: I don't want users to simply use this command to create new data. I know that you can't hide code on front-end, but what are the easiest and most efficient ways to disable this hell of a backdoor?
I am planning to host this application on GitHub pages.
Since your code can access the database, there is no way to prevent other code that runs on the same environment to also access the database.
This means you have two options:
Make sure all code (no matter who wrote it) only can perform authorized operations on the database.
Run the code in a different environment.
For the first option, you'll want to look into Firebase security rules, which automatically run server-side and can enforce most requirements.
For the second option, you could for example run the code in Cloud Functions for Firebase, and call that from your API. This allows you to hide any secret values and code in a trusted environment, but does mean that you'll need to ensure only authorized users can call that Cloud Function.

Meteor Random package vs randomSeed

I'm trying to replicate the latency compensation done by Meteor and minimongo. For instance to create an id on the client and then the same id on the server after calling the method, so the client can update the UI without waiting for the server response.
For this I need to generate the same Id on both the client and the server.
So, in meteor if I do: Random.createWithSeeds('abc').id()
I always get:
WKrBPwCSbzNHmhacn
But if I connect from and external app, outside of metor using a ddp client:
self.send({msg: 'method', id:id, randomSeed: 'abc', method: name, params: params});
I get a different Id. It's repeatable, but not the same as the one generated by Random. Why?
I cannot understand. Are they using a different generationId algorithm?
Packages I'm using:
On Meteor: https://atmospherejs.com/meteor/random
On external Client (outside Meteor): https://github.com/eddflrs/meteor-ddp + source code of random.js
This may not be a complete answer (I'm still looking too), but the way you're using Random.createWithSeeds should read:
> let generator = Random.createWithSeeds('abc')
> generator.id()
'WKrBPwCSbzNHmhacn'
> generator.id()
'h6iLWkdEfZ7wXWpPQ'
Perhaps an edit might clarify that createWithSeeds('abc') is supposed to return an object you call .id() on multiple times. I've never tried passing the seed from another ddp client though, and I'll let you know when I do

How to make per user base logging with hapi js

I am using winston logging framework and logging on basis of log level, but now i am facing difficulties in tracking down bugs. So we decided to make logging on per user basis, and this is where i ran into problem.
What i want to acheive?
log file for every user will be generated on every hour. (We can skip every hour constraint in this thread) and every user has unique identifier 'uid'.
What i have?
I have followed architecture as used here 'https://github.com/agendor/sample-hapi-rest-api'. Some additional lib modules exist too.
Currently i am using winston library (but i can afford to replace this if needed).
Brief introduction of flow
Currently, i have access to request object in handler function only, but i want to log events in DAO, library functions too ( on per user basis). 'Uid' is available to me in handler function in request object as i put uid in request in authentication middleware.
My solution (which is not elegant)
pass request object ( or only uid) to every function and log (using winston) event. Custom transport will determine where (in which file, on basis of uid) to put the log.
Certainly, this is not elegant way as every function must have uid parameter in order to log event, which seems bad.
What i want from you?
A better, elegant approach which is scalable too.
Related post: https://github.com/hapijs/discuss/issues/51
Try taking a look at Continuation-Local Storage:
https://github.com/othiym23/node-continuation-local-storage
Heres a good article on implementing it within express:
https://datahero.com/blog/2014/05/22/node-js-preserving-data-across-async-callbacks/

How to customize the OData server using JayData?

I'm quite new to JayData, so this may sound like a stupid question.
I've read the OData server tutorial here: http://jaydata.org/blog/install-your-own-odata-server-with-nodejs-and-mongodb - it is very impressive that one can set up an OData provider just like that. However the tutorial did not go into details about how to customize the provider.
I'd be interested in seeing how I can set it up with a custom database and how I can add a layer of authentication/authorization to the OData server. What I mean is, not every user may have permissions to every entity and not every user has the permission to add new entities.
How would I handle such use cases with JayData?
Thanks in advance for your answers!
UPDATE:
Here are two posts that will get you started:
How to use the odata-server npm module
How to set up authentication/authorization
The $data.createODataServer method frequently used in the posts is a convenience method that hides the connect/express pipleline from you. To interact with the pipeline examine the method body of $data.createODataServer function found in node_modules/odata-server folder.
Disregard text below
Authentication must be solved with the connect pipeline there are planty of middleware for that.
For authorization EntityContext constructor accepts an authorization function that must be promise aware.
The all-allow authorizator looks like this.
function checkPerm(access, user, entitysets, callback) {
var pHandler = new $data.PromiseHandler();
var clbWrapper = pHandler.createCallback(callback);
var pHandlerResult = pHandler.getPromise();
clbWrapper.success(true); // this grants a joker rw permission to everyone
//consult user, entitySet and acces to decide on success/error
//since you return a promise you can call async stuff (will not be fast though)
return pHandlerResult;
}
I have to consult with one of the team members on the syntax that let you pass this into the build up process - but I can confirm this is doable and is supported. I'll get back with the answer ASAP.
Having authenticated the user you can also use EntityContext Level Events to intercept Read/Update/Create/Delete operations.
$data.EntityContext.extend({
MySet: { type: $data.EntitySet, elementType: Foobar,
beforeDelete: function(items) {
//if delete was in batch you'll get multiple items
//check items here,access this.request.user
return false // deny access
}
});
And there is a declarative way, you can annotate Role names with permissions on entity sets, this requirest that your user object actually has a roles field with an array of role names.
I too have been researching oData recently and as we develop our platform in both node and C# naturally looked at JayStorm. From my understanding of the technical details of JayStorm the whole capability of Connect and Express are available to make this topic possible. We use Restify to provide the private API of our platform and there we have written numerous middleware modules for exactly this case.
We are using JayData for our OData Service layer also, and i have implemnment a very simple basic authentication with it.
Since the JayData is using Express, so we can leverage Express' features. For Basic Auth, the simplest way is:
app.use(c.session({ secret: 'session key' }));
// Authenticator
app.use(c.basicAuth('admin', 'admin'));
app.use("/odata.svc", $data.JayService.OData.Utils.simpleBodyReader());
you also can refer to this article for more detail for authentication with Express: http://blog.modulus.io/nodejs-and-express-basic-authentication
Thanks.
I wrote that blogpost, I work for JayData.
What do you mean by custom database?
We have written a middleware for authentication and authorization but it is not open source. We might release it later.
We have a service called JayStorm, it has a free version, maybe that is good for you.
We probably will release an appliance version of it.

Categories

Resources