2017-06-26 50 views
1

我的問題是我想從webservice讀取數據並將其打印在 chatbot中。如何從webservice讀取數據並使用java代碼在chatbot中打印

web服務將有事件數量及其在它狀態,我必須將數據從互聯網服務和打印複印。我嘗試使用下面的代碼,但我不確定如何在對話聊天機器人中打印這些數據。

protected void doPost(HttpServletRequest request, HttpServletResponse 
response) throws IOException { 
    String requestMessage = request.getParameter("message"); 
    String contextString = request.getParameter("context"); 
    JSONObject contextObject = new JSONObject(); 
    if(contextString != null) {enter code here 
     contextObject = JSONObject.parseObject(contextString); 
    } 
    System.out.println("Context: "); 
    System.out.println(contextObject); 
    Map<String, Object> contextMap = Utility.toMap(contextObject); 
    if(requestMessage == null || requestMessage.isEmpty()){ 
     requestMessage = "Greetings"; 
    } 
    if(requestMessage == "1" || requestMessage == "merge id"){ 
     // throw new NullPointerException(); 
     requestMessage = "call"; 
try { 
     URL e = new URL(
       "webservice url"); 
     HttpURLConnection conn = (HttpURLConnection) e.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Accept", "TEXT/PLAIN"); 
     if (conn.getResponseCode() != 200) { 
      throw new RuntimeException("Failed : HTTP error code : " + 
conn.getResponseCode()); 
     } 

     BufferedReader br = new BufferedReader(new 
InputStreamReader(conn.getInputStream())); 
     System.out.println("Output from Server .... \n"); 

     String output; 
+0

您可以將數據作爲上下文變量傳遞給對話。然後可以在對話節點中使用上下文變量,例如在響應中打印。 –

+0

感謝您的回覆,請以示例幫助我,我是watson和java的新手。 – user2319726

+0

我假設你已經閱讀過這個:https://www.ibm.com/watson/developercloud/doc/conversation/dialog-context.html –

回答

0

在應用程序代碼中,您必須創建/維護一個名爲JSON context的變量。作爲該結構的一部分,您可以使用值定義自己的變量。上下文將通過message API(直接或通過Watson SDK)傳遞給Watson對話。

在Watson Conversation的工作區編輯器中,您現在可以訪問這些上下文變量。這可以作爲對話節點的條件的一部分,在響應文本中,或者將新值分配給上下文變量時。對於你的情況,它可能是一個簡單的$ticket_details。有關詳細信息,請參閱此expression reference

此外,我寫了一個blog entry that explains even more about variables和鏈接到GitHub repository with some extended examples

相關問題