Display list tempate - alexa-skills-kit

I am trying to implement the List template feature of Alexa skill kit. However, I am unable to return the response in an appropriate format.
I have implemented the feature using the official documentation. However, I am not getting how to return the response of list template to my custom intent
'ListTemplate':function(){
var title = "This is a sample list";
var speechOutput = "Showing the sample list";
var template = {
"type":"Display.RenderTemplate",
"template":{
"type":"ListTemplate1",
"token":"ListTemplate",
"title":title,
"backButton":"VISIBLE",
"backgroundImage":{
"contentDescription":"backgroundImage",
"sources":[
{
"url":"https://democard.s3.amazonaws.com/hostel-720.jpg"
}]
},
"listItems":[{
"token":"item1",
"image":{
"sources":[{
"url":"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"
}],
"contentDescription":"first item of list"
},
"textContent":{
"primaryText":{
"type":"PlainText",
"text":"primary Text is here"
},
"secondaryText":{
"type":"PlainText",
"text":"Secondary text is here"
}
},
},
{
"token":"item2",
"image":{
"sources":[{
"url":"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"
}],
"contentDescription":"second item"
},
"textContent":{
"primaryText":{
"type":"PlainText",
"text":"primary text is here"
},
"secondaryText":{
"type":"PlainText",
"text":"secondary text"
}
}
}
]
}};
var directives =[ template ];
//return build_speechlet_response(title,speechOutput,directives, SESSION_LIST);
// function
build_speechlet_response(title,speechOutput,directives,phase){
const response = {
"version": "1.0",
"response": {
"outputSpeech":{
"type":"PlainText",
"text":"what else would you like to see"
},
"card":{
'type':'Simple',
'title':title,
'content':speechOutput
},
"directives":directives,
"shouldEndSession":'False'
},
"sessionAttributes":{
"template":"list_"
}
};
// return response;
this.emit(':tell',response);
},
The response I should get must be a custom list. But I am not getting it

It looks like this issues is that response is an object. It should be something like this.emit(':tell', speechOutput) (where speechOutput is a string).
If you want to also send a card it's this.emit(':tellWithCard', speechOutput, cardTitle, cardContent, imageObj).
But, since you're trying to use a render template, it would be something like:
this.response.speak(speechOutput)
.cardRenderer(cardTitle, cardContent, cardImage)
.renderTemplate(template);
this.emit(':responseReady');
You can find more info here - https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/tree/1.x
I do notice you're using v1 of the SDK - I would really recommend using v2 as it's a lot more straight forward.
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs
Hope that helps.

I tried the following code yet the response was not rendered.
const DisplayListIntentHandler = {
canHandle(handlerInput){
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'DisplayList';
},
handle(handlerInput){
var title = "This is a sample list";
var speechOutput = "Showing the sample list";
var template = {
type:'Display.RenderTemplate',
template:{
type:"ListTemplate1",
token:"ListTemplate",
title:'title',
backButton:"VISIBLE",
backgroundImage:{
contentDescription:"backgroundImage",
sources:[
{
url:"https://democard.s3.amazonaws.com/hostel-720.jpg"
}]
},
listItems:[{
token:"item1",
image:{
sources:[{
url:"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"
}],
contentDescription:"first item of list"
},
textContent:{
primaryText:{
type:"PlainText",
text:"primary Text is here"
},
secondaryText:{
type:"PlainText",
text:"Secondary text is here"
}
},
},
{
token:"item2",
image:{
sources:[{
url:"https://democard.s3.amazonaws.com/c-v-raman-college-of-engineering-squarelogo-1534916004379+(3).jpg"
}],
contentDescription:"second item"
},
textContent:{
primaryText:{
type:"PlainText",
text:"primary text is here"
},
secondaryText:{
type:"PlainText",
text:"secondary text"
}
}
}
]
}};
return handlerInput.responseBuilder
.addRenderTemplateDirective(template)
.getResponse();
}
};

Related

Formatter called multiple times or too soon with `undefined`

I'm using OpenUI5. Using the formatter.js, I have formatted some text in my view.
But my formatter is called 3 times:
When I bind the model to panel control: oPanel.setModel(oModel, "data");
both sBirthday and sFormat are undefined.
After onInit() is finished and the view is rendered:
sBirthday is valorized correctly and sFormat is undefined
Again: both sBirthday and sFormat ara valorized correctly.
Why does this happen? Is it correct?
The app get an error, because the ageDescription() in the formatter, can't manage undefined values.
formatter.js
sap.ui.define([], function () {
"use strict";
return {
ageDescription : function (sBirthday, sFormat) {
do.something();
var sFromMyBd = moment(sBirthday, sFormat).fromNow();
do.something();
return sAge;
}
}
});
main.view.xml
<mvc:View
controllerName="controller.main"
xmlns="sap.m"
xmlns:mvc="sap.ui.core.mvc">
<Panel id="user-panel-id">
<Input id="name-input-id" enabled="false" value="{data>/user/name}" />
<Label text="{i18n>age}: " class="sapUiSmallMargin"/>
<Label text="{
parts: [
{path: 'data>/user/birthday'},
{path: 'data>/user/dateFormat'}
],
formatter: '.formatter.ageDescription' }"/>
</Panel>
</mvc:View>
Main.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
"model/formatter"
], function (Controller, JSONModel, formatter) {
"use strict";
return Controller.extend("controller.main", {
formatter: formatter,
onInit: function () {
var oModel = new JSONModel();
var oView = this.getView();
oModel.loadData("model/data.json");
var oPanel = oView.byId("user-panel-id");
oPanel.setModel(oModel,"data");
do.something();
},
});
});
data.json
{
"user": {
"name": "Frank",
"surname": "Jhonson",
"birthday": "23/03/1988",
"dateFormat": "DD/MM/YYYY",
"enabled": true,
"address": {
"street": "Minnesota street",
"city": "San Francisco",
"zip": "94112",
"country": "California"
}
}
}
Set the model to the view only when the data request is completed:
onInit: function() {
const dataUri = sap.ui.require.toUri("<myNamespace>/model/data.json");
const model = new JSONModel(dataUri);
model.attachEventOnce("requestCompleted", function() {
this.getView().setModel(model);
}, this);
// ...
},
This ensures that the formatter is called only once (invoked by checkUpdate(true) which happens on binding initialization; see below), and no further changes are detected afterwards.
Additionally (or alternatively), make the formatter more defensive. Something like:
function(value1, value2) {
let result = "";
if (value1 && value2) {
// format accordingly ...
}
return result;
}
Why does this happen?
View gets instantiated.
onInit of the Controller gets invoked. Here, the file model/data.json is requested (model is empty).
Upon adding the view to the UI, UI5 propagates existing parent
models to the view.
Bindings within the view are initialized, triggering checkUpdate(/*forceUpdate*/true)src in each one of them.
Due to the forceUpdate flag activated, change event is fired, which forcefully triggers the formatters even if there were no changes at all:
[undefined, undefined] → [undefined, undefined]. - 1st formatter call
Fetching model/data.json is now completed. Now the model needs to checkUpdate again.
[undefined, undefined] → [value1, undefined] → change detected → 2nd formatter call
[value1, undefined] → [value1, value2] → change detected → 3rd formatter call
Now, given that you are trying to load a static JSON file into your project, it's better to maximize the usage of the manifest.json.
That way, you are sure that the data is already loaded and available in the model prior to any binding.
You achieve this by adding the JSON file as a data source under sap.app
manifest.json
"sap.app": {
"id": "com.sample.app",
"type": "application",
"dataSources": {
"data": {
"type": "JSON",
"uri": "model/data.json"
}
}
}
Now, simply add this dataSource called data as one of the models under sap.ui5.
"sap.ui5": {
"rootView": {
"viewName": "com.sample.app.view.App",
"type": "XML"
},
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "com.app.sample.i18n.i18n"
}
},
"data": {
"type": "sap.ui.model.json.JSONModel",
"dataSource": "data"
}
}
}
With this, you DON'T need to call this anymore:
var oModel = new JSONModel();
var oView = this.getView();
oModel.loadData("model/data.json");
var oPanel = oView.byId("user-panel-id");
oPanel.setModel(oModel,"data");
..as the data model we added in the manifest.json, is already visible to oView and oPanel right from the get go.
This way, it doesn't matter if the formatter gets called multiple times, as it would already have the data available to it right from the beginning.

