174 lines
7.3 KiB
C#
174 lines
7.3 KiB
C#
using Aliyun.Acs.Core;
|
||
using Aliyun.Acs.Core.Exceptions;
|
||
using Aliyun.Acs.Core.Http;
|
||
using Aliyun.Acs.Core.Profile;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WX.CRM.Common;
|
||
|
||
namespace Core.VoiceConsole
|
||
{
|
||
class Program
|
||
{
|
||
|
||
// 地域ID,固定值。
|
||
public const string REGIONID = "cn-shanghai";
|
||
public const string PRODUCT = "nls-filetrans";
|
||
public const string DOMAIN = "filetrans.cn-shanghai.aliyuncs.com";
|
||
public const string API_VERSION = "2018-08-17";
|
||
public const string POST_REQUEST_ACTION = "SubmitTask";
|
||
public const string GET_REQUEST_ACTION = "GetTaskResult";
|
||
// 请求参数
|
||
public const string KEY_APP_KEY = "appkey";
|
||
public const string KEY_FILE_LINK = "file_link";
|
||
public const string KEY_VERSION = "version";
|
||
public const string KEY_ENABLE_WORDS = "enable_words";
|
||
// 响应参数
|
||
public const string KEY_TASK = "Task";
|
||
public const string KEY_TASK_ID = "TaskId";
|
||
public const string KEY_STATUS_TEXT = "StatusText";
|
||
// 状态值
|
||
public const string STATUS_SUCCESS = "SUCCESS";
|
||
public const string STATUS_RUNNING = "RUNNING";
|
||
public const string STATUS_QUEUEING = "QUEUEING";
|
||
|
||
public const string enable_callback = "enable_callback";
|
||
static void Main(string[] args)
|
||
{
|
||
string retmsg = Utility.PostAjaxData("http://qm.dn8188.com:8031/CallBack/Test", Newtonsoft.Json.JsonConvert.SerializeObject(new{ ax="ss"}), Encoding.UTF8);//返回信息
|
||
return;
|
||
|
||
//if (args.Length < 3)
|
||
//{
|
||
// System.Console.WriteLine("FileTrans Demo need params: <AccessKey Id> <AccessKey Secret> <app-key>");
|
||
// return;
|
||
//}
|
||
string accessKeyId = "LTAI4GBSG4oKCDViiLKj8exo";
|
||
string accessKeySecret = "WqTYTI6bvUFxZ60DVNnjjBjlwrhgKE";
|
||
string appKey = "YDl2NkFJjHtWnVQz";
|
||
string fileLink = "http://qm.dn8188.com:8099/e378eeb195517d0ea1e395068fe59c15.mp3";
|
||
/**
|
||
* 创建阿里云鉴权对象
|
||
*/
|
||
IClientProfile profile = DefaultProfile.GetProfile(
|
||
REGIONID,
|
||
accessKeyId,
|
||
accessKeySecret
|
||
);
|
||
DefaultAcsClient client = new DefaultAcsClient(profile);
|
||
try
|
||
{
|
||
/**
|
||
* 创建录音文件识别请求,设置请求参数。
|
||
*/
|
||
CommonRequest request = new CommonRequest();
|
||
request.Domain = DOMAIN;
|
||
request.Version = API_VERSION;
|
||
request.Action = POST_REQUEST_ACTION;
|
||
request.Product = PRODUCT;
|
||
request.Method = MethodType.POST;
|
||
// 设置task,以JSON字符串形式设置到请求Body中。
|
||
JObject obj = new JObject();
|
||
obj[KEY_APP_KEY] = appKey;
|
||
obj[KEY_FILE_LINK] = fileLink;
|
||
// 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置。
|
||
obj[KEY_VERSION] = "4.0";
|
||
// 设置是否输出词信息,默认为false。开启时需要设置version为4.0。
|
||
obj[KEY_ENABLE_WORDS] = false;
|
||
obj["enable_sample_rate_adaptive"] = true;
|
||
obj["enable_disfluency"] = true;
|
||
|
||
//obj["enable_callback"] = true;
|
||
//obj["callback_url"] = "http://qm.dn8188.com:8031/Voice/callback";
|
||
string task = obj.ToString();
|
||
request.AddBodyParameters(KEY_TASK, task);
|
||
/**
|
||
* 提交录音文件识别请求,处理服务端返回的响应。
|
||
*/
|
||
CommonResponse response = client.GetCommonResponse(request);
|
||
System.Console.WriteLine(response.Data);
|
||
if (response.HttpStatus != 200)
|
||
{
|
||
System.Console.WriteLine("录音文件识别请求失败: " + response.HttpStatus);
|
||
return;
|
||
}
|
||
// 获取录音文件识别请求任务ID,以供识别结果查询使用。
|
||
string taskId = "";
|
||
JObject jsonObj = JObject.Parse(response.Data);
|
||
string statusText = jsonObj[KEY_STATUS_TEXT].ToString();
|
||
if (statusText.Equals(STATUS_SUCCESS))
|
||
{
|
||
System.Console.WriteLine("录音文件识别请求成功响应!");
|
||
taskId = jsonObj[KEY_TASK_ID].ToString();
|
||
}
|
||
else
|
||
{
|
||
System.Console.WriteLine("录音文件识别请求失败!");
|
||
return;
|
||
}
|
||
/**
|
||
* 创建识别结果查询请求,并设置查询参数为任务ID。
|
||
*/
|
||
CommonRequest getRequest = new CommonRequest();
|
||
getRequest.Domain = DOMAIN;
|
||
getRequest.Version = API_VERSION;
|
||
getRequest.Action = GET_REQUEST_ACTION;
|
||
getRequest.Product = PRODUCT;
|
||
getRequest.Method = MethodType.GET;
|
||
getRequest.AddQueryParameters(KEY_TASK_ID, taskId);
|
||
/**
|
||
* 提交录音文件识别结果查询请求
|
||
* 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述为“SUCCESS”、“SUCCESS_WITH_NO_VALID_FRAGMENT”,
|
||
* 或者为错误描述,则结束轮询。
|
||
*/
|
||
statusText = "";
|
||
while (true)
|
||
{
|
||
CommonResponse getResponse = client.GetCommonResponse(getRequest);
|
||
System.Console.WriteLine(getResponse.Data);
|
||
if (getResponse.HttpStatus != 200)
|
||
{
|
||
System.Console.WriteLine("识别结果查询请求失败,Http错误码:" + getResponse.HttpStatus);
|
||
break;
|
||
}
|
||
JObject jsonObj2 = JObject.Parse(getResponse.Data);
|
||
statusText = jsonObj2[KEY_STATUS_TEXT].ToString();
|
||
if (statusText.Equals(STATUS_RUNNING) || statusText.Equals(STATUS_QUEUEING))
|
||
{
|
||
// 继续轮询
|
||
System.Threading.Thread.Sleep(2 * 1000);
|
||
}
|
||
else
|
||
{
|
||
// 退出轮询
|
||
break;
|
||
}
|
||
}
|
||
if (statusText.Equals(STATUS_SUCCESS))
|
||
{
|
||
System.Console.WriteLine("录音文件识别成功!");
|
||
}
|
||
else
|
||
{
|
||
System.Console.WriteLine("录音文件识别失败!");
|
||
}
|
||
}
|
||
catch (ServerException ex)
|
||
{
|
||
System.Console.WriteLine(ex.ToString());
|
||
}
|
||
catch (ClientException ex)
|
||
{
|
||
System.Console.WriteLine(ex.ToString());
|
||
}
|
||
finally {
|
||
System.Console.ReadLine();
|
||
}
|
||
}
|
||
}
|
||
}
|