115 lines
3.9 KiB
C#
115 lines
3.9 KiB
C#
using Api.Framework;
|
||
using AutoAnswer.Entitys;
|
||
using CsharpHttpHelper;
|
||
using Api.Framework.Tools;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
|
||
namespace AutoAnswer
|
||
{
|
||
public static class Tools
|
||
{
|
||
private static List<fl_plugin_autoanswer_definedlib> _WordbanckCache = null;
|
||
|
||
public static List<fl_plugin_autoanswer_definedlib> WordbanckCache
|
||
{
|
||
get
|
||
{
|
||
if (_WordbanckCache == null) RefreshWordbanck1();
|
||
|
||
return _WordbanckCache;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取新的数据
|
||
/// </summary>
|
||
public static void RefreshWordbanck1()
|
||
{
|
||
_WordbanckCache = ApiClient.GetSession().Find<fl_plugin_autoanswer_definedlib>("select * from fl_plugin_autoanswer_definedlib where match_pattern = 1");
|
||
}
|
||
|
||
#region 图灵
|
||
/// <summary>
|
||
/// 图灵API
|
||
/// </summary>
|
||
/// <param name="text"></param>
|
||
/// <returns></returns>
|
||
public static string GetHtml(string text, string userid, string apikey = "")
|
||
{
|
||
userid = Regex.Replace(userid, "[^a-zA-Z0-9\u4e00-\u9fa5]", "");
|
||
userid = string.IsNullOrEmpty(userid) ? Guid.NewGuid().ToString("N") : userid;
|
||
try
|
||
{
|
||
HttpHelper http = new HttpHelper();
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = "http://openapi.tuling123.com/openapi/api/v2",
|
||
Encoding = System.Text.Encoding.GetEncoding("utf-8"),
|
||
Method = "post",
|
||
Postdata = "{\"reqType\":0,\"perception\": {\"inputText\": {\"text\": \"" + String2Unicode(text) + "\"}},\"userInfo\": {\"apiKey\": \"" + (string.IsNullOrEmpty(apikey) ? Class1.Config.TuLing_apikey.Trim() : apikey.Trim()) + "\",\"userId\":\"" + userid + "\"}}",
|
||
ContentType = "text/html"
|
||
};
|
||
HttpResult result = http.GetHtml(item);
|
||
return result.Html;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 字符串转Unicode
|
||
/// </summary>
|
||
/// <param name="source">源字符串</param>
|
||
/// <returns>Unicode编码后的字符串</returns>
|
||
private static string String2Unicode(string source)
|
||
{
|
||
var bytes = Encoding.Unicode.GetBytes(source);
|
||
var stringBuilder = new StringBuilder();
|
||
for (var i = 0; i < bytes.Length; i += 2)
|
||
{
|
||
stringBuilder.AppendFormat("\\u{0:x2}{1:x2}", bytes[i + 1], bytes[i]);
|
||
}
|
||
return stringBuilder.ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region url编码工厂
|
||
/// <summary>
|
||
/// url编码工厂
|
||
/// </summary>
|
||
/// <param name="content"></param>
|
||
/// <param name="flag">编码还是解码,true编码,false解码</param>
|
||
/// <returns></returns>
|
||
public static string UrlEncodFactory(string content, bool flag = false)
|
||
{
|
||
try
|
||
{
|
||
return flag ? HttpUtility.UrlEncode(content, Encoding.UTF8) : HttpUtility.UrlDecode(content, Encoding.UTF8);
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
return string.Empty;
|
||
}
|
||
#endregion
|
||
|
||
private static List<fl_plugin_autoanswer_unprocessed> unprocesseds = null;
|
||
public static List<fl_plugin_autoanswer_unprocessed> FindUnprocesseds(bool flush = false)
|
||
{
|
||
if (flush || unprocesseds == null)
|
||
{
|
||
var db = ApiClient.GetSession();
|
||
unprocesseds = db.Find<fl_plugin_autoanswer_unprocessed>("select * from fl_plugin_autoanswer_unprocessed");
|
||
}
|
||
return unprocesseds;
|
||
}
|
||
|
||
}
|
||
}
|