Add a number to a list item with DynamoDB

This is the DynamoDB table structure I'm working on:
{
"userId": "99999999-9999-9999-9999-999999999999",
"userProfile": {
"email": "myemail#gmail.com",
"firstName": "1234124",
"lastName": "123423",
},
"masterCards": [
{
"cardId": 101000000000001,
"cardImage": "logo.png",
"cardName": "VipCard1",
"cardWallet": "0xFDB17d12057b6Fe8c8c425D2DB88d8475674567"
},
{
"cardId": 102000000000002,
"cardImage": "logo.png",
"cardName": "VipCard2",
"cardWallet": "0xFDB17d12057b6Fe8c8c425D2DB88d8183454345"
},
{
"cardId": 103000000000003,
"cardImage": "logo.png",
"cardName": "VipCard3",
"cardWallet": "0xFDB17d12057b6Fe8c8c425D2DB88d8184345345"
}
],
}
I'm trying to increase the cardId field by one for the first list item with this Lambda function:
const dynamoDB = new AWS.DynamoDB({region: 'eu-central-1', apiVersion:'2012-08-10'});
const counterId="99999999-9999-9999-9999-999999999999"
const params = {
TableName:"FidelityCardsUsers",
Key: {"userId":{"S":counterId}},
UpdateExpression:"ADD #masterCards[0].#cardId :increment",
ExpressionAttributeNames:{
"#masterCards": "masterCards",
"#cardId": "cardId"
},
ExpressionAttributeValues:{":increment": {"N": "1"}}
}
dynamoDB.updateItem(params, function(err, data) {
if (err) {
console.log('error getting counter from DynamDB: ',err)
callback(err);
} else {
callback(null,data)
}
})
In return I get only a new top-level attribute named "mastercards[0].cardId[0]" with a value number set to 1.
I have tried to increment In an array and its work fine with AWS.DynamoDB.DocumentClient()
Example :
var AWS = require("aws-sdk");
var docClient = new AWS.DynamoDB.DocumentClient();
let params = {
TableName:'tableName',
Key: {
'venueId': 'VENUE_002'
},
UpdateExpression: "ADD #walk.#coordinates[0] :increment",
ExpressionAttributeNames: {
'#walk': 'walk',
'#coordinates': 'coordinates'
},
ExpressionAttributeValues: {
':increment': 1 // This is from the client
},
ReturnValues: 'UPDATED_NEW'
};
docClient.update(params, function (err, data) {
if (err) {
console.log('failure:updateShuttleDirection:failed');
console.log(err);
} else {
console.log('success:updateShuttleDirection:complete');
console.log(data);
}
});
Sample Data:
"walk": {
"coordinates": [
10,
20
],
"type": "Point"
},
I have tried to increment 10 to 11 and its work fine
Reading the doc here, it seems that:
the ADD action can only be used on top-level attributes, not nested
attributes.

