|
| 1 | +package me.chanjar.weixin.aispeech.api.impl; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.reflect.TypeToken; |
| 6 | +import java.lang.reflect.Type; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import me.chanjar.weixin.aispeech.api.WxAispeechDialogService; |
| 11 | +import me.chanjar.weixin.aispeech.bean.dialog.AispeechApiResponse; |
| 12 | +import me.chanjar.weixin.aispeech.bean.dialog.AsyncTaskResult; |
| 13 | +import me.chanjar.weixin.aispeech.bean.dialog.BotIntent; |
| 14 | +import me.chanjar.weixin.aispeech.bean.dialog.DialogQueryRequest; |
| 15 | +import me.chanjar.weixin.aispeech.bean.dialog.DialogResult; |
| 16 | +import me.chanjar.weixin.aispeech.bean.dialog.PublishProgress; |
| 17 | +import me.chanjar.weixin.aispeech.util.WxAispeechSignUtil; |
| 18 | +import me.chanjar.weixin.common.error.WxError; |
| 19 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 20 | +import me.chanjar.weixin.common.util.json.WxGsonBuilder; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | + |
| 23 | +public class WxAispeechDialogServiceImpl implements WxAispeechDialogService { |
| 24 | + private final WxAispeechServiceImpl service; |
| 25 | + |
| 26 | + public WxAispeechDialogServiceImpl(WxAispeechServiceImpl service) { |
| 27 | + this.service = service; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public String getAccessToken(String appid, String account) throws WxErrorException { |
| 32 | + Map<String, String> request = new HashMap<>(); |
| 33 | + if (StringUtils.isNotBlank(account)) { |
| 34 | + request.put("account", account); |
| 35 | + } |
| 36 | + |
| 37 | + String response = service.executeDialogPost("/v2/token", request, false, appid); |
| 38 | + Type type = new TypeToken<AispeechApiResponse<JsonObject>>() { } .getType(); |
| 39 | + AispeechApiResponse<JsonObject> result = WxGsonBuilder.create().fromJson(response, type); |
| 40 | + ensureSuccess(result); |
| 41 | + String token = result.getData().get("access_token").getAsString(); |
| 42 | + service.getConfigStorage().setOpenAiToken(token); |
| 43 | + return token; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String importBotJson(int mode, List<BotIntent> data) throws WxErrorException { |
| 48 | + Map<String, Object> request = new HashMap<>(); |
| 49 | + request.put("mode", mode); |
| 50 | + request.put("data", data); |
| 51 | + |
| 52 | + String response = service.executeDialogPost("/v2/bot/import/json", request, true, null); |
| 53 | + Type type = new TypeToken<AispeechApiResponse<JsonObject>>() { } .getType(); |
| 54 | + AispeechApiResponse<JsonObject> result = WxGsonBuilder.create().fromJson(response, type); |
| 55 | + ensureSuccess(result); |
| 56 | + return result.getData().get("task_id").getAsString(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String publishBot() throws WxErrorException { |
| 61 | + String response = service.executeDialogPost("/v2/bot/publish", "{}", true, null); |
| 62 | + Type type = new TypeToken<AispeechApiResponse<JsonObject>>() { } .getType(); |
| 63 | + AispeechApiResponse<JsonObject> result = WxGsonBuilder.create().fromJson(response, type); |
| 64 | + ensureSuccess(result); |
| 65 | + return result.getRequestId(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public PublishProgress getPublishProgress(String env) throws WxErrorException { |
| 70 | + Map<String, String> request = new HashMap<>(); |
| 71 | + request.put("env", env); |
| 72 | + |
| 73 | + String response = service.executeDialogPost("/v2/bot/effective_progress", request, true, null); |
| 74 | + Type type = new TypeToken<AispeechApiResponse<PublishProgress>>() { } .getType(); |
| 75 | + AispeechApiResponse<PublishProgress> result = WxGsonBuilder.create().fromJson(response, type); |
| 76 | + ensureSuccess(result); |
| 77 | + return result.getData(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public AsyncTaskResult queryAsyncTask(String taskId) throws WxErrorException { |
| 82 | + Map<String, String> request = new HashMap<>(); |
| 83 | + request.put("task_id", taskId); |
| 84 | + |
| 85 | + String response = service.executeDialogPost("/v2/async/fetch", request, true, null); |
| 86 | + Type type = new TypeToken<AispeechApiResponse<AsyncTaskResult>>() { } .getType(); |
| 87 | + AispeechApiResponse<AsyncTaskResult> result = WxGsonBuilder.create().fromJson(response, type); |
| 88 | + ensureSuccess(result); |
| 89 | + return result.getData(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public DialogResult query(DialogQueryRequest request) throws WxErrorException { |
| 94 | + String json = WxGsonBuilder.create().toJson(request); |
| 95 | + String encrypted = WxAispeechSignUtil.encryptAesCbcToBase64(json, service.getConfigStorage().getAesKey()); |
| 96 | + String response = service.executeDialogPost("/v2/bot/query", encrypted, true, null); |
| 97 | + |
| 98 | + String responseJson = response; |
| 99 | + if (!looksLikeJson(response)) { |
| 100 | + responseJson = WxAispeechSignUtil.decryptAesCbcFromBase64(response, service.getConfigStorage().getAesKey()); |
| 101 | + } |
| 102 | + |
| 103 | + Type type = new TypeToken<AispeechApiResponse<DialogResult>>() { } .getType(); |
| 104 | + AispeechApiResponse<DialogResult> result = WxGsonBuilder.create().fromJson(responseJson, type); |
| 105 | + ensureSuccess(result); |
| 106 | + |
| 107 | + DialogResult dialogResult = result.getData(); |
| 108 | + if (dialogResult != null && looksLikeJson(dialogResult.getAnswer())) { |
| 109 | + dialogResult.setRawAnswer(WxGsonBuilder.create().fromJson(dialogResult.getAnswer(), JsonElement.class)); |
| 110 | + } |
| 111 | + return dialogResult; |
| 112 | + } |
| 113 | + |
| 114 | + private boolean looksLikeJson(String value) { |
| 115 | + return StringUtils.isNotBlank(value) && (value.startsWith("{") || value.startsWith("[")); |
| 116 | + } |
| 117 | + |
| 118 | + private void ensureSuccess(AispeechApiResponse<?> response) throws WxErrorException { |
| 119 | + if (response == null) { |
| 120 | + throw new WxErrorException("响应为空"); |
| 121 | + } |
| 122 | + if (response.getCode() == null || response.getCode() != 0) { |
| 123 | + throw new WxErrorException(WxError.builder() |
| 124 | + .errorCode(response.getCode() == null ? -1 : response.getCode()) |
| 125 | + .errorMsg(response.getMsg()) |
| 126 | + .build()); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments