在Watson Assistant上使用第三方api

时间:2018-10-25 01:26:43

标签: javascript ibm-cloud ibm-watson watson-conversation openweathermap

我正在使用open weather map api来获取有关当前天气的信息,然后将其与watson Assistant集成(我将this as a reference用于watson Assistant代码),然后再部署到终端上。这是我的代码:

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);
        }
}

它可以工作,但响应如下:

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

无论何时我使用任何第三方api,而不是在我输入触发关键字后立即响应,终端都会要求我在响应之前输入另一个条目(在上述情况下,我什么也没输入)。但是,当我尝试输入与意图相关的关键字,而这些意图的答案是立即从Watson Assistant中检索出来的(与end_conversation一样),终端会立即做出响应。

我有办法强迫终端只问一次吗?

2 个答案:

答案 0 :(得分:2)

有多种方法可以避免在实际响应之前输入内容。

看看client-based dialog actions。关键是使用skip_user_input标志并在您的应用程序中对其进行检查。基本上,它将向您的应用程序指示您需要处理一些数据。该应用程序会将其发送回Watson Assistant进行响应。 There is also the server-based dialog action。在那种情况下,Watson Assistant将调用IBM Cloud Functions操作。 A tutorial using that approach is here, interfacing with a Db2 database

另一种技术是我所说的replaced markers。您将让Watson Assistant返回带有占位符的答案。您的应用将替换这些标记。

第三,您将JavaScript与异步处理一起使用。似乎在获取天气数据时处理了空提示。天气的IF与空提示无关。尝试解决该问题。

答案 1 :(得分:0)

Michal Bida's advice之后,我尝试在云功能中实现第三方API,该方法可以正常工作。只需使用php implementation of the openweather map api创建了一个php函数,并遵循了如何通过this tutorial在php中创建一个动作的步骤。在实现过程中,我遵循了this tutorial,以了解如何在Watson Assistant中实施操作。现在,即使直接从Watson Assistant侧面的聊天机器人中调用它,它也可以正常工作。

返回的响应示例为:

{"weather":"It is raining today in Seattle with a temperature of 15 degrees Celsius"}