Chatbot

Source: Internet
Author: User

Recently, a public platform has been studied to build a chatbot that calls the public interface of Xiaohuang chicken to chat online with Xiaohuang chicken.

Interface calling is not very troublesome, but php version, so I studied the functional modules of C,

Winfrom

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. threading; namespace {public partial class Form1: Form {private static string cookie = null; private string msg = ""; private Thread th1 = null; private Thread th3 = null; private bool changed = false; publi C Form1 () {InitializeComponent (); simsimi. setAllowUnsafeHeaderParsing20 (); Thread th = new Thread (new ThreadStart (Cookie_Thread); H2 = new Thread (new ThreadStart (Hi_Thread )); th3 = new Thread (new ThreadStart (PowerOn_Thread); th. start (); th2.Start (); th3.Start ();} private void button#click (object sender, EventArgs e) {if (textBox1.Text! = "") {Changed = true; msg = textBox1.Text; textBox1.Text = ""; richTextBox1.Text + = String. format ("Me: {0} \ n", msg); changed = false ;}} private static void Cookie_Thread () {cookie = simsimi. getCookie ();} private void PowerOn_Thread () {while (cookie = null) {this. invoke (new Action (Update_PowerText), null); Thread. sleep (1500);} this. invoke (new Action (Update_PowerFinalText), null);} private Void Update_PowerText () {richTextBox1.Text + = "opening chicken... \ N ";} private void Update_PowerFinalText () {richTextBox1.Text + =" well, the chicken is finally opened. \ N chicken: Hello, I'm a chicken. O (& cap; _ & cap;) o \ n ";} private void Hi_Thread () {while (true) {while (changed) {this. invoke (new Action (Update_Text), null); break ;}} private void Update_Text () {richTextBox1.Text + = String. format ("chicken: {0} \ n", simsimi. hi_Simsimi (msg, cookie);} private void textBox1_KeyUp (object sender, KeyEventArgs e) {if (e. keyValue = 13) {if (textBox1.Text! = "") {Changed = true; msg = textBox1.Text; textBox1.Text = ""; richTextBox1.Text + = String. format ("Me: {0} \ n", msg); changed = false ;}} private void richTextBox1_TextChanged (object sender, EventArgs e) {// Set the cursor position this. richTextBox1.SelectionStart = richTextBox1.TextLength-1; // scroll to the current cursor position this. richTextBox1.ScrollToCaret ();}}}
Code

Application class code: the application interface uses a json-format content that is passed back from Xiaohuang chicken. Therefore, I am applying Newtonsoft, a common method on the Internet, to solve the json parsing problem. json. You can download the corresponding DLL from the Internet.

Using System; using System. collections. generic; using System. linq; using System. text; using System. net; using System. IO; using System. IO. compression; using System. reflection; using Newtonsoft. json; using Newtonsoft. json. linq; namespace Xiaoyan chicken {class simsimi {/// <summary> // Cookie // </summary> /// <returns> </returns> public static string GetCookie () {string Cookiesstr = null; CookieCollection cookies = n Ew CookieCollection (); HttpWebRequest request = null; request = (HttpWebRequest) WebRequest. create ("http://www.simsimi.com/talk.htm"); request. cookieContainer = new CookieContainer (); request. cookieContainer. add (cookies); // Get the response from the server and save the cookies from the request .. httpWebResponse response = (HttpWebResponse) request. getResponse (); Cookiesstr = request. cookieContainer. GetCookieHeader (request. requestUri); return Cookiesstr;} public static string Hi_Simsimi (string que, string cookies) {string ans = "let's change to another topic"; string url = String. format ("http://www.simsimi.com/func/reqN? Required ", que); HttpWebRequest hi_request = null; try {hi_request = (HttpWebRequest) WebRequest. create (url); hi_request.Method = "GET"; hi_request.KeepAlive = true; token = false; hi_request.AllowWriteStreamBuffering = false; // terminal information hi_request.Accept = "application/json, text/javascript, */*; q= 0.01 "; hi_reques T. headers. add ("Accept-Language", "zh-cn"); hi_request.Headers.Add ("Accept-Encoding", "gzip, deflate"); hi_request.Headers.Add ("Cookie", cookies + "; simsimi_uid = 1; "); hi_request.Referer =" http://www.simsimi.com/talk.htm "; hi_request.UserAgent =" Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729 ;. NET4.0C ;. NET4.0E) "; hi_request.C OntentType = "application/json; charset = utf-8"; bytes = false; HttpWebResponse hi_response = (HttpWebResponse) hi_request.GetResponse (); StreamReader sr = new StreamReader (hi_response.GetResponseStream (), Encoding. UTF8); ans = sr. readToEnd (); if (ans! = "{}") {JObject jo = JObject. parse (ans); string [] ArrayList = jo. properties (). select (item => item. value. toString ()). toArray (); if (ArrayList [0] = "200") {ans = ArrayList [6];} else {ans = "I didn't understand it. Can I say it again? ";}} Hi_request.Abort (); hi_response.Close ();} catch (System. exception e) {Console. writeLine (e. toString (); // return e. toString (); return "Sorry, I am so sorry ~ ";} Return ans;} public static bool SetAllowUnsafeHeaderParsing20 () {// Get the assembly that contains the internal class Assembly aNetAssembly = Assembly. getAssembly (typeof (System. net. configuration. settingsSection); if (aNetAssembly! = Null) {// Use the assembly in order to get the internal type for the internal class Type aSettingsType = aNetAssembly. getType ("System. net. configuration. settingsSectionInternal "); if (aSettingsType! = Null) {// Use the internal static property to get an instance of the internal settings class. // If the static instance isn' t created allready the property will create it for us. object anInstance = aSettingsType. invokeMember ("Section", BindingFlags. static | BindingFlags. getProperty | BindingFlags. nonPublic, null, null, new object [] {}); if (anInstance! = Null) {// Locate the private bool field that tells the framework is unsafe header parsing shoshould be allowed or not FieldInfo aUseUnsafeHeaderParsing = aSettingsType. getField ("useUnsafeHeaderParsing", BindingFlags. nonPublic | BindingFlags. instance); if (aUseUnsafeHeaderParsing! = Null) {aUseUnsafeHeaderParsing. SetValue (anInstance, true); return true ;}}} return false ;}}}
Code

 

Chatbot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.