在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 机器人聊天功能(基于图灵api)

机器人聊天功能(基于图灵api)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.38M
  • 下载次数:17
  • 浏览次数:269
  • 发布时间:2020-05-31
  • 实例类别:C#语言基础
  • 发 布 人:shangguan1209
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 机器人 机器 聊天

实例介绍

【实例简介】

图灵机器人Demo源码

源码描述:
一、源码特点
      图灵机器人Demo开发,依据图灵API开发http://www.tuling123.com/openapi/cloud/home.jsp,此源码需要注册个人key
二、菜单功能
      1、机器人只能聊天功能功能
      2、运行后,在输入框中输入想要说的话,譬如“你好”,点击发送即可
      3、收到用户发送的消息后,机器人会只能的给予回复

【实例截图】

from clipboard

【核心代码】

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.Net;
using System.IO;
using System.Web.Script.Serialization;
namespace TuLingRobot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static string APIKEY = "d91b25b8866fef13f82cd28c0d523c8a";
        private static string INFO = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes("你好"));
        private static string getURl = "http://www.tuling123.com/openapi/api?key=" APIKEY "&info=" INFO;
        private string code = "";
        private string text = "";
        private string url = "";
        private string list = "";

        public string article = "";
        public string source = "";
        public string detailurl = "";
        public string trainnum = "";
        public string start = "";
        public string terminal = "";
        public string flight = "";
        public string route = "";
        public string starttime = "";
        public string endtime = "";
        public string icon = "";
        public string name = "";
        public string info = "";
        
        private ToJoson DoAPI(string Msg)
        {
            INFO = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Msg));
            getURl = "http://www.tuling123.com/openapi/api?key=" APIKEY "&info=" INFO;
            Uri uri = new Uri(getURl);
            HttpWebRequest getUrl = WebRequest.Create(uri) as HttpWebRequest;
            getUrl.Method = "GET";
            HttpWebResponse response = getUrl.GetResponse() as HttpWebResponse;
            Stream respStream = response.GetResponseStream();
            StreamReader stream = new StreamReader(respStream,Encoding.UTF8);
            string respStr = stream.ReadToEnd();
            stream.Close();
            JavaScriptSerializer js = new JavaScriptSerializer();
            ToJoson joson = js.Deserialize<ToJoson>(respStr);
            code = joson.code;
            text = joson.text;
            url = joson.url;
            List<list> list = joson.list;
            return joson;
        }
        private void Clear()
        {
            txtSendMsg.Text = "";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            StringBuilder receiveMsg = new StringBuilder();
            string msg = txtSendMsg.Text.Trim();
            txtRecMsg.AppendText("我:" DateTime.Now Environment.NewLine);
            txtRecMsg.AppendText(msg "\r\n");
            ToJoson joson = DoAPI(msg);
            List<list> list = joson.list;
            switch (joson.code)
            {
                //文本类
                case "100000":
                    receiveMsg.Append(joson.text Environment.NewLine);
                    break;
                //列车
                case "305000":
                    receiveMsg.Append(joson.text Environment.NewLine)
                              .Append("起始站【" list[0].start "】,到达站【" list[0].terminal "】" Environment.NewLine);
                    receiveMsg.Append("车次\t\t\t\t\t开车时间\t\t到达时间" Environment.NewLine);
                    foreach (list listDetail in list)
                    {
                        receiveMsg.Append(listDetail.trainnum.PadRight(30, ' ') "\t" listDetail.starttime "\t\t\t" listDetail.endtime Environment.NewLine);
                    }
                    receiveMsg.Append("详情地址:" list[0].detailurl Environment.NewLine);
                    break;
                //航班
                case "306000":
                     receiveMsg.Append(joson.text Environment.NewLine) ;
                     receiveMsg.Append("航班\t\t\t\t\t起飞时间\t到达时间" Environment.NewLine);
                    foreach (list listDetail in list)
                    {
                        receiveMsg.Append(listDetail.flight.PadRight(50, ' ') listDetail.starttime "\t\t" listDetail.endtime Environment.NewLine);
                    }
                    break;
                //网址类数据 
                case "200000":
                    receiveMsg.Append(joson.text Environment.NewLine)
                               .Append(joson.url);
                    break;
                //新闻 
                case "302000":
                    receiveMsg.Append(joson.text Environment.NewLine)
                              .Append(joson.url);
                    foreach (list listDetail in list)
                    {
                        receiveMsg.Append(listDetail.source "   " listDetail.article Environment.NewLine)
                            .Append(listDetail.detailurl Environment.NewLine);
                    }
                    break;
                //菜谱、视频、小说 
                case "308000":
                     receiveMsg.Append(joson.text Environment.NewLine)
                              .Append(joson.url);
                    foreach (list listDetail in list)
                    {
                        receiveMsg.Append(listDetail.name "   " listDetail.info Environment.NewLine)
                            .Append(listDetail.detailurl Environment.NewLine);
                    }
                    break;
                //key的长度错误(32位)
                case "40001":
                    receiveMsg.Append("key的长度错误(32位)" Environment.NewLine);
                    break;
                //请求内容为空
                case "40002":
                    receiveMsg.Append("请求内容为空" Environment.NewLine);
                    break;
                //Key错误或帐号未激活
                case "40003":
                    receiveMsg.Append("Key错误或帐号未激活" Environment.NewLine);
                    break;
                //当天请求次数已用完
                case "40004":
                    receiveMsg.Append("当天请求次数已用完" Environment.NewLine);
                    break;
                //暂不支持该功能
                case "40005":
                    receiveMsg.Append("暂不支持该功能" Environment.NewLine);
                    break;
                //服务器升级中
                case "40006":
                    receiveMsg.Append("服务器升级中" Environment.NewLine);
                    break;
                //服务器数据格式异常
                case "40007":
                    receiveMsg.Append("服务器数据格式异常" Environment.NewLine);
                    break;
                default:
                    break;
            }
            //txtRecMsg.Select(0, 3);
            //txtRecMsg.SelectionFont = new Font("宋体", 13, FontStyle.Bold); ;
            //txtRecMsg.SelectionColor = Color.Green;
            txtRecMsg.AppendText("机器人:" DateTime.Now Environment.NewLine);
            txtRecMsg.AppendText(receiveMsg.ToString()  "\r\n");
            txtRecMsg.Select(txtRecMsg.TextLength, 0);
            Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //StringBuilder receiveMsg = new StringBuilder();
            //receiveMsg.Append("航班\t\t\t\t\t起飞时间\t到达时间" Environment.NewLine);
            //receiveMsg.Append("CZ8102 南方航空 CA3964 中国国航 共享".PadRight(50, ' ') "07:15" "\t\t" "08:27" Environment.NewLine);
            //receiveMsg.Append("CA3916 中国国航 共享".PadRight(50, ' ') "07:15" "\t\t" "08:27" Environment.NewLine);
           
            //txtRecMsg.AppendText(receiveMsg.ToString());

            //txtRecMsg.AppendText("我:" "1211");
            //txtRecMsg.Select(0, 1);
            //txtRecMsg.SelectionFont =  new Font("宋体", 13 , FontStyle.Bold);;
            //txtRecMsg.SelectionColor = Color.Green;
        }

        private void txtSendMsg_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                btnSend_Click(sender, e);
            }
        }

        private void txtRecMsg_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }
    }
}



标签: 机器人 机器 聊天

实例下载地址

机器人聊天功能(基于图灵api)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警