C# Winfrom小黄鸡功能调用

by admin on 2019年9月5日

直白从大家网小黄鸡代码里面提取一下了。

近些日子研讨微信公众平台,搭建了多个微信聊天机器人,调用小黄鸡的公众接口,完毕在线和小黄鸡聊天的职能。

近来钻探微教徒人平台,搭建了三个微信聊天机器人,调用小黄鸡的万众接口,达成在线和小黄鸡聊天的效应。

小黄鸡网页版 小黄鸡爆笑聊天工具

笔者:西凉来源:未知公布时间:二〇一二-02-22 14:56:51

二〇一三年七月,“小黄鸡”在大家网设置公共主页,火速火起来。那只有意思的、能用萌语即时回应各个提问的“小黄鸡”仅用了11天,就具有了140万听众,并在以每时辰扩张1万客官的便捷在集合人气,最新测算其基友数量以五18位/秒速度暴增。只要网上朋友关怀了“小黄鸡”,就足以拓宽“人鸡对话”。

操作表明

“小黄鸡”是大伙儿网络的智能聊天机器人。你能够通过关切小黄鸡之后,在意况里@小黄鸡恐怕苏醒它,就能够接受来自小黄鸡很人性化地回复,不过“小黄鸡”主页上的过来主要依旧人造复苏。神一般的东山复起引发广泛网上基友调戏小黄鸡的热潮。

那么你也想一试“小黄鸡”的意思问答吗?一同来尝试吧,保您爆笑。

奥门威尼斯网址 1小黄鸡网页版地址:

从最伊始隔初阶提式无线电话机的优伤欲绝、惶惶不得安心,到本人在你日前瞧着你的冷静泪流不独有却照旧无可挽救什么,到本身三个单身在素不相识的车站静静的谋算、冷静。

 

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

接口调用不是很麻烦,不过是php版本,所以商量了一下C#的效率模块,

本人不是不会以四个明眼人、社会人的角度、口吻与人攀谈,只是在你眼下本人从不想过。如故是单身沉浸在常青恋人之间的单纯爱恋,未有物质、未有社会。

懒得申请SIMSIMI_KEY的话伪造User-Agent和Referer也能得到到json串

Winfrom版

Winfrom版

才精晓成长是逃可是去的,无非自己成长和被迫成长。成长的代价总是惨烈的,也是好的。早一点被迫,至少依然青春的,还足以再来的,不过正是心态各异。

 

奥门威尼斯网址 2

奥门威尼斯网址 2

奥门威尼斯网址,事实上自个儿也很反感把心情看作全体去投入,却又希望爱情的美好。有人出自内心的关心你、照拂你、保养你,好像自身的人生得到了认可,不断沉溺于这种承认,扬弃了该去努力的任何。

#!/usr/bin/env python

后台分界面代码为

后台分界面代码为

唯独幼稚的人啊,总是要结结实实撞在南墙上,好非常的痛一把,才会分晓。

#-*-coding:utf-8-*-

奥门威尼斯网址 4奥门威尼斯网址 5

奥门威尼斯网址 6奥门威尼斯网址 7

其实笔者很想和您共同,很想带你相差不待见的情形,让您真的融合笔者的社会风气,让自身有时机天天伴你左右。

 

  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  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 }
  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 }

(劝诫一句:除非你们是当真的魂魄伴侣,已经丰富领会对方心意,有可观的联系格局,有显著共同努力的大势,有能支撑的经济基础,不然别异地。男男也好女女也罢男女更甚)

# 从simsimi读数据

View Code

View Code

 

接纳的类代码:应用接口用小黄鸡那边传回到的是三个json情势的内容,所以利用到json的剖析难题,笔者利用的是英特网通用的主意Newtonsoft.Json,能够从网络下载对应的DLL

运用的类代码:应用接口用小黄鸡这边传回到的是多少个json格局的内容,所以接纳到json的深入分析难题,小编利用的是英特网通用的办法Newtonsoft.Json,能够从英特网下载对应的DLL

import requests

奥门威尼斯网址 8奥门威尼斯网址 9

奥门威尼斯网址 10奥门威尼斯网址 11

