How to get Watson Assistant to response correctly - ibm-cloud

First of all, sorry for language errors, I'm Brazilian.
I'm trying to create a Watson chatbot and I'm running into this issue.
I'm making chatbot that's take orders.
At the end, the bot responses 'Thank you! You asked for a ["hot dog","big coke","fries"].'
How can i get a response like 'Thank you! You asked for a hot dog, a big coke and fries.'?
Additional information:
the JSON for take the order:
{
"context": {
"menu": "#menu.values"
}
}
I made the response like this:
Thank you! You asked for a $menu.

As you may have noticed, the $menu is a JSON array. Referring just to its values, gives the printed values as shown by you. Take a look at the Watson Assistant expressions for handling JSON data, especially JSONArray.join.
It combines the values of an array into a single string. Something like this should work.
Thank you! You asked for <? $menu.join(', ') ?>

Related

Can't get handlebars to work with dynamic templates

I am very stuck. Tried everything I could think of to solve this issue, but I feel as though it is just something wrong with my JSON. When I build a dynamic template, I'd like to insert some variables for the send. As you can see in the example, simply just adding the first name in the handlebars. However, when I send tests using postman, I can not for the life of me get the first_name to display. I've tried so many different options in the JSON and nothing seems to work. Here is where I am currently at, omitting the first_name obviously. Any help on how to format this I would very much appreciate it.
{
"from": {"email":"example#example.com"},
"template_id":"ID HERE"
"personalizations": [
{
"to": [
{
"email":"recipient#gmail.com"
}
]
}
]
}
I tried 100 different variations of the request.
Twilio developer evangelist here.
I think that using that JSON is mainly for when you're using the API to send an email with a template. You then provide the JSON data as dynamic_template_data and it is populated in the email template.
first_name is a reserved field and substitution tag which lets you use any reserved or custom field data you've added to Marketing Campaigns to dynamically generate unique content for each recipient of your email. One common example is adding a recipient's first name to the body (or even the subject line) of your email.
The data that populates your Substitution Tags will come from the information you have stored about each contact.
You can work with substitution tags with the code editor or design editor.
Let me know if this helps at all!

How to respond with dynamic options in IBM Watson Assistant?

My webhook returns an array of elements. I need to make IBM Watson Assistant respond with those elements as options to the users.
How can I achieve that?
The JSON structure for an IBM Watson Assistant answer with options is documented. You already mentioned that your webhook returned an array of elements. It would need to match that structure.
Now, in your dialog you would need to add that options array myOptionsArray to your output. Assuming the array data is stored in the variable myvar, use something like this:
<? output.generic.addAll($myvar.myOptionsArray) ?>
The generic refers to the generic JSON output format - in contrast to the integrations JSON format. The above expression could be placed in a response or in some intermediate assignment. It might need some experimenting but works...
#data_henrik response was very helpful but it was little vague. So, I thought to post my own answer. I made some changes in my function so that the result it returns matches the options format just like in the image below.
And just as #data_henrik suggested, I stored that result in a context variable named myOptions and used it like :
<? output.generic.addAll($myOptions) ?>

REST: Is it considered restful if API sends back two type of response?

