Status 206 from api.ai https webhook - node.js

I currently have a working ExpressJS webhook for api.ai hosted on EC2.
However, my backend fails when I use the identical GET/POST handlers in an https webhook. In particular, I don't see any (console.log) output from the POST handler, and my api.ai action returns a 206 error.
I've verified that the https webhook server is reachable from a browser. TIA for any suggestions
The api.ai JSON status is below:
{
"id": "ff86f1da-a136-4ec2-b7f8-4aa7cb443f5a",
"timestamp": "2017-07-16T19:04:27.522Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "add bread",
"action": "",
"actionIncomplete": false,
"parameters": {
"number": "",
"shopping_item": "bread",
"unit-weight-name": ""
},
"contexts": [],
"metadata": {
"intentId": "7ea8b3b1-28d6-418e-b9d2-b7353bf5a008",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 126,
"intentName": "add shopping item"
},
"fulfillment": {
"speech": "done",
"messages": [
{
"type": 0,
"speech": "okey-dokey"
}
]
},
"score": 0.9599999785423279
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: Webhook response was empty."
},
"sessionId": "963f3693-5fa3-4d45-a3f3-817f9d433965"
}
The POST handler is below.
app.post('/',function(req, res){
res.setHeader('Content-Type', 'application/json');
//debugging output for the terminal
console.log('you posted: Id: ' + req.body.id + ', Item: ' + req.body.result.parameters.shopping_item
+ ', Query: '+req.body.result.resolvedQuery);
jsonObj={};
jsonObj["displayText"]=req.body.result.parameters.shopping_item;
jsonObj["textToSpeech"]=req.body.result.parameters.shopping_item;
jsonObj["formattedText"]=req.body.result.parameters.shopping_item;
response = "added - "+req.body.result.parameters.shopping_item;
res.send(JSON.stringify({ "speech": response, "displayText": response}));
});

Related

How to send to Amazon's Alexa Event Gateway?

I am trying to test sending an event to the Amazon's Event Gateway for my Alexa Smart Home skill using Postman but I keep receiving an 'invalid access token exception.' I have read the Amazon's documentation on this but apparently I am missing something.
When I enable my skill, my Smart Home Lambda receives the AcceptGrant.
{
"directive": {
"header": {
"namespace": "Alexa.Authorization",
"name": "AcceptGrant",
"messageId": "b2862179-bc56-4bb2-ac05-ce55c7a3e977",
"payloadVersion": "3"
},
"payload": {
"grant": {
"type": "OAuth2.AuthorizationCode",
"code": "ANSVjPzpTDBsdfoRSyrs"
},
"grantee": {
"type": "BearerToken",
"token": "Atza|IwEB..."
}
}
}
}
My lambda sends a POST to 'https://api.amazon.com/auth/o2/token' to receive the Access and Refresh tokens. It then stores those tokens. Next, my Lamdba responds with the following:
{
"event": {
"header": {
"namespace": "Alexa.Authorization",
"name": "AcceptGrant.Response",
"messageId": "b2862179-bc56-4bb2-ac05-ce55c7a3e977",
"payloadVersion": "3"
},
"payload": {}
}
}
I then get a message web page that I have successfully linked my skill - all is good.
Next, I try to send an event to Amazon's Alexa event gateway using the Postman app. I put the Access token (I also tried the Refresh token) in the header as a 'BearerToken' type and the in the 'scope' of the 'endpoint' object.
POST https://api.amazonalexa.com/v3/events?Content-Type=application/json&charset=UTF-8
with a header that specifies a Bearer Token (Access token received earlier) and a body that contains the following:
{
"event": {
"header": {
"messageId": "abc-123-def-456",
"namespace": "Alexa",
"name": "ChangeReport",
"payloadVersion": "3"
},
"endpoint": {
"scope": {
"type": "BearerToken",
"token": "<access token>"
},
"endpointId": "MySmartSwitch-001"
},
"payload": {
"change": {
"cause": {
"type": "RULE_TRIGGER"
},
"properties": [
{
"namespace": "Alexa.ModeController",
"name": "mode",
"value": "Backup",
"timeOfSample": "2020-01-02T09:30:00ZZ",
"uncertaintyInMilliseconds": 50
}
]
}
}
},
"context": {
"properties": [
{
"namespace": "Alexa.PowerController",
"name": "powerState",
"value": "ON",
"timeOfSample": "2020-01-02T09:30:00Z",
"uncertaintyInMilliseconds": 60000
},
{
"namespace": "Alexa.EndpointHealth",
"name": "connectivity",
"value": {
"value": "OK"
},
"timeOfSample": "2020-01-02T09:30:00Z",
"uncertaintyInMilliseconds": 0
}
]
}
}
The response received is '401 Unauthorized'
{
"header": {
"namespace": "System",
"name": "Exception",
"messageId": "95bd23c3-76e6-472b-9c6d-74d436e1eb61"
},
"payload": {
"code": "INVALID_ACCESS_TOKEN_EXCEPTION",
"description": "Access token is not valid."
}
}
I figured out the issue. I was mistakenly sending parameters: Content-Type=application/json and charset=UTF-8 as well including them in the header - my bad. You just need to include them in the header.

