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),让计算机能够"听懂"人类语音,语音的文字信息"提取". 日本富国生命保险公司 ...
随机推荐
- 高通LCD的pwm背光驱动
发生异常的现象: msm8953 lcd在快速亮灭的情况下背光概率性休眠不灭:测量高通pwm,发现正常的时候pwm的管脚LCM_BL_PWM为低电平,失败的时候为高电平: 根据原理图: mpp是什么? ...
- 【11】python 递归,深度优先搜索与广度优先搜索算法模拟实现
一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件 2.找出这一次和上一次关系 3.假设 ...
- DevExpress09、SimpleButton、CheckButton、DropDownButton、HScrollBar控件和VScrollBar控件
SimpleButton控件 使用SimpleButton控件, 创建一个Button按钮, 可以通过其Image属性添加图片: 该控件与WinForm自带的Button按钮类似: 效果如下: Che ...
- 2017 SDN第一次作业
(1)我会选择的,因为网络现在越来越重要,各行各业都离不开网络,这个方向可以适合各种岗位,感觉比较容易就业.但选这个课是为了多学一点东西,没想太多,嘎嘎嘎. (2)SDNLAB,是一个SDN的大的中文 ...
- SDN 第二次上机作业
SDN第二次上机作业 1.控制器floodlight所示可视化图形拓扑的截图,及主机拓扑连通性检测截图 拓扑 连通性 2.利用字符界面下发流表,使得'h1'和'h2' ping 不通 流表截图 连通性 ...
- python第二十六课——装饰器
装饰器是闭包的一种使用场景: python中的装饰器在定义上需要传入一个函数对象, 在此函数执行之前或者之后都可以追加其它的操作, 这样做的好处是,在不改变源码(原本业务逻辑的)同时,进行功能的扩展: ...
- nowcoder练习赛28
https://www.nowcoder.com/acm/contest/200#question 最近突然找到了打比赛的乐趣,于是参加了这场比赛. 生日宴会:https://www.nowcoder ...
- BZOJ3105:[CQOI2013]新Nim游戏(线性基,贪心)
Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴 ...
- BZOJ2893:征服王(费用流)
Description 虽然春希将信息传递给了雪菜,但是雪菜却好像完全不认得春希了.心急如焚的春希打开了第二世代机能,对雪菜的脑内芯片进行了直连-hack. 进入到雪菜内部的春希发现(这什么玩意..) ...
- PHPer是草根吗
以下文字并没有非常多的技术词汇,所以只要对PHP感兴趣的人都可以看看. PHPer是草根吗? 从PHP诞生之日起,PHP就开始在Web应用方面为广大的程序员服务.同时,作为针对Web开发量身定制的脚本 ...