gpt4 book ai didi

java - 如何使用java代码从web服务读取数据并在聊天机器人中打印

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:26 26 4
gpt4 key购买 nike

My problem is I want to read data from webservice and print it in chatbot .

Web服务将包含事件编号及其状态,我必须从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;

最佳答案

在应用程序代码中,您必须创建/维护一个名为 JSON context 的变量。作为该结构的一部分,您可以使用值定义自己的变量。上下文通过 message API 传递到 Watson Conversation (直接或通过 Watson SDK )。

在 Watson Conversation 的工作区编辑器中,您现在可以访问这些上下文变量。这可以作为对话节点条件的一部分,在响应文本中,或者在为上下文变量分配新值时。对于您的情况,它可能是一个简单的 $ticket_details。看这个expression reference了解详情。

而且,我写了一个blog entry that explains even more about variables并链接到 GitHub repository with some extended examples .

关于java - 如何使用java代码从web服务读取数据并在聊天机器人中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com