botkit middleware - How to use sendToWatson to update context?

I use https://github.com/watson-developer-cloud/botkit-middleware#implementing-app-actions as my reference.
The context in my conversation does not update.
Here is my bot-facebook.js.
function checkBalance(context, callback) {
var contextDelta = {
user_name: 'Henrietta',
fname: 'Pewdiepie'
};
callback(null, context);
}
var checkBalanceAsync = Promise.promisify(checkBalance);
var processWatsonResponse = function (bot, message) {
if (message.watsonError) {
console.log(message.watsonError);
return bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
}
if (typeof message.watsonData.output !== 'undefined') {
//send "Please wait" to users
bot.reply(message, message.watsonData.output.text.join('\n'));
if (message.watsonData.output.action === 'check_balance') {
var newMessage = clone(message);
newMessage.text = 'check new name';
checkBalanceAsync(message.watsonData.context).then(function (contextDelta) {
console.log("contextDelta: " + JSON.stringify(contextDelta));
return watsonMiddleware.sendToWatsonAsync(bot, newMessage, contextDelta);
}).catch(function (error) {
newMessage.watsonError = error;
}).then(function () {
return processWatsonResponse(bot, newMessage);
});
}
}
};
controller.on('message_received', processWatsonResponse);
The JSON editor of welcome node in my watson conversation.
{
"context": {
"fname": "",
"user_name": ""
},
"output": {
"text": {
"values": [
"Good day :) My name is Doug and I am a chatbot."
],
"selection_policy": "random"
},
"action": "check_balance"
}
}
I have tried multiple ways I could imagine.
Do I need to do something like fname: <?contextDelta.fname?> in the json editor?
You aren't checking context in your dialog.
Context object in JSON editor is used to store captured data in context,
so your node actually empties context variable.
Probably you need to remove that context initialization from your dialog,
To see value of context variable, you have to use it in the output
"Good day, $fname :) My name is Doug and I am a chatbot."

