2

i'm using the open weather map api in order to get information on the current weather and then integrate it with watson assistant (i used this as a reference for the watson assistant code) before deploying on the terminal. here's my code:

var city = "Seattle";
weather.setCity(city);
function processResponse(err, response){
        if(err){
            console.log(err);
            return;
        }
        var endConversation = false;
        if(response.intents[0]){
            if(response.intents[0].intent=="CurrentWeather"){
                 weather.getDescription(function(err, desc){
                     weather.getTemperature(function(err, temp){
                         console.log("It is " + desc + " today with a temperature of " + temp + " degrees Celsius.");
                     )};
                 )};
            }
            else if(response.intents[0].intent=="end_conversation"){
                console.log(response.output.text);
                endConversation = true;
            }
        }
        if(!endConversation){
            var newMessageFromUser = prompt(">> ");
            service.message({
                workspace_id: workspace_id,
                input: {
                    text: newMessageFromUser
                },
                context: response.context
            },processResponse);
        }
}

it works, but then the response looks like this:

>> what is the weather today in seattle
>>
It is few clouds today with a temperature of 29 degrees Celsius.
>> bye
['See ya!']

whenever i use any third party apis, instead of responding right after i enter the trigger keywords, the terminal asks me to input another entry (in the scenario above, i entered nothing) before responding. however, when i try to enter keywords related to intents whose responses are just retrieved right away from the watson assistant (as is with end_conversation), the terminal responds right away.

Is there a way for me to force the terminal to only ask once?

data_henrik
  • 16,724
  • 2
  • 28
  • 49
xjm
  • 164
  • 2
  • 4
  • 18

2 Answers2

2

There are different ways to get around entering something before the actual response.

Take a look at client-based dialog actions. The key is to use the skip_user_input flag and check it within your application. Basically, it would indicate to your application that you need to process some data. The app would it send back to Watson Assistant to respond. There is also the server-based dialog action. In that case Watson Assistant is invoking an IBM Cloud Functions action. A tutorial using that approach is here, interfacing with a Db2 database.

Another technique is what I call replaced markers. You would have Watson Assistant returns an answer with placeholders. Your app would replace those markers.

Third, you are using JavaScript with asynchronous processing. It seems that your empty prompt is processed while you fetch the weather data. The IF for the weather is independent of the empty prompt. Try fixing that.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • do you have any suggestions on how i could fix the if clause for the weather? – xjm Oct 25 '18 at 07:02
  • I am no JavaScript expert, but you could probably use Promises – data_henrik Oct 25 '18 at 07:41
  • 1
    I would add that you can also use cloud function to make a call to third part API and return the response back to dialog. We have used this in the past to create a weather app ourselves. Worked fine. – Michal Bida Oct 26 '18 at 08:37
  • incorporated the server actions – data_henrik Oct 26 '18 at 09:01
  • @MichalBida can this be done using a lite/free account? – xjm Oct 29 '18 at 09:02
  • @xjm Well from https://console.bluemix.net/openwhisk/learn/pricing (29.10.2018), if your cloud function executes in 500ms and its memory limit is 128MB then you get 5,000,000 calls per month for free... But check the web for more details. – Michal Bida Oct 29 '18 at 09:16
  • @MichalBida i've created a cloud foundry space as it says but i keep getting "No Cloud Foundry Space" when i open console.bluemix.net/openwhisk/create so I thought it might have been because of pricing – xjm Oct 29 '18 at 09:21
  • 1
    @xjm Afaik you need to select proper region (I use us-south) then you need to have cloud foundry organization and inside that organization you need to have cloud foundry space. You can set this in the cloud UI when you select Functions -> Actions then in the upper left part of the page. For some reason for me the defaults are always blank - so this might be the reason? If this won't help I would try to contact IBM cloud support (forums or directly...)... – Michal Bida Oct 29 '18 at 09:27
  • 1
    @MichalBida thanks, that did the trick for me! i'll try implementing it now. – xjm Oct 29 '18 at 09:31
0

following Michal Bida's advice, I tried implementing the third party API in cloud function and it worked. simply created a php function using the php implementation of the openweather map api and followed the steps on how to create an action in php through this tutorial. for the implementation, i followed this tutorial on how to implement actions in watson assistant. it now works even when directly invoked from the chat bot at the side of the watson assistant.

an example of a response it returns will be:

{"weather":"It is raining today in Seattle with a temperature of 15 degrees Celsius"}
xjm
  • 164
  • 2
  • 4
  • 18