We have stock website and we help buyers connect with the sellers. We are creating API to let buyers push their contact details and get back the seller details. This is transaction and get logged in our database. We have created following API:
The request is POST, the URL looks like:
/api/leads
The request body looks like:
{
"buyermobile": "9999999999",
"stockid": "123"
}
The response looks like:
{
"sellermobile" : "8888888888",
"selleraddress": "123 avenue park"
}
We have a new requirement, i.e. we need to send back PDF URL (instead of "sellermobile" & "selleraddress"). This PDF URL would contain the seller details in case it comes from one of our client.
We have modified the same API, now the request body looks like:
{
"buyermobile": "9999999999",
"stockid": "123",
"ispdf": true
}
The response looks like:
{
"sellerdetailspdf" : "https://example.com/sellerdetails-1.pdf",
}
Is it RESTFUL to do this? OR we should create separate API for getting response as PDF?
I wouldn't approach it this way. What happens when you need to add XLS? Do you add "isxls" to the request too?
Things I'd consider:
Use a mime type for content negotiation. Post the same request, and specify in the Accept header what you expect back - JSON, PDF, etc. You're then actually getting the report instead of a link to the report, which may or may not be better.
- or -
Include a link in the typical lead response.
{
"sellermobile" : "8888888888",
"selleraddress": "123 avenue park",
"_links": {
"seller-details-pdf": "https://example.com/sellerdetails-1.pdf"
}
}
- or -
Support a query parameter that specifies the type in the response.
- or -
Have a single property that specifies the type in the response, rather than a boolean. Much cleaner to extend when you add new response types.
The first two options have the bonus that you don't require clients to handle multiple response types to a single request. That's not forbidden by any spec, but it's annoying for clients. Try not to annoy the people who you want to pay you. :)
Again the implementation looks good to me, however you could potentially look at breaking the return of the PDF URL to another endpoint maybe something like api/lead/pdf that way your request body is the same for api/lead and all subsequent endpoints under /lead. Allowing your routes and other code to handle small portioned tasks instead of having a route that handles multiple flags and multiple code routes.
That looks good to me - the same type of input should give the same type of response but in your case you have two different types of input - one with the "ispdf" flag and one without. So it's consistent to responds with two different types of response, one with the PDF link and one without.
That's still something you'll want to document but basically it's a correct implementation.

How to give personalised greeting in Watson Conversation?

While Defining the Dialog in the Watson Conversation I'm not able to greet user with his/her name or I'm not able to detect contact number sent by the user and rephrase it to the user. Is it possible to do it in the Watson Conversation Api or not.
Although Mitch's response is correct, here is an example of doing a personalised response.
1. Set your conversation_start node text to "Hello <? context.username ?>".
2. In your code you would do something like this (Python).
import json
from watson_developer_cloud import ConversationV1
conversation = ConversationV1(
username='SERVICE_USERNAME',
password='SERVICE_PASSWORD',
version='2016-07-11')
workspace_id = 'WORKSPACE_ID_CONVERSATION'
response = conversation.message(workspace_id=workspace_id, context= {'username':'Simon'})
print json.dumps(response)
3. When you run this, it should output the following, with the "text" part being what the user sees.
{
"entities":[],
"intents":[],
"output":{
"log_messages":[],
"nodes_visited":["node_1_1472298724972],
"text":["Hello Simon"]
},
"context":{
"username":"Simon",
"conversation_id":"9dc1501b-ac53-4b51-a299-37f5314ebf89",
"system":{
"dialog_turn_counter":1,
"dialog_stack":["root"],
"dialog_request_counter":1
}
},
"input":{}
}
One thing to be aware is that, the context object is used to maintain the state of the conversation. So if you plan to use just REST API's then you need to merge your context variables into the preceding context object before sending it. You do only need to do this at points where you do know the conversation needs that context.
Do you already have access to this information? You can send these values through as context, and refer to them using $context_variable
The same goes for collecting information from a user. You can capture things using regular expressions via your application, or using some Spring Expressions, you can see the text.matches here:
https://www.ibm.com/watson/developercloud/doc/conversation/dialog_reference.shtml
You would store this as context, and then refer to it using $context_variable again.
Information like names and phone numbers is quite open ended, so can be difficult to capture without using an open entity extraction engine, which we are researching best ways to incorporate this.
To get the user's input, use:
"context": {"yourVariable": "<?input.text?>"}
And to show:
"output": {"text": "You entered this $yourVariable"}

Google MAP Api http Request

I am making an iphone app that enables users to search for a city/town/countries/states by keying in texts in a UISearchbar, I managed to get the reply from google via JSON. It works ok, but I have some unexpected replies for certain places.
When I search for, say, "Vietnam", "China", "Malaysia", "Indonesia" it gives 0 results.
But when I search for, "Singapore", "Cambodia", then, it gives a result fine.
I don't get it, why does some countries aren't available as the search results?
Thanks.
Codes: The http request I sent is something like:
http://maps.google.com/maps/geo?q=%#?output=json
The %# is a searchBar.text formatter.
So if i type Indonesia, basically the request becomes:
http://maps.google.com/maps/geo?q=Indonesia?output=json
Instead of using an undocumented output of maps.google.com, you should try using the Geocoding API:http://code.google.com/apis/maps/documentation/geocoding/#JSON Your query would be something like:
http://maps.googleapis.com/maps/api/geocode/json?address=indonesia&sensor=false