用c#快速實現的智慧聊天機器人

uoou發表於2015-05-29

這是一個聊天機器人,很方便,直接呼叫圖靈機器人api即可,對話方式一問一答! 圖靈機器人官網上目前只給出了php和java的示例,於是自己寫了個Windows的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;//程式需引用Newtonsoft.Json 下載地址http://json.codeplex.com/

namespace TuLing
{
      public partial class MainForm : Form
      {
           HttpWebResponse Response = null;
      public MainForm()
      {
          InitializeComponent();
       }
       //
       /// <summary>
       /// 對話圖靈機器人
       /// </summary>
       /// <param name="p_strMessage"></param>
      /// <returns></returns>
      public string ConnectTuLing(string p_strMessage)
      {
            string result = null;
            try
            {
                //註冊碼自己到網上註冊去
               String APIKEY = "c32ccaa805b6441be76bc18074f12e51";
               String _strMessage = p_strMessage;
               String INFO = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(_strMessage));
               String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
               HttpWebRequest MyRequest = (HttpWebRequest)HttpWebRequest.Create(getURL);
               HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
               Response = MyResponse;
               using (Stream MyStream = MyResponse.GetResponseStream())
              {
                      long ProgMaximum = MyResponse.ContentLength;
                      long totalDownloadedByte = 0;
                      byte[] by = new byte[1024];
                      int osize = MyStream.Read(by, 0, by.Length);
                      Encoding encoding = Encoding.UTF8;
                      while (osize > 0)
                      {
                           totalDownloadedByte = osize + totalDownloadedByte;
                           result += encoding.GetString(by, 0, osize);
                           long ProgValue = totalDownloadedByte;
                           osize = MyStream.Read(by, 0, by.Length);
                       }
                }
                //解析json
                JsonReader reader = new JsonTextReader(new StringReader(result));
                while (reader.Read())
                {
                     //text中的內容才是你需要的
                     if (reader.Path=="text")
                     {
                          //結果賦值
                          result = reader.Value.ToString();
                      }
                     Console.WriteLine(reader.TokenType + "\t\t" + reader.ValueType + "\t\t" + reader.Value);  
                 }
              }
             catch (Exception)
             {
                   throw;
              }
              return result;
        }




       private void btn_send_Click(object sender, EventArgs e)
       {
              string returnMess = ConnectTuLing(rtb_send.Text);
              rtb_mess.Text = returnMess;
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
              this.this.FindForm().Close();
         }

         /// <summary>
         /// 回車快捷鍵
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void rtb_send_KeyDown(object sender, KeyEventArgs e)
         {
              if (e.KeyCode == Keys.Enter)
         {
               rtb_mess.Text=  ConnectTuLing(rtb_send.Text);
          }
       }
   }

enter image description here

相關文章