Passing a parameter between multiple intents triggering Dialogflow's Fulfillment - javascript

I have a Dialogflow agent that I've setup with a web hook that is pulling info from a Firebase database. I am trying to have different intents kick off different queries on the DB. From the Welcome Intent the agent asks for a name that it will matching the DB (e.g. Hi it's nice to meet you? What is the name you looking for?). From there the user is gives a name which is a response that triggers another intent called "name." This intent has a parameter called "agent.parameters.defaultName" which is passed into the fulfillment and used to query the DB. Below is the webhook kicks off the following script:
});
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request,
response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' +
JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
//function to request bio info on the db
function handleData(agent) {
const congressName = agent.parameters.congressName;
return admin.database().ref().once("value").then((snapshot) => {
var nameInfo = snapshot.child('Name/' + congressName + '/profile').val();
agent.add(nameInfo + "\n \n Tell me the first and last name of the next
person you'd like to learn about. Or you can say Twitter to get this
individual's Twitter info.");
});
}
//Function to return the name's tweet info
function handleTweet(agent) {
const congressName = agent.parameters.congressName;
return admin.database().ref().once("value").then((snapshot) => {
var nameTweet = snapshot.child('Name/' + congressName + '/twitter_handle').val();
agent.add(nameTweet);
});
}
// Run the proper function handler based on the matched Dialogflow intent
name
let intentMap = new Map();
intentMap.set('name', handleData);
intentMap.set('name - custom', handleTweet);
agent.handleRequest(intentMap);
});
This returns the bio and prompts the user to say another name or say twitter to pull the twitter info.
If the user says Twitter I'd like for another intent to be triggered. This Intent would also have a fulfillment that would call the DB, but this time it'd pull the Twitter info. Where I'm having an issue is I can't get this intent to trigger the fulfillment. I'm wondering if my parameter is in the right section or if I need to figure out how to pass it to the Twitter intent. I'm not sure where I'm off.
Below are the intents:
//Name intent
{
"id": "0c7bd173-e7fe-4bb4-9b87-7b94624ceb4e",
"name": "name",
"auto": true,
"contexts": [],
"responses": [{
"resetContexts": false,
"action": "congressName",
"affectedContexts": [{
"name": "Name",
"parameters": {},
"lifespan": 5
}],
"parameters": [{
"id": "a79559d6-d3db-4b37-b681-174fce8bc58c",
"required": true,
"dataType": "#sys.any",
"name": "congressName",
"value": "$congressName",
"prompts": [{
"lang": "en",
"value": "What is the proper first and last name of the person you
are looking for info on?"
}],
"isList": false
}],
"messages": [{
"type": 0,
"lang": "en",
"speech": []
}],
"defaultResponsePlatforms": {},
"speech": []
}],
"priority": 500000,
"webhookUsed": true,
"webhookForSlotFilling": false,
"lastUpdate": 1535995990,
"fallbackIntent": false,
"events": []
}
[{
"id": "a14768b0-c64d-4a63-9ccb-d9452b74ed21",
"data": [{
"text": "tammy duckworth",
"alias": "congressName",
"meta": "#sys.any",
"userDefined": false
}],
"isTemplate": false,
"count": 0,
"updated": 1535223341
},
{
"id": "520acfc8-102b-4e14-9342-54678e9f6940",
"data": [{
"text": "tom cotton",
"alias": "congressName",
"meta": "#sys.any",
"userDefined": false
}],
"isTemplate": false,
"count": 0,
"updated": 1535223341
}
]
//Twitter intent
"id": "78330811-d692-4c70-adb2-3130b608d46f",
"name": "twitter",
"auto": true,
"contexts": [],
"responses": [{
"resetContexts": false,
"action": "",
"affectedContexts": [],
"parameters": [{
"id": "7acd4cb2-9cd7-4c2a-b5aa-2981ee25acf4",
"dataType": "#congressName",
"name": "congressName",
"value": "$congressName",
"isList": false
}],
"messages": [{
"type": 0,
"lang": "en",
"speech": []
}],
"defaultResponsePlatforms": {},
"speech": []
}],
"priority": 500000,
"webhookUsed": true,
"webhookForSlotFilling": false,
"lastUpdate": 1535996186,
"fallbackIntent": false,
"events": []
}
[{
"id": "4f099a33-74c6-4832-acd1-815aca6605f2",
"data": [{
"text": "Susan Collins",
"alias": "congressName",
"meta": "#congressName",
"userDefined": false
},
{
"text": " Twitter",
"userDefined": false
}
],
"isTemplate": false,
"count": 0,
"updated": 1535996186
},
{
"id": "f775749a-56fd-410d-a167-174e6eb03ddf",
"data": [{
"text": "twitter ",
"userDefined": false
},
{
"text": "#congress",
"alias": "congressName",
"meta": "#congressName",
"userDefined": true
}
],
"isTemplate": false,
"count": 0,
"updated": 1535238371
}
]

