C#_Winform_聊天机器人
最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能。
接口调用不是很麻烦,不过是php版本,所以研究了一下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 th2 = null;
private Thread th3 = null;
private bool changed = false; public Form1()
{
InitializeComponent();
simsimi.SetAllowUnsafeHeaderParsing20();
Thread th = new Thread(new ThreadStart(Cookie_Thread));
th2 = new Thread(new ThreadStart(Hi_Thread));
th3 = new Thread(new ThreadStart(PowerOn_Thread));
th.Start();
th2.Start();
th3.Start();
} private void button1_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 += "正在开鸡中。。。\n";
}
private void Update_PowerFinalText()
{
richTextBox1.Text += "唔,终于开鸡了。\n小贱鸡:你好,我是小贱鸡。o(∩_∩)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("小贱鸡:{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)
{
//设定光标所在位置
this.richTextBox1.SelectionStart = richTextBox1.TextLength-1;
//滚动到当前光标处
this.richTextBox1.ScrollToCaret();
} }
}
Code
应用的类代码:应用接口用小黄鸡那边传回来的是一个json形式的内容,所以应用到json的解析问题,我应用的是网上通用的方法Newtonsoft.Json,可以从网上下载对应的DLL
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 小贱鸡
{
class simsimi
{
/// <summary>
/// Cookie
/// </summary>
/// <returns></returns>
public static string GetCookie()
{
string Cookiesstr = null;
CookieCollection cookies = new 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 = "我们换个话题吧";
string url = String.Format("http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req={0}&fl=http%3A%2F%2Fwww.simsimi.com%2Ftalk.htm", que);
HttpWebRequest hi_request = null;
try
{
hi_request = (HttpWebRequest)WebRequest.Create(url);
hi_request.Method = "GET";
hi_request.KeepAlive = true;
hi_request.ServicePoint.Expect100Continue = false; hi_request.AllowWriteStreamBuffering = false;
//终端信息
hi_request.Accept = "application/json,text/javascript,*/*;q=0.01";
hi_request.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.ContentType = "application/json;charset=utf-8"; hi_request.AllowAutoRedirect = 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 = "没有听懂,可以再说一遍吗?";
}
}
hi_request.Abort();
hi_response.Close();
}
catch (System.Exception e)
{ Console.WriteLine(e.ToString());
//return e.ToString();
return "不好意思死鸡了⊙︿⊙重启下程序吧~";
} 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 should 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
C#_Winform_聊天机器人的更多相关文章
- 【翻译】用AIML实现的Python人工智能聊天机器人
		前言 用python的AIML包很容易就能写一个人工智能聊天机器人. AIML是Artificial Intelligence Markup Language的简写, 但它只是一个简单的XML. 下面 ... 
- 3.C#面向对象基础聊天机器人
		基于控制台的简单版的聊天机器人,词库可以自己添加. 聊天机器人1.0版本 源码如下: using System; using System.Collections.Generic; using Sys ... 
- Python 简易聊天机器人
		聊天机器人 | |-----MySql | |---module--"逻辑运算层" | | | |---ciku--"与词库交互" | | | |---dict ... 
- 使用图灵机器人API实现聊天机器人
		使用图灵机器人的API需要先注册,获取key才行,这我就不说了,自己到http://www.tuling123.com/注册一个账号即可. 下面就是一个简单的python调用API实现聊天机器人的简易 ... 
- AngularJS作出简单聊天机器人
		简单聊天机器人 很初级的对话框形式.以前做对话框使用js,今天尝试使用AngularJS做出来 这里直接使用自己写的JSON数据. <!DOCTYPE html> <html lan ... 
- 用 AIML 开发人工智能聊天机器人
		借助 Python 的 AIML 包,我们很容易实现人工智能聊天机器人.AIML 指的是 Artificial Intelligence Markup Language (人工智能标记语言),它不过是 ... 
- 笔记5:QQ群聊天机器人
		之前经常在别人群里看到有自动回复消息的机器人. 功能有好多,可以玩各种游戏.觉得还蛮有意思的.. 于是就去请教别人怎么弄得,但是他们都说得好复杂,好高大上,无非就是不想让别人弄 本人是个不会轻易放弃的 ... 
- vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)
		使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ... 
- 学习笔记TF059:自然语言处理、智能聊天机器人
		自然语言处理,语音处理.文本处理.语音识别(speech recognition),让计算机能够"听懂"人类语音,语音的文字信息"提取". 日本富国生命保险公司 ... 
随机推荐
- C# 字符串的操作
			var tStr = "0|1:开门|2:关门|3:门检失败|4:开门|5:开门|6:关门"; ).Split(], s.Split(]).Replace(").Repl ... 
- 美团SQL优化工具SQLAdvisor
			介绍 在数据库运维过程中,优化 SQL 是 DBA 团队的日常任务.例行 SQL 优化,不仅可以提升程序性能,还能够降低线上故障的概率. 目前常用的 SQL 优化方式包括但不限于:业务层优化.SQL逻 ... 
- innodb_file_per_table - 转换为InnoDB
			共享InnoDB / var / lib / mysql / ibdata1存储的问题InnoDB表当前将数据和索引存储到共享表空间(/ var / lib / mysql / ibdata1).由于 ... 
- Grafana是一个可视化面板-安装配置介绍
			Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Prometheus和Ope ... 
- [Spark RDD_add_1] groupByKey & reduceBykey 的区别
			[groupByKey & reduceBykey 的区别] 在都能实现相同功能的情况下优先使用 reduceBykey Combine 是为了减少网络负载 1. groupByKey 是没有 ... 
- 前端工程构建工具之Yeoman
			一.Yeoman 简介 通常在开发新项目时我们都需要配置工程环境,开发目录,需要下载一些库.框架文件(如 jQuery.Backbone 等),配置编译环境(Less.Sass.Coffeescrip ... 
- Please select Android SDK解决办法
			项目不能运行,提示如下 打开项目local.properties文件,查看sdk地址是否正确,注意区分大小写 如果sdk地址正确,那么点击File-Sync Project with gradle ... 
- Lr场景设计-hc课堂笔记
			性能测试最基本也是最难的部分:场景设计.瓶颈分析和定位 10个业务10个脚本,每个脚本单独执行:单独场景.10个脚本一起执行:混合场景.10个业务1个脚本:可以设计不同用户登录,分别做不同的操作等场景 ... 
- vue实例的属性和方法
			vue实例的属性和方法 1. 属性 vm.$el #指定要绑定的元素 vm.$data #Vue 实例的数据对象 vm.$options #获取自定义属性的值 new Vue({ customOpti ... 
- Android 打造属于自己的照片选择器
			前言 在做第一个项目时照片选择器使用了开源的PhotoPicker 渐渐无法满足需求,就想着打造一款属于自己的照片选择器. 花了一周的时间完成了该项目,其实代码有一大半并非自己写的,在阅读PhotoP ... 
