12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 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.Config;
- using WingServerCommon.Config.Parameters;
- 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 = EnvironmentConfigManager.GetParammeter<StringParameter>("ASR", "AppKey").Value,
- SecretKey = EnvironmentConfigManager.GetParammeter<StringParameter>("ASR", "AppSecret").Value
- };
- // 实例化一个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";
- req.ConvertNumMode = 3;
- // 返回的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 url:{url} code:{errorCode} message:{errorMessage}");
- return (message: errorMessage, isSuccess: false);
- }
- }
- Logger.WriteLineError($"ASRApi.CallASRApi url:{url} error {e}");
- return (message: "Unknow error", isSuccess: false); ;
- }
- }
- }
- }
|