Related

How to get the wallet ID of sender and receiver from solana transaction signature?

I have the signature of a Solana transaction and I want to find the wallet ID of the sender and the receiver. This is what I did:
const connection = new Web3.Connection(Web3.clusterApiUrl("devnet"), "confirmed");
const transaction = await connection.getTransaction(signature);
When I do console.log(transaction), it has fields like blockTime and all, but not to and from address. Can someone help?
When calling getTransaction, the node will return the transaction in the format specified. By default, connection.getTransaction will return the transaction with base58 encoding, so try using connection.getParsedTransaction instead. Here's an example for a simple transfer:
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0",
"id": 1,
"method": "getTransaction",
"params": [
"2YHDUWRRh4jwaAqSPJqCiu97FTy6Pu2C6XGbAzsaBbyjQeXW11z
hhF3DJHt4vDFHVND1ybdSHf6E5FxbjFXZP4gQ",
"jsonParsed"
]
}
' | python3 -m json.tool
{
"jsonrpc": "2.0",
"result": {
"blockTime": 1647001173,
"meta": {
"err": null,
"fee": 5000,
"innerInstructions": [],
"logMessages": [
"Program 11111111111111111111111111111111 invoke [1]",
"Program 11111111111111111111111111111111 success"
],
"postBalances": [
23932341357,
110000000,
1
],
"postTokenBalances": [],
"preBalances": [
23942346357,
100000000,
1
],
"preTokenBalances": [],
"rewards": [],
"status": {
"Ok": null
}
},
"slot": 120237987,
"transaction": {
"message": {
"accountKeys": [
{
"pubkey": "4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn",
"signer": true,
"writable": true
},
{
"pubkey": "4AUt2JyjzJYVhWkjKugXmzhWizpb4SpLHBtL2fuqPskU",
"signer": false,
"writable": true
},
{
"pubkey": "11111111111111111111111111111111",
"signer": false,
"writable": false
}
],
"instructions": [
{
"parsed": {
"info": {
"destination": "4AUt2JyjzJYVhWkjKugXmzhWizpb4SpLHBtL2fuqPskU",
"lamports": 10000000,
"source": "4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn"
},
"type": "transfer"
},
"program": "system",
"programId": "11111111111111111111111111111111"
}
],
"recentBlockhash": "3zny2xt5wimev9Jry3TiAyK8yA2pMMcGvsPWpFN5HiL6"
},
"signatures": [
"2YHDUWRRh4jwaAqSPJqCiu97FTy6Pu2C6XGbAzsaBbyjQeXW11zhhF3DJHt4vDFHVND1ybdSHf6E5FxbjFXZP4gQ"
]
}
},
"id": 1
}
In result.transaction.message.instructions[0].parsed you'll get what you're looking for.
More information about the getTransaction call at https://docs.solana.com/developing/clients/jsonrpc-api#gettransaction

LoopbackJS Get authenticated UserId to be stored against a model without exposing it via the API