Malformed Response: Failed to parse Dialogflow response into AppResponse because of empty speech response

I am using firebase function for the webhook fulfillment in Dialogflow. I am getting webhook successful as a fulfillment status but it is not working. I am using version 1. When I test it on Google Assistant simulator, it says "App is not responding".
firebase function
const functions = require('firebase-functions');
exports.webhook = functions.https.onRequest((request, response) => {
response.send({
"google":{
"richResponse":{
"items":[
{
"simpleResponse":{
"textToSpeech":"Hey! Good to see you."
}
},
{
"mediaResponse":{
"mediaType":"AUDIO",
"mediaObjects":[
{
"name":"Exercises",
"description":"ex",
"largeImage":{
"url":"http://res.freestockphotos.biz/pictures/17/17903-balloons-pv.jpg",
"accessibilityText":"..."
},
"contentUrl":"https://theislam360.me:8080/hbd.mp3"
}
]
}
}
],
"suggestions":[
{
"title":"chips"
}
]
}
}
}
)
});`
When I copy paste the response from {google... to the end in the custom payload manually via GUI, It works. While for webhook, it is not working.
RAW API RESPONSE
{
"id": "eaf627ed-26b5-4965-b0b0-bc77144e144b",
"timestamp": "2019-04-15T11:54:18.948Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "play hbd",
"action": "",
"actionIncomplete": false,
"parameters": {
"any": "hbd"
},
"contexts": [],
"metadata": {
"isFallbackIntent": "false",
"webhookResponseTime": 34,
"intentName": "play",
"intentId": "e60071cd-ce31-4ef9-ae9b-cc370c3362b3",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false"
},
"fulfillment": {
"messages": []
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "e91bd62f-766b-b19d-d37b-2917ac20caa6"
}
FULFILLMENT REQUEST
{
"id": "eaf627ed-26b5-4965-b0b0-bc77144e144b",
"timestamp": "2019-04-15T11:54:18.948Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "play hbd",
"speech": "",
"action": "",
"actionIncomplete": false,
"parameters": {
"any": "hbd"
},
"contexts": [],
"metadata": {
"intentId": "e60071cd-ce31-4ef9-ae9b-cc370c3362b3",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"isFallbackIntent": "false",
"intentName": "play"
},
"fulfillment": {
"speech": "",
"messages": []
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "e91bd62f-766b-b19d-d37b-2917ac20caa6"
}
FULFILLMENT RESPONSE
{
"google": {
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Hey! Good to see you."
}
},
{
"mediaResponse": {
"mediaType": "AUDIO",
"mediaObjects": [
{
"name": "Exercises",
"description": "ex",
"largeImage": {
"url": "http://res.freestockphotos.biz/pictures/17/17903-balloons-pv.jpg",
"accessibilityText": "..."
},
"contentUrl": "https://theislam360.me:8080/hbd.mp3"
}
]
}
}
],
"suggestions": [
{
"title": "chips"
}
]
}
}
}
FULFILLMENT STATUS
Webhook execution successful
Firebase Logs
Google Assistant Simulator Logs
You're not using the correct JSON in the response. By putting it in the GUI in the "custom payload" section, it is creating a larger JSON response for you. The google object needs to be under the data object for Dialogflow v1 or payload for Dialogflow v2. (And if you haven't switched to v2 - you should do so immediately, since v1 will be switched off in about a month.)
So what you're returning should look more like
{
"payload": {
"google": {
...
}
}
}

Chatbase send multiple messages at once error sequence

I'm using https://chatbase.com/api/messages Chatbase API to send multiple messages to the Chatbase. The documentation stats that the response would provide error and success for individual messages that are sent in the request.
How I should detect which message is failed out of the 3 messages that I have provided in the request if the response provides error for one message?
JSON Request body for sending multiple messages
{
"messages": [
{
"api_key": "<API KEY>",
"type": "agent",
"user_id": "User_1",
"time_stamp": 1542895834,
"platform": "XXX",
"message": "Test 1",
"not_handled": false,
"version": "1.0",
"session_id": "session-User_1"
},
{
"api_key": "<API KEY>",
"type": "agent",
"user_id": "User_1",
"time_stamp": 1542895834,
"platform": "XXX",
"message": "Test 2",
"not_handled": false,
"version": "1.0",
"session_id": "session-User_1"
},
{
"api_key": "<API KEY>",
"type": "agent",
"user_id": "User_1",
"time_stamp": 1542895834,
"platform": "XXX",
"message": "Test 3",
"not_handled": false,
"version": "1.0",
"session_id": "session-User_1"
}
]
}
JSON Response body
{
"all_succeeded": false,
"responses": [
{
"error": "Error fetching parameter 'type': Invalid conversation type [dfg]",
"status": "error"
},
{
"message_id": 139429278,
"status": "success"
},
{
"error": "Error fetching parameter 'time_stamp': Received a time (1921-02-09 09:49:26) which was too small. Please send a time within the past day for metrics to appear in the dashboards, or omit the time_stamp field for the time to automatically be set to now.",
"status": "error"
}
],
"status": 200
}
The order of the error messages will correspond to the order of the messages you submitted. In this case, it was the first and third messages. I also noticed that you are not using unix milliseconds in your timestamps. The messages will need to be at least one millisecond apart in order to sort them correctly in the Chatbase reports.

Dialogflow - Fulfillment Webhook Response different from Documentation

I'm currently using Dialogflow in combination with fulfillment/webhooks.
In the documentation of the fulfillments there is an example POST request for webhooks:
POST body:
{
"contexts": [
string
],
"lang": string,
"query": string,
"sessionId": string,
"timezone": string
}
The request that I'm receiving on my end is different from what is defined in the documentation. Has somebody an idea why?
This is what I receive:
{
"id": "GUID",
"timestamp": "2018-01-12T12:25:32.202Z",
"lang": "de",
"result": {
"source": "agent",
"resolvedQuery": "Test",
"speech": "",
"action": "",
"actionIncomplete": false,
"parameters": {
"Nummer": ""
},
"contexts": [],
"metadata": {
"intentId": "XYZ",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"intentName": "Intent"
},
"fulfillment": {
"speech": "",
"messages": [{
"type": 0,
"speech": ""
}]
},
"score": 0.6700000166893005
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "GUID"
}
I've found that different invocation sources send slightly different data to the web hook.
For example the Google Action simulator will send different information in the context reply than say the test pane in an intent.
Im guessing that the answer may lie in where/how your calling the web hook.

How to determine the type of the platform in Dialogflow (API.AI)

How do to identify from which platform the message came?
I want to support different platforms like Telegram and Facebook Messenger, When my webhook receive a message, I want to reply according to the platform the message came form.
For example, if the message came from Telegram I want to return a text message but if the message came from messenger I want to return a card.
You have a property source in originalRequest object, see fulfillment docs here.
{
"lang": "en",
"status": {
"errorType": "success",
"code": 200
},
"timestamp": "2017-02-09T16:06:01.908Z",
"sessionId": "1486656220806",
"result": {
"parameters": {
"city": "Rome",
"name": "Ana"
},
"contexts": [],
"resolvedQuery": "my name is Ana and I live in Rome",
"source": "agent",
"score": 1.0,
"speech": "",
"fulfillment": {
"messages": [
{
"speech": "Hi Ana! Nice to meet you!",
"type": 0
}
],
"speech": "Hi Ana! Nice to meet you!"
},
"actionIncomplete": false,
"action": "greetings",
"metadata": {
"intentId": "9f41ef7c-82fa-42a7-9a30-49a93e2c14d0",
"webhookForSlotFillingUsed": "false",
"intentName": "greetings",
"webhookUsed": "true"
}
},
"id": "ab30d214-f4bb-4cdd-ae36-31caac7a6693",
"originalRequest": {
"source": "google",
"data": {
"inputs": [
{
"raw_inputs": [
{
"query": "my name is Ana and I live in Rome",
"input_type": 2
}
],
"intent": "assistant.intent.action.TEXT",
"arguments": [
{
"text_value": "my name is Ana and I live in Rome",
"raw_text": "my name is Ana and I live in Rome",
"name": "text"
}
]
}
],
"user": {
"user_id": "PuQndWs1OMjUYwVJMYqwJv0/KT8satJHAUQGiGPDQ7A="
},
"conversation": {
"conversation_id": "1486656220806",
"type": 2,
"conversation_token": "[]"
}
}
} }

Resources