最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能。

接口调用不是很麻烦,不过是php版本,所以研究了一下C#的功能模块,

Winfrom版

C# Winfrom小黄鸡功能调用

后台界面代码为

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Threading;
 10 
 11 namespace 小贱鸡
 12 {
 13     public partial class Form1 : Form
 14     {
 15         private static string cookie = null;
 16         private string msg = "";
 17         private Thread th2 = null;
 18         private Thread th3 = null;
 19         private bool changed = false;
 20 
 21         public Form1()
 22         {
 23             InitializeComponent();
 24             simsimi.SetAllowUnsafeHeaderParsing20();
 25             Thread th = new Thread(new ThreadStart(Cookie_Thread));
 26             th2 = new Thread(new ThreadStart(Hi_Thread));
 27             th3 = new Thread(new ThreadStart(PowerOn_Thread));
 28             th.Start();
 29             th2.Start();
 30             th3.Start();
 31          }
 32 
 33         private void button1_Click(object sender, EventArgs e)
 34         {
 35             if (textBox1.Text != "")
 36             {
 37                 changed = true;
 38                 msg = textBox1.Text;
 39                 textBox1.Text = "";
 40                 richTextBox1.Text += String.Format("Me:{0}\n", msg);
 41                 changed = false;
 42             }
 43         } 
 44 
 45         private static void Cookie_Thread()
 46         {
 47             cookie = simsimi.GetCookie();
 48         }
 49         private void PowerOn_Thread()
 50         {
 51             while (cookie == null)
 52             {
 53                 this.Invoke(new Action(Update_PowerText), null);
 54                 Thread.Sleep(1500);
 55             }
 56              this.Invoke(new Action(Update_PowerFinalText), null);
 57         }
 58         private void Update_PowerText()
 59         {
 60             richTextBox1.Text += "正在开鸡中。。。\n";
 61         }
 62         private void Update_PowerFinalText()
 63         {
 64             richTextBox1.Text += "唔,终于开鸡了。\n小贱鸡:你好,我是小贱鸡。o(∩_∩)o\n";
 65         }
 66         private void Hi_Thread()
 67         {
 68             while (true)
 69             {
 70                 while (changed)
 71                 {
 72                     this.Invoke(new Action(Update_Text), null);
 73                     break;
 74                 }
 75             }
 76         }
 77         private void Update_Text()
 78         {
 79             richTextBox1.Text += String.Format("小贱鸡:{0}\n", simsimi.Hi_Simsimi(msg,cookie));
 80         }
 81 
 82         private void textBox1_KeyUp(object sender, KeyEventArgs e)
 83        {
 84             if (e.KeyValue == 13)
 85             {
 86                 if (textBox1.Text != "")
 87                 {
 88                     changed = true;
 89                     msg = textBox1.Text;
 90                     textBox1.Text = "";
 91                     richTextBox1.Text += String.Format("Me:{0}\n", msg);
 92                     changed = false;
 93                 }          
 94             }
 95         }
 96 
 97         private void richTextBox1_TextChanged(object sender, EventArgs e)
 98         {
 99             //设定光标所在位置 
100             this.richTextBox1.SelectionStart = richTextBox1.TextLength-1;
101             //滚动到当前光标处 
102             this.richTextBox1.ScrollToCaret();
103         } 
104 
105     }
106 }
View Code

相关文章: