簡體   English   中英

在 watson 助手上使用第三方 api

[英]using third party api on watson assistant

我正在使用開放天氣地圖 api以獲取有關當前天氣的信息,然后在部署到終端之前將其與 watson 助手集成(我將其用作watson 助手代碼的參考)。 這是我的代碼:

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 助手(與 end_conversation 一樣)檢索響應的意圖相關的關鍵字時,終端會立即響應。

有沒有辦法強制終端只詢問一次?

在實際響應之前,有多種方法可以繞過輸入內容。

看看基於客戶端的對話操作 關鍵是使用skip_user_input標志並在您的應用程序中檢查它。 基本上,它會向您的應用程序表明您需要處理一些數據。 該應用程序會將其發送回 Watson Assistant 進行響應。 還有基於服務器的對話操作 在這種情況下,Watson Assistant 正在調用 IBM Cloud Functions 操作。 此處提供了使用該方法的教程,與 Db2 數據庫接口

另一種技術是我所說的替換標記 您可以讓 Watson Assistant 返回一個帶有占位符的答案。 您的應用程序將替換這些標記。

第三,您使用 JavaScript 進行異步處理。 似乎在您獲取天氣數據時處理了您的空提示。 天氣的 IF 獨立於空提示。 嘗試修復它。

按照Michal Bida 的建議,我嘗試在雲函數中實現第三方 API 並且它起作用了。 使用openweather map apiphp 實現簡單地創建了一個 php 函數並按照有關如何通過本教程在 php 中創建操作的步驟進行操作。 對於實施,我遵循了有關如何在 watson 助手中實施操作的教程 即使直接從 watson 助手旁邊的聊天機器人調用,它現在也能工作。

它返回的響應示例如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM