初始化CRICS
This commit is contained in:
151
Common/XidaoDuOperation.cs
Normal file
151
Common/XidaoDuOperation.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
public static class XiaoDuOperation
|
||||
{
|
||||
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(XiaoDuOperation));
|
||||
private static readonly string _welcomeURL = "https://dueros.baidu.com/business/open/restful";
|
||||
/// <summary>
|
||||
/// 获取AccessToken
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static XiaoDuError GetXiaoDuAccessToken()
|
||||
{
|
||||
try
|
||||
{
|
||||
StringBuilder sbURL = new StringBuilder();
|
||||
sbURL.Append("https://dueros.baidu.com/business/oauth/v2/token");
|
||||
sbURL.Append("?grant_type=client_credentials");
|
||||
sbURL.Append("&app_id=vG6mcOYY7kBvGv8GGhqLe36t9wAWQHjH");
|
||||
sbURL.Append("&app_secret=c7ulEmnWSATaEBLXjV35Oj3onxRmhuNQ");
|
||||
string result = HttpWebRequestHelper.GetWebRequest(sbURL.ToString());
|
||||
XiaoDuError error = Newtonsoft.Json.JsonConvert.DeserializeObject<XiaoDuError>(result);
|
||||
if (error.errno > 0)
|
||||
{
|
||||
logger.Error(string.Format("获取小度accessToken失败。errno:{0},errmsg:{1}", error.errno, error.errmsg));
|
||||
}
|
||||
return error;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(string.Format("获取小度accessToken失败:{0}", ex.Message));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送命令给小度
|
||||
/// </summary>
|
||||
/// <param name="accessToken"></param>
|
||||
/// <param name="cuid"></param>
|
||||
/// <param name="paramJson"></param>
|
||||
/// <param name="hotelCode"></param>
|
||||
/// <param name="roomNumber"></param>
|
||||
/// <returns></returns>
|
||||
public static void PostWebRequestToXiaoDu(string accessToken, string cuid, XiaoDuParamJson paramJson, string hotelCode, string roomNumber)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(cuid))
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("cuid=" + cuid);
|
||||
sb.Append("×tamp=" + TimeHelper.GetCurrentTimestamp(false));
|
||||
sb.Append("&v=2.0");
|
||||
sb.Append("&source=baidu");
|
||||
sb.Append("¶mJson=" + Newtonsoft.Json.JsonConvert.SerializeObject(paramJson));
|
||||
HttpWebRequestHelper.PostWebRequest(_welcomeURL, "Bearer " + accessToken, sb.ToString());
|
||||
//string result = HttpWebRequestHelper.PostWebRequest(_welcomeURL, "Bearer " + accessToken, sb.ToString());
|
||||
//XiaoDuError error = Newtonsoft.Json.JsonConvert.DeserializeObject<XiaoDuError>(result);
|
||||
//if (error.errno > 0)
|
||||
//{
|
||||
// logger.Error(string.Format("酒店({0})客房({1})调用小度接口失败。cuid:{2}, method:{3}, errmsg:{4}, error:{5}", hotelCode, roomNumber, cuid, paramJson.method, error.errmsg, result));
|
||||
//}
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// logger.Error(string.Format("酒店({0})客房({1})调用小度接口失败。cuid:{2}, accessToken:{3}", hotelCode, roomNumber, cuid, accessToken));
|
||||
//}
|
||||
}
|
||||
/// <summary>
|
||||
/// 通知小度播报欢迎词
|
||||
/// </summary>
|
||||
/// <param name="hotelCode"></param>
|
||||
/// <param name="roomNumber"></param>
|
||||
/// <param name="webhookUrl"></param>
|
||||
/// <param name="message"></param>
|
||||
public static void UploadWebhook(string accessToken, string cuids, string message, string hotelCode, string roomNumber)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (string cuid in cuids.Split(','))//多个小度英文逗号隔开
|
||||
{
|
||||
XiaoDuParamJson paramJson = new XiaoDuParamJson()
|
||||
{
|
||||
method = "welcome",
|
||||
cuid = cuid,
|
||||
outputSpeechText = message,
|
||||
cardContent = message,
|
||||
backgroundMusic = "http://www.abc.com"
|
||||
};
|
||||
PostWebRequestToXiaoDu(accessToken, cuid, paramJson, hotelCode, roomNumber);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(string.Format("酒店({0})客房({1})调用小度接口({2})失败。原因:{3}", hotelCode, roomNumber, _welcomeURL, ex.Message));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 同步设备技能
|
||||
/// </summary>
|
||||
/// <param name="hotelName"></param>
|
||||
/// <param name="roomNumber"></param>
|
||||
/// <param name="cuids"></param>
|
||||
/// <param name="accessToken"></param>
|
||||
/// <returns></returns>
|
||||
//public static bool UploadDeviceFun(string hotelName, string roomNumber, string cuids, string accessToken)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// XiaoDuParamJson paramJson = new XiaoDuParamJson() { method = "deviceSync", isNoBlocked = 1 };
|
||||
// foreach (string cuid in cuids.Split(','))//多个小度英文逗号隔开
|
||||
// {
|
||||
// PostWebRequestToXiaoDu(accessToken, cuid, paramJson);
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// logger.Error(string.Format("酒店({0})客房({1})调用小度接口({2})失败:{3}", hotelName, roomNumber, _welcomeURL, ex));
|
||||
// }
|
||||
// return false;
|
||||
//}
|
||||
}
|
||||
|
||||
public class XiaoDuParamJson
|
||||
{
|
||||
public string method { get; set; }
|
||||
public string cuid { get; set; }
|
||||
public string outputSpeechText { get; set; }
|
||||
public string cardContent { get; set; }
|
||||
public string backgroundMusic { get; set; }
|
||||
public int isNoBlocked { get; set; }
|
||||
}
|
||||
|
||||
public class XiaoDuError
|
||||
{
|
||||
public int errno { get; set; }
|
||||
public string errmsg { get; set; }
|
||||
public XiaoDuErrorData data { get; set; }
|
||||
}
|
||||
|
||||
public class XiaoDuErrorData
|
||||
{
|
||||
public string access_token { get; set; }
|
||||
public int expires_in { get; set; }
|
||||
public int deadline { get; set; }
|
||||
public string refresh_token { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user