using System.Linq; using System.Text.RegularExpressions; using Newtonsoft.Json.Linq; using TencentCloud.Asr.V20190614; using TencentCloud.Asr.V20190614.Models; using TencentCloud.Common; using TencentCloud.Common.Profile; using WingServerCommon.Log; namespace WingASRService.Tencent { public class ASRApi { private ASRApi(){} public static (string message, bool isSuccess) CallASRApi(string url, string fileType) { try { // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取 Credential cred = new Credential { SecretId = "AKIDgDUVFbjbDahYAXeV6RXNxHK8gy0mJabN", SecretKey = "XprdARRBgusa3GEwObYFgrmRq0uYv3vY" }; // Credential cred = new Credential { // SecretId = "AKIDMSIsADUUpvJGwJOKvsb5IDrGtGNBRzzi", // SecretKey = "4sQhdJBl5HI7f3oLbZIlwqXOyILyLWP0" // }; // 实例化一个client选项,可选的,没有特殊需求可以跳过 ClientProfile clientProfile = new ClientProfile(); // 实例化一个http选项,可选的,没有特殊需求可以跳过 HttpProfile httpProfile = new HttpProfile(); httpProfile.Endpoint = ("asr.tencentcloudapi.com"); clientProfile.HttpProfile = httpProfile; // 实例化要请求产品的client对象,clientProfile是可选的 AsrClient client = new AsrClient(cred, "", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 SentenceRecognitionRequest req = new SentenceRecognitionRequest(); req.ProjectId = 0; req.SubServiceType = 2; req.EngSerViceType = "16k_zh_medical"; req.SourceType = 0; req.Url = url; req.VoiceFormat = fileType; req.UsrAudioKey = "any"; // 返回的resp是一个SentenceRecognitionResponse的实例,与请求对象对应 var resp = client.SentenceRecognitionSync(req); return (message: resp.Result, isSuccess: true); } catch (Exception e) { var regMatch = Regex.Match(e.Message,@".*code:([.\w]+)[\s]+.*"); if(regMatch.Groups.Count>1) { var errorCode = regMatch.Groups[1].Value; if(!string.IsNullOrWhiteSpace(errorCode) && ASRErrorCode.ErrorsDictionary.Keys.Contains(errorCode)) { var errorMessage = ASRErrorCode.ErrorsDictionary[errorCode]; Logger.WriteLineError($"ASRApi.CallASRApi code:{errorCode} message:{errorMessage}"); return (message: errorMessage, isSuccess: false); } } Logger.WriteLineError($"ASRApi.CallASRApi error {e}"); return (message: "Unknow error", isSuccess: false);; } } } }