public string RntMsg(string Content, string CounterID) { string com = "select * from blue.turing where state = '1' order by robotid"; MySQL msql = new MySQL("blue"); DataTable dt = msql.S(com); if (dt.Rows.Count == 0) { return("好累啊,明天在说吧。"); } else { string appkey = dt.Rows[0]["APIkey"].ToString(); string url = "http://www.tuling123.com/openapi/api"; var parameters = new Dictionary <string, string>(); if (Content.Length > 30) { Content = Content.Substring(0, 29); } Content = Content.ToLower().Replace("BlueYifan", "图灵机器人"); parameters.Add("key", appkey); //你申请的key parameters.Add("info", Content); //要发送给机器人的内容,不要超过30个字符 parameters.Add("userid", CounterID); //1~32位,此userid针对您自己的每一个用户,用于上下文的关联 string str = sendPost(url, parameters, "post"); JObject jo = JObject.Parse(str); string code = jo["code"].ToString(); string rnt = ""; switch (code) { case "100000": { rnt = jo["text"].ToString(); } break; case "200000": { rnt = jo["text"].ToString() + "\n"; rnt = rnt + "<a href=\"" + jo["url"].ToString() + "\">" + "点击查看" + "</a> "; } break; case "302000": { rnt = jo["text"].ToString() + "\n"; JArray ja = JArray.Parse(jo["list"].ToString()); rnt = rnt + "共" + ja.Count.ToString() + "条\n"; for (int i = 0; i < ja.Count; i++) { JObject jo_ = JObject.Parse(ja[i].ToString()); rnt = rnt + "<a href=\"" + jo_["detailurl"].ToString() + "\">" + (i + 1).ToString() + "、" + jo_["article"].ToString() + "</a> \n"; } } break; case "308000": { rnt = jo["text"].ToString() + "\n"; JArray ja = JArray.Parse(jo["list"].ToString()); rnt = rnt + "共" + ja.Count.ToString() + "条\n"; for (int i = 0; i < ja.Count; i++) { JObject jo_ = JObject.Parse(ja[i].ToString()); rnt = rnt + "<a href=\"" + jo_["detailurl"].ToString() + "\">" + (i + 1).ToString() + "、" + jo_["name"].ToString() + "</a> \n"; } } break; case "40004": { rnt = "抱歉,未能收到您发的信息,可能是因为丢包或其它原因。能再发一遍吗?"; msql.I_D_U("UPDATE blue.turing SET state = '0' where APIkey = '" + appkey + "'"); } break; case "40001": { rnt = "抱歉,我出故障了,错误代码 1 ,可以把错误代码转告给Wan Yifan吗?"; } break; case "40007": { rnt = "抱歉,我出故障了,错误代码 7 ,可以把错误代码转告给Wan Yifan吗?"; } break; case "40002": { rnt = "抱歉,我出故障了,错误代码 2 ,可以把错误代码转告给Wan Yifan吗?"; } break; } rnt = rnt.Replace("图灵机器人", "BlueYifan").Replace("亲爱的", "尊敬的用户").Replace("旧服务已下线,请迁移至 http://api.fanyi.baidu.com", "我不说了"); return(rnt); } }
public string RntMsg(string PicUrl) { WebClient wc = new WebClient(); byte[] bytes = wc.DownloadData(PicUrl); MemoryStream ms = new MemoryStream(bytes); Image img = Image.FromStream(ms); MC = Statistics.PrincipalColorAnalysis((Bitmap)img, 3, 24); MySQL msql = new MySQL("blue"); DataTable dtColor = new DataTable(); dtColor = msql.S("SELECT * FROM blue.color;"); DataTable dtRnt = new DataTable(); dtRnt.Columns.Add("name"); dtRnt.Columns.Add("amount"); for (int i = 0; i < MC.Count; i++) { string cn_name = ""; string en_name = ""; string name = ""; long sim = long.MaxValue; int X1 = MC[i].Color & 255; int X2 = (MC[i].Color & 65280) / 256; int X3 = (MC[i].Color & 16711680) / 65536; foreach (DataRow drColor in dtColor.Rows) { int Y1 = Convert.ToInt32(drColor["R"].ToString()); int Y2 = Convert.ToInt32(drColor["G"].ToString()); int Y3 = Convert.ToInt32(drColor["B"].ToString()); long result = (X1 - Y1) * (X1 - Y1) + (X2 - Y2) * (X2 - Y2) + (X3 - Y3) * (X3 - Y3); //double X = (X1 * Y1 + X2 * Y2 + X3 * Y3); //double Y = Math.Sqrt(X1 * X1 + X2 * X2 + X3 * X3) * Math.Sqrt(Y1 * Y1 + Y2 * Y2 + Y3 * Y3); //double result = X / Y; if (result <= sim) { cn_name = drColor["CN_NAME"].ToString(); en_name = drColor["EN_NAME"].ToString(); name = cn_name + "(" + en_name + ")"; sim = result; } } DataRow drNew = dtRnt.NewRow(); drNew["name"] = name; drNew["amount"] = MC[i].Amount.ToString(); dtRnt.Rows.Add(drNew); } Dictionary <string, DataRow> dict = new Dictionary <string, DataRow>(); //这个字典用于查找第一列相同的项目 List <DataRow> removeRows = new List <DataRow>(); //这个List 存储需要删除的重复行 foreach (DataRow row in dtRnt.Rows) { string key = row["name"].ToString(); DataRow dr_; if (dict.TryGetValue(key, out dr_)) { dr_["amount"] = int.Parse(dr_["amount"].ToString()) + int.Parse(row["amount"].ToString()); removeRows.Add(row); //将这一行加入要删除的行列表中 } else { dict.Add(key, row); } } //删除重复的行 foreach (DataRow row in removeRows) { dtRnt.Rows.Remove(row); } string rnt = ""; foreach (DataRow dr in dtRnt.Rows) { rnt = rnt + "颜色名称:" + dr["name"].ToString() + "\n" + "像素个数:" + dr["amount"].ToString() + "\n\n"; } return(rnt); }