|
| 1 | +package me.chanjar.weixin.mp.api.impl; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import me.chanjar.weixin.common.WxType; |
| 6 | +import me.chanjar.weixin.common.error.WxError; |
| 7 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 8 | +import me.chanjar.weixin.mp.AiLangType; |
| 9 | +import me.chanjar.weixin.mp.api.WxMpAiOpenService; |
| 10 | +import me.chanjar.weixin.mp.api.WxMpService; |
| 11 | +import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | + |
| 15 | +/** |
| 16 | + * <pre> |
| 17 | + * Created by BinaryWang on 2018/6/9. |
| 18 | + * </pre> |
| 19 | + * |
| 20 | + * @author <a href="https://github.com/binarywang">Binary Wang</a> |
| 21 | + */ |
| 22 | +public class WxMpAiOpenServiceImpl implements WxMpAiOpenService { |
| 23 | + |
| 24 | + private static final JsonParser JSON_PARSER = new JsonParser(); |
| 25 | + public static final String TRANSLATE_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?lfrom=%s<o=%s"; |
| 26 | + private WxMpService wxMpService; |
| 27 | + |
| 28 | + public WxMpAiOpenServiceImpl(WxMpService wxMpService) { |
| 29 | + this.wxMpService = wxMpService; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void uploadVoice(String voiceId, AiLangType lang, File voiceFile) throws WxErrorException { |
| 34 | + if (lang == null) { |
| 35 | + lang = AiLangType.zh_CN; |
| 36 | + } |
| 37 | + |
| 38 | + this.wxMpService.execute(VoiceUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), |
| 39 | + String.format(VOICE_UPLOAD_URL, "mp3", voiceId, lang.getCode()), |
| 40 | + voiceFile); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String recogniseVoice(String voiceId, AiLangType lang, File voiceFile) throws WxErrorException { |
| 45 | + this.uploadVoice(voiceId, lang, voiceFile); |
| 46 | + return this.queryRecognitionResult(voiceId, lang); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException { |
| 51 | + final String responseContent = this.wxMpService.post(String.format(TRANSLATE_URL, langFrom.getCode(), langTo.getCode()), |
| 52 | + content); |
| 53 | + final JsonObject jsonObject = new JsonParser().parse(responseContent).getAsJsonObject(); |
| 54 | + if (jsonObject.get("errcode") == null || jsonObject.get("errcode").getAsInt() == 0) { |
| 55 | + return jsonObject.get("to_content").getAsString(); |
| 56 | + } |
| 57 | + |
| 58 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String queryRecognitionResult(String voiceId, AiLangType lang) throws WxErrorException { |
| 63 | + if (lang == null) { |
| 64 | + lang = AiLangType.zh_CN; |
| 65 | + } |
| 66 | + |
| 67 | + final String responseContent = this.wxMpService.get(VOICE_QUERY_RESULT_URL, |
| 68 | + String.format("voice_id=%s&lang=%s", voiceId, lang.getCode())); |
| 69 | + final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); |
| 70 | + if (jsonObject.get("errcode") == null || jsonObject.get("errcode").getAsInt() == 0) { |
| 71 | + return jsonObject.get("result").getAsString(); |
| 72 | + } |
| 73 | + |
| 74 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP)); |
| 75 | + } |
| 76 | +} |
0 commit comments