Meteor Publish-Composite nesting issue

Problem:
I have a number of groups that each have members that belong to different groups. Each member has a title (role) in each group.
I’m trying to list all the groups and display each member in the group and their title.
I’m using reywood:publish-composite, and everything is working except I can’t get the title of each member to display.
I think the problem is in the Template.groupMembers.helpers file
title: function() {
console.log(this.roleId); // this shows up in the console for each member
return Titles.findOne({titleId: this.roleId}); // but this doesn’t work
},
Collections:
groups {
"_id" : "xFSzAHBEps2dSKcWM",
"name" : "Generic Group",
"logo" : "generic-logo-hi.png"
}
members {
"_id" : "vyDtiaKKukZYQdFvs",
"groupId" : "xFSzAHBEps2dSKcWM",
"memberId" : "hRx8GBTyB5X8iQQ52",
"roleId" : "1"
}
Meteor.users {
"_id" : "hRx8GBTyB5X8iQQ52",
"profile" : {
"name" : "Bob Lorros"
},
}
titles {
"_id" : "bYsKpsyYtyKR8NYpm",
"titleId" : 1,
"title" : "Staff (non-voting)"
}
server/publications/publications.js
Meteor.publishComposite('groupMembers', {
find: function() {
return Groups.find({}, {
sort: {name: 1}
});
},
children: [
{
find: function() {
return Titles.find();
},
find: function(group) {
return Members.find({groupId: group._id});
},
children: [
{
find: function(member) {
return Meteor.users.find({_id: member.memberId});
}
},
]
},
]
});
client/templates/test/test.js
Template.groupMembers.helpers({
groupMembers: function() {
return Groups.find({}, {
sort: {name: 1}
});
},
members: function() {
return Members.find({groupId: this._id});
},
title: function() {
console.log(this.roleId); // this shows up in the console for each member
return Titles.findOne({titleId: this.roleId}); // but this doesn’t work
},
memberName: function() {
return Meteor.users.findOne(this.memberId);
},
});
client/templates/test/test.html
<template name="groupMembers">
<h4>Group - Members</h4>
{{#each groupMembers}}
<b>{{name}}</b><br>
{{#each members}}
{{memberName.profile.name}}
- title = {{title.title}}
<br>
{{/each}}
<br>
{{/each}}
</template>
Output :
This is the ouput
Looking at this from a completely different perspective, I actually think you could use alanning:roles to accomplish exactly what you're looking for. You can use the role as the 'title' in this case and the 'group' to replace your groups. Here's the documentation:
https://github.com/alanning/meteor-roles
Not sure but I think your second find may be overriding your first. Instead of:
find: function() {
return Titles.find();
},
find: function(group) {
return Members.find({groupId: group._id});
},
Try returning an array of cursors.
find: function() {
return [
Titles.find(),
Members.find({groupId: group._id})
];
},
I don't understand however why Titles is a child of GroupMembers when the query for titles is all titles. Did you mean to have a query there?
I think your publishComposite is causing the problem, each object in the children array should have only one find and zero or more children. Also the second parameter in your publication must be a function and not a JSON object. Try this,
Meteor.publishComposite('groupMembers', function () {
return {
find: function() {
return Groups.find({}, {
sort: {name: 1}
});
},
children: [{
find: function() {
return Titles.find();
}
},
{
find: function(group) {
return Members.find({groupId: group._id});
},
children: [{
find: function(member) {
return Meteor.users.find({_id: member.memberId});
}
}]
}]
};
});
You can also improve performance by moving Titles.find to the root level
Meteor.publishComposite('groupMembers', function () {
return [{
find: function() {
return Titles.find();
}
}, {
find: function() {
return Groups.find({}, {
sort: {name: 1}
});
},
children: [{
find: function(group) {
return Members.find({groupId: group._id});
},
children: [{
find: function(member) {
return Meteor.users.find({_id: member.memberId});
}
}]
}]
}];
});

How to join denormalized data that goes two+ levels deep in Firebase

Here's the scenario: I have a list of topics, each topic includes posts, and each post was "liked" by a list of users. Thus my data looks something like this:
"topics": {
"topic1": {
"posts": {
"post1": true,
"post2": true
}
}
},
"posts": {
"post1": {
"title": "An awesome post",
"likes": {
"user1": true
}
},
"post2": {
"title": "An even better post",
"likes": {
"user1": true,
"user2": true
}
}
},
"users": {
"user1": {
"name": "Mr. T",
"email": "t#t.com"
},
"user2": {
"name": "Mr. Hello World",
"email": "hello#world.com"
}
}
I (think I) know how to get all posts for the topic using Firebase.util (http://firebase.github.io/firebase-util):
Firebase.util.intersection(
fb.child('topics').child('topic1').child('posts'),
fb.child('posts')
)
But now I would like each post to include the names of the users who liked the post. How does one do that?
Probably won't change anything, but this is all happening in AngularFire.
See a working example here
The gist of this sort of denormalization is to fetch the users as you grab posts. It's nothing more complex than it sounds. Just go grab them.
Firebase does a lot of work internally to optimize requests and re-uses the same socket connection for all the listeners, so this is quite performant--barely more overhead than the amount of bytes being downloaded, regardless of whether they are split into separate paths or stored together.
The HTML:
<h3>Normalizing user profiles into posts</h3>
<ul ng-controller="ctrl">
<li ng-repeat="post in posts | orderByPriority" ng-init="user = users.$load(post.user)">
{{user.name}}: {{post.title}}
</li>
</ul>
The JavaScript:
var app = angular.module('app', ['firebase']);
var fb = new Firebase(URL);
app.controller('ctrl', function ($scope, $firebase, userCache) {
$scope.posts = $firebase(fb.child('posts'));
$scope.users = userCache(fb.child('users'));
});
app.factory('userCache', function ($firebase) {
return function (ref) {
var cachedUsers = {};
cachedUsers.$load = function (id) {
if( !cachedUsers.hasOwnProperty(id) ) {
cachedUsers[id] = $firebase(ref.child(id));
}
return cachedUsers[id];
};
cachedUsers.$dispose = function () {
angular.forEach(cachedUsers, function (user) {
user.$off();
});
};
return cachedUsers;
}
});

Resources