1、去http://www.tuling123.com網址創建賬號,創建機器人 重點 2、上代碼 winform界面如上 HttpRequestHelper.PostAsync方法具體如下 winform後臺代碼如下 到此一個簡單調用圖靈機器人完成。 ...
1、去http://www.tuling123.com網址創建賬號,創建機器人
重點
2、上代碼
winform界面如上
HttpRequestHelper.PostAsync方法具體如下
/// <summary> /// 使用post方法非同步請求 /// </summary> /// <param name="url">目標鏈接</param> /// <param name="data">發送的參數字元串</param> /// <returns>返回的字元串</returns> public static async Task<string> PostAsync(string url, string data, Dictionary<string, string> header = null, bool Gzip = false) { using (HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false })) { HttpContent content = new StringContent(data,System.Text.Encoding.UTF8); if (header != null) { client.DefaultRequestHeaders.Clear(); foreach (var item in header) { client.DefaultRequestHeaders.Add(item.Key, item.Value); } } HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); string responseBody; if (Gzip) { GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync()); responseBody = new StreamReader(inputStream).ReadToEnd(); } else { responseBody = await response.Content.ReadAsStringAsync(); } return responseBody; } }
winform後臺代碼如下
public TuLingTest() { InitializeComponent(); } private Action<string> ShowMsg; private void TuLingTest_Load(object sender, EventArgs e) { ShowMsg = new Action<string>((string msg) => { if (Txt_Msg.TextLength > 30000) Txt_Msg.Clear(); Txt_Msg.AppendText("\r\n-------當前時間" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---------------------------------" + "\r\n圖靈機器人回覆:" + msg + "\r\n"); Txt_Msg.ScrollToCaret(); }); } private async void Btn_start_Click(object sender, EventArgs e) { RequestInfo request = new RequestInfo(); UserInfo userInfo = new UserInfo(); userInfo.apiKey = "你的apikey"; Perception perception = new Perception(); InputText inputText = new InputText(); inputText.text = Txt_Reust.Text.Trim(); perception.inputText = inputText; request.perception = perception; request.userInfo = userInfo; var result = await HttpRequestHelper.PostAsync("http://openapi.tuling123.com/openapi/api/v2", JsonConvert.SerializeObject(request)); ResponseInfo response = JsonConvert.DeserializeObject<ResponseInfo>(result); Txt_Msg.BeginInvoke(ShowMsg, response.results[0].values.text); } #region 請求消息 public class RequestInfo { public int reqType { get; set; } public Perception perception { get; set; } public UserInfo userInfo { get; set; } } public class UserInfo { public string apiKey { get; set; } public string userId { get; set; } } public class Perception { public InputText inputText { get; set; } public InputImage inputImage { get; set; } public List<Location> selfInfo { get; set; } } public class Location { public string city { get; set; } public string province { get; set; } public string street { get; set; } } public class InputText { public string text { get; set; } } public class InputImage { public string url { get; set; } } #endregion #region 返回消息 public class ResponseInfo { public Intent intent { get; set; } public List<Results> results { get; set; } } public class Intent { public string code { get; set; } } public class Results { public int groupType { get; set; } public string resultType { get; set; } public Values values { get; set; } } public class Values { public string text { get; set; } } #endregion
到此一個簡單調用圖靈機器人完成。