package com.spirit.spirstFly.service; import com.alibaba.da.coin.ide.spi.meta.AskedInfoMsg; import com.alibaba.da.coin.ide.spi.meta.ExecuteCode; import com.alibaba.da.coin.ide.spi.meta.ResultType; import com.alibaba.da.coin.ide.spi.standard.TaskQuery; import com.alibaba.da.coin.ide.spi.standard.TaskResult; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; /** * 解析请求参数,构造向终端接口发送打数据打JSON,之后得到响应数据,提取构造返回给天猫的响应数据 * @author 苗权威 * @dateTime 2019/6/18 */ @Service public class SpiritService { /** * org.slf4j.Logger打印日志信息 */ private static final Logger logger = LoggerFactory.getLogger(SpiritService.class); /** * 获取返回给天猫的数据 * @param request 请求参数 * @return 返回数据封装在开发包中的TaskResult */ public TaskResult getResponse(TaskQuery request) { return getNeededParam(request); } /** * 解析获取需要的参数信息:用户的话,用户的id,意图id * @param request 请求参数 * @return 返回数据封装在开发包中的TaskResult */ private TaskResult getNeededParam(TaskQuery request) { Long userId = request.getBotId(); String userWord = request.getUtterance(); logger.info("The customer say:"+userWord); Long intentId = request.getIntentId(); return postToQiWuInterface(userId, userWord, intentId); } /** * 向接口公司的接口发送数据:user_id 包括用户id, text TTS文本内容,access 来源标识 * 获取返回:audio 音频链接,text TTS文本内容 * @param userId 用户id * @param userWord TTS文本内容 * @param intentId 意图id * @return 返回数据封装在开发包中的TaskResult */ private TaskResult postToQiWuInterface(Long userId, String userWord, Long intentId) { String audioUrl = ""; String textContent = "请说推荐作品"; CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try { httpClient = HttpClientBuilder.create().build(); HttpPost post = new HttpPost("http://aliyun-hz1.chewrobot.com:8070/api/chat"); HashMap params = new HashMap<>(); params.put("user_id", String.valueOf(userId)); params.put("text", userWord); params.put("access","TIANMAO"); String paramsJsonFormat = JSON.toJSONString(params); StringEntity body = new StringEntity(paramsJsonFormat, "UTF-8"); post.setHeader("Content-Type", "application/json;charset=utf-8"); post.setEntity(body); response = httpClient.execute(post); HttpEntity responseBody = response.getEntity(); if (responseBody != null) { String responseContent = EntityUtils.toString(responseBody); logger.info("齐悟接口返回: "+ responseContent); JSONObject responseJsonFormat = JSONObject.parseObject(responseContent); audioUrl = (String) responseJsonFormat.get("audio"); textContent = (String) responseJsonFormat.get("text"); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (httpClient != null) httpClient.close(); if (response != null) response.close(); } catch (IOException e) { e.printStackTrace(); } } return buildTaskResult(audioUrl, textContent, intentId); } /** * 封装返回数据在TaskResult中 * @param audio 音频链接 * @param text TTS文本内容 * @param intentId 意图id * @return 返回数据封装在开发包中的TaskResult */ private TaskResult buildTaskResult(String audio, String text, Long intentId) { TaskResult result = new TaskResult(); result.setReply(text); result.setResultType(ResultType.ASK_INF); result.setExecuteCode(ExecuteCode.SUCCESS); AskedInfoMsg askedInfoMsg = new AskedInfoMsg("any", intentId); ArrayList listInfo = new ArrayList<>(); listInfo.add(askedInfoMsg); result.setAskedInfos(listInfo); // List actions = new ArrayList<>(); // Action action = new Action("audioPlayGenieSource"); // //action.addProperty("audioGenieId", "21441"); // action.addProperty("audioGenieId", audio); // actions.add(action); // result.setActions(actions); return result; } }