281 lines
11 KiB
C#
281 lines
11 KiB
C#
using AY.CRM.BLL.Wx;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using Quartz;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data.Entity;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using WX.CRM.BLL.Csvr;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.Model.Entity;
|
||
|
||
namespace WX.CRM.CRMServices.WeiXin
|
||
{
|
||
public class WxRecordToText
|
||
{
|
||
|
||
public void Start()
|
||
{
|
||
|
||
WX_MANUALADDWEIXINFRIEND_BL wx_ManualAddWeiXinFriend_BL = new WX_MANUALADDWEIXINFRIEND_BL();
|
||
CSVR_CALLRECORD_BL cSVR_CALLRECORD_BL = new CSVR_CALLRECORD_BL();
|
||
string path = Utility.GetSettingByKey("RecordToText");
|
||
LogHelper.Error("录音地址:" + path);
|
||
IEnumerable<WX_MANUALADDWEIXINFRIEND> data = wx_ManualAddWeiXinFriend_BL.GetList(p => p.ADDTYPE == 1 && p.RECORDID > 0 && string.IsNullOrEmpty(p.RECORDTXT)).Take(90);
|
||
foreach (var item in data)
|
||
{
|
||
CSVR_CALLRECORD model = cSVR_CALLRECORD_BL.Get(p => p.RECORDID == item.RECORDID);
|
||
if (model != null)
|
||
{
|
||
item.RECORDTXT = Transcription.GetData(path + "111.mp3");
|
||
wx_ManualAddWeiXinFriend_BL.Update(item);
|
||
LogHelper.Error("录音的内容是:" + item.RECORDTXT);
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
public class WxRecordToTextJob : IJob
|
||
{
|
||
static bool _isNotice = false;
|
||
|
||
public void Execute(JobExecutionContext context)
|
||
{
|
||
LogHelper.Error("测试录音转文字");
|
||
if (_isNotice)
|
||
return;
|
||
try
|
||
{
|
||
_isNotice = true;
|
||
new WxRecordToText().Start();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error("WX.CRM.WinService.WeiXin.WxRecordToTextJob.Execute:" + ex);
|
||
}
|
||
finally
|
||
{
|
||
_isNotice = false;
|
||
}
|
||
throw new NotImplementedException();
|
||
}
|
||
}
|
||
|
||
public class RequestBody
|
||
{
|
||
public string app_key { get; set; } //appkey 应用的key
|
||
public string oss_link { get; set; } //语音文件存储地址
|
||
|
||
}
|
||
public class HttpUtil
|
||
{
|
||
/*
|
||
* 计算MD5+BASE64
|
||
*/
|
||
public static string MD5Base64(string s)
|
||
{
|
||
if (s == null)
|
||
return null;
|
||
string encodeStr = "";
|
||
//string 编码必须为utf-8
|
||
byte[] utfBytes = Encoding.UTF8.GetBytes(s);
|
||
|
||
MD5 mdTemp;
|
||
try
|
||
{
|
||
mdTemp = new MD5CryptoServiceProvider();
|
||
byte[] md5Bytes = mdTemp.ComputeHash(utfBytes);
|
||
encodeStr = Convert.ToBase64String(md5Bytes);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("Failed to generate MD5 : " + ex.Message);
|
||
}
|
||
return encodeStr;
|
||
}
|
||
|
||
/*
|
||
* 计算 HMAC-SHA1
|
||
*/
|
||
public static string HMACSha1(string data, string key)
|
||
{
|
||
string result;
|
||
try
|
||
{
|
||
HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(key));
|
||
byte[] rstRes = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(data));
|
||
result = Convert.ToBase64String(rstRes);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("Failed to generate HMAC : " + ex.Message);
|
||
}
|
||
return result;
|
||
}
|
||
/*
|
||
* 发送POST请求
|
||
*/
|
||
public static HttpWebResponse sendPost(string url, string body, string ak_id, string ak_secret)
|
||
{
|
||
string method = "POST";
|
||
string accept = "application/json";
|
||
string content_type = "application/json";
|
||
DateTime date = DateTime.Now.ToUniversalTime();
|
||
// 1.对body做MD5+BASE64加密
|
||
string bodyMd5 = HttpUtil.MD5Base64(body);
|
||
string stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date.ToString("r");
|
||
// 2.计算 HMAC-SHA1
|
||
string signature = HttpUtil.HMACSha1(stringToSign, ak_secret);
|
||
// 3.得到 authorization header
|
||
string authHeader = "Dataplus " + ak_id + ":" + signature;
|
||
|
||
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
||
httpWebRequest.Method = method;
|
||
httpWebRequest.Accept = accept;
|
||
httpWebRequest.ContentType = content_type;
|
||
httpWebRequest.Date = date;
|
||
//httpWebRequest.Headers["Date"] = date.ToString("r");
|
||
httpWebRequest.Headers["Authorization"] = authHeader;
|
||
byte[] data = Encoding.UTF8.GetBytes(body);
|
||
httpWebRequest.GetRequestStream().Write(data, 0, data.Length);
|
||
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
|
||
|
||
return httpResponse;
|
||
}
|
||
///*
|
||
// * GET请求
|
||
// */
|
||
public static string sendGet(string url, string task_id, string ak_id, string ak_secret)
|
||
{
|
||
string result = "";
|
||
try
|
||
{
|
||
Uri realUrl = new Uri(url + "/" + task_id);
|
||
/*
|
||
* http header 参数
|
||
*/
|
||
string method = "GET";
|
||
string accept = "application/json";
|
||
string content_type = "application/json";
|
||
//string path = realUrl.getFile();
|
||
DateTime date = DateTime.Now.ToUniversalTime();
|
||
// 1.对body做MD5+BASE64加密
|
||
//string bodyMd5 = MD5Base64("");
|
||
string stringToSign = method + "\n" + accept + "\n" + "" + "\n" + content_type + "\n" + date.ToString("r");
|
||
// 2.计算 HMAC-SHA1
|
||
string signature = HMACSha1(stringToSign, ak_secret);
|
||
// 3.得到 authorization header
|
||
string authHeader = "Dataplus " + ak_id + ":" + signature;
|
||
// 打开和URL之间的连接
|
||
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(realUrl);
|
||
|
||
httpWebRequest.Method = method;
|
||
httpWebRequest.Accept = accept;
|
||
httpWebRequest.ContentType = content_type;
|
||
httpWebRequest.Date = date;
|
||
httpWebRequest.Headers["Authorization"] = authHeader;
|
||
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
|
||
if (httpResponse.StatusCode == HttpStatusCode.OK)
|
||
{
|
||
using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
}
|
||
httpResponse.Dispose();
|
||
return result;
|
||
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
throw e;
|
||
}
|
||
|
||
}
|
||
}
|
||
public class Transcription
|
||
{
|
||
private static string url = "https://nlsapi.aliyun.com/transcriptions";
|
||
private static RequestBody body = new RequestBody();
|
||
private static HttpUtil request = new HttpUtil();
|
||
private static string ak_id = Utility.GetSettingByKey("RTTAccessId"); //数加管控台获得的accessId
|
||
private static string ak_secret = Utility.GetSettingByKey("RTTAccessSecret"); // 数加管控台获得的accessSecret
|
||
public static string GetData(string oss_link)
|
||
{
|
||
string result = "";
|
||
body.app_key = "nls-service-telephone8khz"; //简介页面给出的Appkey //body.oss_link = "http://qq1193545725.oss-cn-shenzhen.aliyuncs.com/932a631c-2112-11e8-8a5b-511ac20b5043.mp3";//离线文件识别的文件url,推荐使用oss存储文件。链接大小限制为128MB
|
||
body.oss_link = oss_link;
|
||
//热词接口
|
||
//使用热词需要指定Vocabulary_id字段,如何设置热词参考文档:[热词设置](~~49179~~)
|
||
//body.setVocabulary_id("vocab_id");
|
||
|
||
/* 获取完整识别结果,无需设置本参数!*/
|
||
//body.addValid_time(100,2000,0); //validtime 可选字段 设置的是语音文件中希望识别的内容,begintime,endtime以及channel
|
||
//body.addValid_time(2000,10000,1); //validtime 默认不设置。可选字段 设置的是语音文件中希望识别的内容,begintime,endtime以及channel
|
||
/* 获取完整识别结果,无需设置本参数!*/
|
||
/*
|
||
* 发送录音转写请求
|
||
* **/
|
||
|
||
string bodystring;
|
||
bodystring = JsonConvert.SerializeObject(body);
|
||
#region 关键json格式处理
|
||
string[] data = bodystring.Substring(1, bodystring.Length - 2).Split(',');
|
||
StringBuilder sb = new StringBuilder();
|
||
foreach (var item in data)
|
||
{
|
||
sb.Append("\n\t" + item + ",");
|
||
}
|
||
sb.Remove(sb.Length - 1, 1);
|
||
sb.Append("\n}");
|
||
string str = "{" + sb.ToString();
|
||
#endregion
|
||
HttpWebResponse httpResponse = HttpUtil.sendPost(url, str, ak_id, ak_secret);
|
||
string message = "";
|
||
if (httpResponse.StatusCode == HttpStatusCode.OK) //200
|
||
{
|
||
using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
|
||
{
|
||
message = reader.ReadToEnd();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
result = "error msg: " + httpResponse.StatusCode.ToString();
|
||
}
|
||
/*
|
||
* 通过TaskId获取识别结果
|
||
* **/
|
||
if (httpResponse.StatusCode == HttpStatusCode.OK) //200
|
||
{
|
||
string TaskId = ((JObject)JsonConvert.DeserializeObject(message))["id"].ToString();
|
||
string status = "RUNNING";
|
||
|
||
while (status.Equals("RUNNING"))
|
||
{
|
||
Thread.Sleep(10000);
|
||
result = HttpUtil.sendGet(url, TaskId, ak_id, ak_secret);
|
||
status = ((JObject)JsonConvert.DeserializeObject(result))["status"].ToString();
|
||
if (status == "SUCCEED")
|
||
{
|
||
var model = ((JObject)JsonConvert.DeserializeObject(result))["result"];
|
||
result = "";
|
||
for (int i = 0; i < model.Count(); i++)
|
||
{
|
||
result += model[i]["text"].ToString() + "。";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
httpResponse.Dispose();
|
||
return result;
|
||
}
|
||
}
|
||
}
|