import cookielib

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Net;  6 using System.IO;  7 using System.IO.Compression;  8 using System.Reflection;  9 using Newtonsoft.Json; 10 using Newtonsoft.Json.Linq; 11   12  13 namespace 小贱鸡 14 { 15     class simsimi 16     { 17         /// <summary> 18         /// Cookie 19         /// </summary> 20         /// <returns></returns> 21         public static string GetCookie() 22         { 23             string Cookiesstr = null; 24             CookieCollection cookies = new CookieCollection(); 25             HttpWebRequest request = null; 26             request = (HttpWebRequest)WebRequest.Create("http://www.simsimi.com/talk.htm"); 27             request.CookieContainer = new CookieContainer(); 28             request.CookieContainer.Add; 29             //Get the response from the server and save the cookies from the request.. 30             HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 31             Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri); 32  33             return Cookiesstr; 34         } 35   36  37         public static string Hi_Simsimi(string que, string cookies) 38         { 39             string ans = "我们换个话题吧"; 40             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); 41             HttpWebRequest hi_request = null; 42             try 43             { 44                 hi_request = (HttpWebRequest)WebRequest.Create; 45                 hi_request.Method = "GET"; 46                 hi_request.KeepAlive = true; 47                 hi_request.ServicePoint.Expect100Continue = false; 48  49                 hi_request.AllowWriteStreamBuffering = false; 50                 //终端信息 51                 hi_request.Accept = "application/json,text/javascript,*/*;q=0.01"; 52                 hi_request.Headers.Add("Accept-Language", "zh-cn"); 53                 hi_request.Headers.Add("Accept-Encoding", "gzip,deflate"); 54                 hi_request.Headers.Add("Cookie", cookies + ";simsimi_uid=1;"); 55                 hi_request.Referer = "http://www.simsimi.com/talk.htm"; 56                 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)"; 57                 hi_request.ContentType = "application/json;charset=utf-8"; 58  59                 hi_request.AllowAutoRedirect = false; 60                 HttpWebResponse hi_response = (HttpWebResponse)hi_request.GetResponse(); 61                 StreamReader sr = new StreamReader(hi_response.GetResponseStream(), Encoding.UTF8); 62                 ans = sr.ReadToEnd(); 63                 if (ans != "{}") 64                 { 65  66                     JObject jo = JObject.Parse; 67                     string[] ArrayList = jo.Properties().Select(item => item.Value.ToString.ToArray(); 68                     if (ArrayList[0] == "200") 69                     { 70                         ans = ArrayList[6]; 71                     } 72                     else 73                     { 74                         ans = "没有听懂,可以再说一遍吗?"; 75                     } 76                 } 77                 hi_request.Abort(); 78                 hi_response.Close(); 79             } 80             catch (System.Exception e) 81             { 82  83                 Console.WriteLine(e.ToString; 84                 //return e.ToString(); 85                 return "不好意思死鸡了⊙︿⊙重启下程序吧~"; 86             } 87  88             return ans; 89         } 90  91        92   93         public static bool SetAllowUnsafeHeaderParsing20() 94         { 95             //Get the assembly that contains the internal class 96             Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); 97             if (aNetAssembly != null) 98             { 99                 //Use the assembly in order to get the internal type for the internal class100                 Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");101                 if (aSettingsType != null)102                 {103                     //Use the internal static property to get an instance of the internal settings class.104                     //If the static instance isn't created allready the property will create it for us.105                     object anInstance = aSettingsType.InvokeMember("Section",106                       BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });107 108                     if (anInstance != null)109                     {110                         //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not111                         FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);112                         if (aUseUnsafeHeaderParsing != null)113                         {114                             aUseUnsafeHeaderParsing.SetValue(anInstance, true);115                             return true;116                         }117                     }118                 }119             }120             return false;121         }122     }123 }
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Net;
  6 using System.IO;
  7 using System.IO.Compression;
  8 using System.Reflection;
  9 using Newtonsoft.Json;
 10 using Newtonsoft.Json.Linq;
 11  
 12 
 13 namespace 小贱鸡
 14 {
 15     class simsimi
 16     {
 17         /// <summary>
 18         /// Cookie
 19         /// </summary>
 20         /// <returns></returns>
 21         public static string GetCookie()
 22         {
 23             string Cookiesstr = null;
 24             CookieCollection cookies = new CookieCollection();
 25             HttpWebRequest request = null;
 26             request = (HttpWebRequest)WebRequest.Create("http://www.simsimi.com/talk.htm");
 27             request.CookieContainer = new CookieContainer();
 28             request.CookieContainer.Add(cookies);
 29             //Get the response from the server and save the cookies from the request..
 30             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 31             Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
 32 
 33             return Cookiesstr;
 34         }
 35  
 36 
 37         public static string Hi_Simsimi(string que, string cookies)
 38         {
 39             string ans = "我们换个话题吧";
 40             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);
 41             HttpWebRequest hi_request = null;
 42             try
 43             {
 44                 hi_request = (HttpWebRequest)WebRequest.Create(url);
 45                 hi_request.Method = "GET";
 46                 hi_request.KeepAlive = true;
 47                 hi_request.ServicePoint.Expect100Continue = false;
 48 
 49                 hi_request.AllowWriteStreamBuffering = false;
 50                 //终端信息
 51                 hi_request.Accept = "application/json,text/javascript,*/*;q=0.01";
 52                 hi_request.Headers.Add("Accept-Language", "zh-cn");
 53                 hi_request.Headers.Add("Accept-Encoding", "gzip,deflate");
 54                 hi_request.Headers.Add("Cookie", cookies + ";simsimi_uid=1;");
 55                 hi_request.Referer = "http://www.simsimi.com/talk.htm";
 56                 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)";
 57                 hi_request.ContentType = "application/json;charset=utf-8";
 58 
 59                 hi_request.AllowAutoRedirect = false;
 60                 HttpWebResponse hi_response = (HttpWebResponse)hi_request.GetResponse();
 61                 StreamReader sr = new StreamReader(hi_response.GetResponseStream(), Encoding.UTF8);
 62                 ans = sr.ReadToEnd();
 63                 if (ans != "{}")
 64                 {
 65 
 66                     JObject jo = JObject.Parse(ans);
 67                     string[] ArrayList = jo.Properties().Select(item => item.Value.ToString()).ToArray();
 68                     if (ArrayList[0] == "200")
 69                     {
 70                         ans = ArrayList[6];
 71                     }
 72                     else
 73                     {
 74                         ans = "没有听懂,可以再说一遍吗?";
 75                     }
 76                 }
 77                 hi_request.Abort();
 78                 hi_response.Close();
 79             }
 80             catch (System.Exception e)
 81             {
 82 
 83                 Console.WriteLine(e.ToString());
 84                 //return e.ToString();
 85                 return "不好意思死鸡了⊙︿⊙重启下程序吧~";
 86             }
 87 
 88             return ans;
 89         }
 90 
 91       
 92  
 93         public static bool SetAllowUnsafeHeaderParsing20()
 94         {
 95             //Get the assembly that contains the internal class
 96             Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
 97             if (aNetAssembly != null)
 98             {
 99                 //Use the assembly in order to get the internal type for the internal class
100                 Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
101                 if (aSettingsType != null)
102                 {
103                     //Use the internal static property to get an instance of the internal settings class.
104                     //If the static instance isn't created allready the property will create it for us.
105                     object anInstance = aSettingsType.InvokeMember("Section",
106                       BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
107 
108                     if (anInstance != null)
109                     {
110                         //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
111                         FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
112                         if (aUseUnsafeHeaderParsing != null)
113                         {
114                             aUseUnsafeHeaderParsing.SetValue(anInstance, true);
115                             return true;
116                         }
117                     }
118                 }
119             }
120             return false;
121         }
122     }
123 }

import socket

View Code

View Code

import random

 

import sys

sys.path.append(‘..’)

reload(sys)

sys.setdefaultencoding(‘utf-8’)

 

try:

    from settings import SIMSIMI_KEY

except:

    SIMSIMI_KEY = ”

 

class SimSimi:

 

    def __init__(self):

 

        self.session = requests.Session()

 

        self.chat_url = ”

        self.api_url =

 

        if not SIMSIMI_KEY:

            self.initSimSimiCookie()

 

    def initSimSimiCookie(self):

        self.session.headers.update({‘User-Agent’: ‘Mozilla/5.0 (X11;
Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0’})

        self.session.get(”)

        self.session.headers.update({‘Referer’:
‘)

        self.session.get(”)

        self.session.headers.update({‘Referer’:
‘)

 

    def getSimSimiResult(self, message, method=’normal’):

        if method == ‘normal’:

            r = self.session.get(self.chat_url % message)

        else:

            url = self.api_url % (SIMSIMI_KEY, message)

            r = requests.get(url)

        return r

 

    def chat(self, message=”):

        if message:

            r = self.getSimSimiResult(message, ‘normal’ if not
SIMSIMI_KEY else ‘api’)

            try:

                answer = r.json()[‘response’]

                return answer.encode(‘utf-8’)

            except:

                return random.choice([‘呵呵’, ‘。。。’, ‘= =’,
‘=。=’])

        else:

            return ‘叫作者干嘛’

 

simsimi = SimSimi()

 

def handle(data, bot):

    return simsimi.chat(data[‘message’])

 

if __name__ == ‘__main__’:

    print handle({‘message’: ‘最终二个题材’}, None)

    print handle({‘message’: ‘还大概有一个主题素材’}, None)

    print handle({‘message’: ‘其实作者有多个难点’}, None)

奥门威尼斯网址 12

 

 

 

 

 参考:

python模拟simsimi聊天室来调用simsimi的api:

 

github xiaohuangji:

懒得申请SIMSIMI_KEY的话伪造User-Agent和Referer也能获取到json串
#!/usr/bi…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图