I have the below "Content" model in Loopback which requires an authenticated user to access.
Each piece of Content can be created by only one user. (user is inherited from the Loopback internal User model) So when creating Content I would like the UserId to be stored in the Content record.
However, i'd like this to happen behind the scenes without having to pass through the userId via the API.
I've had a play with relations, but this doesn't seem to be working out..
{
"name": "Content",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"BooId": {
"type": "string",
"required": true
},
"Name": {
"type": "string",
"required": true
},
"Language": {
"type": "string",
"required": true,
"default": "en-gb"
},
"Version": {
"type": "number",
"default": 1
},
"Text": {
"type": "string",
"required": true
},
"Created": {
"type": "date",
"defaultFn": "now"
},
"Tags": {
"type": [
"string"
],
"default": []
}
},
"validations": [],
"relations": [],
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
}
],
"methods": {}
}
First of all, I would create a new relation for the content. So I would add the relation to your Content model:
"relations": {
"author": {
"type": "belongsTo",
"model": "user", // <-- your user model name
"foreignKey": ""
}
}
Then, you could use the before save hook to set the userId on your content model.
Or, you can simply just download this mixin to do it for you.
https://github.com/akkonrad/loopback-author-mixin

Querying a nested array in MongoDB

This is my posting document in MongoDB:
{
"_id": {
"$oid": "5b4e60ab24210138f5746402"
},
"type": [
"full",
"temp"
],
"applications": [
{
"_id": {
"$oid": "5b52113d1123631744fa9f39"
},
"applicationDate": {
"date": 20,
"day": 5,
"hours": 18,
"minutes": 43,
"month": 6,
"seconds": 41,
"time": 1532105021753,
"timezoneOffset": -120,
"year": 2018
},
"userId": {
"$oid": "5b51fb6f9686430cee31a0d9"
},
"resume": {
"fieldname": "resume",
"originalname": "resume_acc.pdf",
"encoding": "7bit",
"mimetype": "application/pdf",
"destination": "./public/resumes/",
"filename": "765d650b9014cc3ddadb801d10d495a5",
"path": "public/resumes/765d650b9014cc3ddadb801d10d495a5",
"size": 8
},
"coverLetter": {
"fieldname": "docs",
"originalname": "cover letter.pdf",
"encoding": "7bit",
"mimetype": "application/pdf",
"destination": "./public/resumes/",
"filename": "e5892869b24f3fc5e72d3e057b4dd61d",
"path": "public/resumes/e5892869b24f3fc5e72d3e057b4dd61d",
"size": 5
}
}
],
"creatorId": {
"$oid": "5b4b95cc16778c325010a55d"
},
"title": "Developer",
"salary": "50/h",
"timeLine": "One year",
"description": "You'll be building apps",
"duties": "Building apps",
"experience": "2 years",
"province": "ON",
"visible": true,
"__v": 0
}
Postings is an array of posting, which look like the above document. applications is an array which is in every posting. I want to search all postings.applications to see get all postings the user applied to. For now I tried to do it like this:
var Posting = require('../models/posting');
var postings = await Posting
.find({'visible': true});
console.log('posts', postings);
var applications = await Posting
.find({'visible': true})
.where(postings
.map(posting => posting.applications
.map(application => application.userId.equals(req.user._id)))
);
But obviously this failed.
I tried this as well:
var postings = await Posting
.find({'visible': true, 'applications[$].userId': req.user._id});
or
var postings = await Posting
.find({'visible': true, 'applications.$.userId': req.user._id});
But no luck. They both return an empty array.
Posting model:
var mongoose = require('mongoose');
jobPostingSchema = mongoose.Schema({
"creatorId": mongoose.Schema.Types.ObjectId, //ObjectID('aaaa'), // ID of the User or Account
"eventId": {'type': mongoose.Schema.Types.ObjectId, 'default': undefined},
"title": String,
"type": [], //"Full", // What this means? I did not understand.
"salary": String,
"timeLine": String, // What this means? I did not understand.
"description": String,
"duties": String,
"experience": String,
"province": String, // Employer will post job postings based on the province and region
// Applications means, how many people applied for this job post?
"applications": [
// {
// ObjectID: cccc,
// userId: dddd,
// Resume: {},
// coverLetter: String,
// },
],
"visible": Boolean,
});
module.exports = mongoose.model('posting', jobPostingSchema);
So how can I get all applications where userId equals req.user._id?
Maybe this works as a solution ( sourcing from the SO link shared by #DSCH here ):
Posting.find({
'applications': {
$elemMatch: { userId: req.user._id }
},
'visible:': true
});
If you wish to seek clarification on how it works, you may refer to the link here
Posting.find({
'visibile:': true,
'applications': {
$elemMatch: { userId: req.user._id }
}
});
$elemMatch is the mongo operator that you probably need.
Hope that one helps better.

Error in enum data type while uploading intent dialogflow

I am trying to upload my intent everything is working fine, i am writing script in V2 and everything works but for parameter webhookState data type is enum ( [here] ) and we have to enter following of the three values ( [here][1]) now when i enter any one and try to upload it shows.
" Unable to load file: SyntaxError: Unexpected token W in JSON at position 98 "
now when I pass WEBHOOK_STATE_ENABLED(or any one) in quotes code uploads successfully (because as per my knowledge it treats it as string and gets executed) and intent is created for my agent but webhook remains off and also training phrases are not there, i am certain that the parameter webhookState is not being activated when i pass in quotes(because of above mentioned reason) Same is the case with rest, like when i try to put in type of training phrases.
{
"name": "Warehouse_Management",
"displayName": "Warehouse_Management",
"webhookState": "WEBHOOK_STATE_ENABLED_FOR_SLOT_FILLING" ,
"priority": 50000,
"isFallback": false,
"mlDisabled": false,
"trainingPhrases": [
{
"name":"Try1" ,
"type": "EXAMPLE",
"parts": [
{
"text": "for",
"userDefined": true
},
{
"text": "warehouse",
"entityType": "#Properties",
"alias": "Properties",
"userDefined": true
},
{
"text": "management",
"userDefined": true
}
]
},
{
"name":"Try2" ,
"type": "EXAMPLE",
"parts": [
{
"text": "i want app for ",
"userDefined": true
},
{
"text": "warehouse",
"alias": "Properties",
"entityType": "#Properties",
"userDefined": true
}
]
}
],
"outputContexts": [
{
"name": "Yes",
"lifespanCount": 2
},
{
"name": "No",
"lifespanCount": 2
},
{
"name": "Device_Integration",
"lifespanCount": 2
}
],
"resetContexts": false,
"parameters": [
{
"name": "Properties",
"displayName": "Properties",
"value": "$parameter_name",
"entityTypeDisplayName": "#Properties",
"mandatory": false,
"isList": true
}
],
"messages": [
{
"text":"This is sample response"
}
],
"rootFollowupIntentName": "root",
"parentFollowupIntentName": "parent"
}
Please NOTE that in the attached Json i have put it in quotes so it would get successfully executed and will create intent.
In order for it to be valid JSON, the enum value should be wrapped in quotes. It's expecting the enum value as a String.

How do I expand nested relationships in REST API using Sails.js

I am a newbie to NodeJS and Sails.js.
I want create a REST API that allows me to expand a resource based on query parameter. For eg
HTTP GET /snippets
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "htep://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": "http://localhost:8000/users/2/",
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
HTTP GET /snippets?expand=owner
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "http://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": {
"url": "http://localhost:8000/users/2/",
"username": "test",
"email": "test#test.com"
},
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
Wondering how can I do that in Sails.js or NodeJS?
You should use assocations.
Here is how you would create a one-to-many association between your User model and your Snippet model:
// User.js
module.exports = {
// ...
attributes: {
// ...
snippets: {
collection: 'Snippet',
via: 'owner'
}
}
};
// Snippet.js
module.exports = {
// ...
attributes: {
// ...
owner: {
model: 'User'
}
}
};
Then you can hit /snippets if you want a list of snippets, and hit /snippets?populate=[owner] if you need details about the owners of the snippets.

Categories