很早就知道小I机器人做的不错,但是一直没有时间去研究,趁着元旦放假,下来.Net的SDK好好研究了一下,Demo程序是一个控制台程序,用户交互还不是很方便,此外代码中竟然有重复的地方,看来小I机器人的主人们不是那么认真,唉,真苦了小I这孩子:)
我重新做一个基于Windows窗体的程序,添加了输入窗口,这样机器人不会回答的时候,主人也可以代答。此外代码也重新进行了整理,所有信息也都修改为中文,方便了用户调试和开发。此外发现该程序一旦启动,便无法正常关闭,不知道为什么?这也许是Demo示例开发者执意为控制台程序的原因之一吧(瞎猜的,初次接触该SDK程序,我也有很多不明白的地方)。
程序运行后,还是让人眼前一亮的,下面是程序运行截图。
相关代码如下,需要的朋友可以参考一下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Runtime.Serialization;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using Incesoft.BotPlatform.SDK;
using Incesoft.BotPlatform.SDK.Interface;
namespace YFMGC
{
public partial class frmMain : Form
{
string address; //地址
int port; //端口
string user; //用户
string password; //密码
IRobotServer server;
MyHandler m_MyHandler;
MyListener m_MyListener;
public IRobotSession session = null;
public frmMain()
{
InitializeComponent();
}
//初始化
private void frmMain_Load(object sender, EventArgs e)
{
address = "msnbot.incesoft.com";
port = 6602;
user = "SP042761";
password = "yefan7722";
server = RobotServerFactory.Instance.createRobotServer(address, port);
m_MyHandler = new MyHandler(server, this);
m_MyListener = new MyListener(this);
server.addRobotHandler(m_MyHandler);
server.addConnectionListener(m_MyListener);
lblEx_LinkClicked(null, null);
txtInfo.Enabled = false;
txtSend.Enabled = false;
}
//关闭
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
//无法成功关闭
//server.removeConnectionistener(m_MyListener);
//server.removeRobotHandler(m_MyHandler);
//m_MyListener.serverDisconnected(server);
//server = null;
//m_MyListener = null;
//m_MyHandler = null;
//Application.Exit();
}
//登录/退出
private void btnLogin_Click(object sender, EventArgs e)
{
if (btnLogin.Text == "登录")
{
try
{
btnLogin.Enabled = false;
server.login(user, password, 30000);
btnLogin.Text = "退出";
btnLogin.Enabled = true;
txtInfo.Enabled = true;
txtSend.Enabled = true;
}
catch (RobotException ex)
{
btnLogin.Enabled = true;
ShowInfo("登录错误:" + ex.Message);
}
}
else
{
try
{
btnLogin.Enabled = false;
server.logout();
btnLogin.Text = "登录";
btnLogin.Enabled = true;
txtInfo.Enabled = false;
txtSend.Enabled = false;
}
catch (RobotException ex)
{
btnLogin.Enabled = true;
ShowInfo("退出错误:" + ex.Message);
}
}
}
//显示信息
public delegate void lbInfo_ShowInfo(string strInfo);
public void ShowInfo(string strInfo)
{
if (strInfo.Length == 0) return;
if (lbInfo.InvokeRequired)
{
lbInfo_ShowInfo run = new lbInfo_ShowInfo(ShowInfo);
lbInfo.Invoke(run, new object[] { strInfo });
}
else
{
lbInfo.Items.Insert(0, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + strInfo);
lbInfo.TopIndex = 0;
lbInfo.SelectedIndex = 0;
}
}
//显示接收的数据
public delegate void txtInfo_ShowInfo(string strInfo);
public void TxtShowInfo(string strInfo)
{
if (strInfo.Length == 0) return;
if (txtInfo.InvokeRequired)
{
txtInfo_ShowInfo run = new txtInfo_ShowInfo(TxtShowInfo);
txtInfo.Invoke(run, new object[] { strInfo });
}
else
{
txtInfo.Text +="[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + strInfo+"/r/n";
}
}
private void frmMain_SizeChanged(object sender, EventArgs e)
{
scBar.Height = this.Height - 159 + (lbInfo.Visible? 0:lbInfo.Height);
}
//显隐扩展面板
private void lblEx_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (lbInfo.Visible)
{
lblEx.Text = ">>>";
lbInfo.Visible = false;
this.Height = this.Height - lbInfo.Height;
}
else
{
lblEx.Text = "<<<";
lbInfo.Visible = true;
this.Height = this.Height + lbInfo.Height;
}
}
//发送
private void btnSend_Click(object sender, EventArgs e)
{
IRobotMessage msg = session.createMessage();
msg.setSignature("[叶帆插话]");
msg.setString(txtSend.Text);
session.send(msg);
txtSend.Text = "";
}
}
class MyHandler : IRobotHandler
{
frmMain fm;
public MyHandler(IRobotServer server,frmMain fm)
{
this.server = server;
this.fm = fm;
}
private IRobotServer server;
private Random rdm = new Random();
#region命令菜单
public System.String commandList = "*****************************************/r" +
"** BOTPLATFORM SDK DEMO COMMAND LIST **/r" +
"*****************************************/r" +
" preface --- test message preface. /r" +
" emo ------- test emoticon./r" +
" nudge ----- test nudge./r" +
" p4 -------- test msn activity./r" +
" typing ---- test typing info./r" +
" name ------ test change friendly name./r" +
" pm -------- test change personal msg./r" +
" dp -------- test change display picture./r" +
" bye --------- test close session./r" +
" help ------ print this command list./r" +
" ? --------- print this command list./r" +
" ent --------- print enterprise only command list.";
public System.String commandListEnt = "*****************************************/r" +
"** BOTPLATFORM SDK ENTERPRISE ONLY COMMAND LIST **/r" +
"** Only use the following commands after upgraded your sp account **/r" +
"*****************************************/r" +
" file ------ test file transfer./r" +
" bg -------- test background sharing./r" +
" ink ------- test send ink./r" +
" wink ------ test send wink./r" +
" voice ----- test send voiceclip./r" +
" webcam ---- test send webcam/r" +
" cs <account> -------- test create session./r" +
" pu <account> ------ test push offline message./r" +
" iv <account> -------- test invite user./r" +
" ent --------- print this command list.";
#endregion
public virtual void sessionOpened(IRobotSession session)
{
fm.session = session;
fm.ShowInfo("会话开始...");
try
{
switch (session.OpenMode)
{
case SessionOpenMode.OPEN_MODE_CONV_OPEN:
session.send(commandList);
break;
case SessionOpenMode.OPEN_MODE_ROBOT:
session.send("You have an alert message: blablabla");
break;
default:
break;
}
}
catch (RobotException e)
{
util.consoleOut(e.ToString());
}
}
public virtual void sessionClosed(IRobotSession session)
{
fm.ShowInfo("会话结束");
}
//消息处理
public virtual void messageReceived(IRobotSession session, IRobotMessage message)
{
fm.session = session;
fm.TxtShowInfo(message.String);
try
{
string command = message.String;
IRobotMessage msg = session.createMessage();
string command_ex = "";
if (command.Length > 2)
{
command_ex = command.Substring(0, 2);
if (command_ex == "cs" || command_ex == "pu" || command_ex == "iv")
{
command = command_ex;
command_ex = command.Substring(2).Trim();
}
}
switch (command.ToLower())
{
case "help": //帮助
case "?":
session.send(commandList);
break;
case "preface": //设置用户
msg.setSignature("叶叶 - " + rdm.Next());
msg.setString("我来说一下");
session.send(msg);
break;
case "nudge": //闪屏
session.sendNudge();
break;
case "p4": //扩展界面
session.sendActivity("http://blog.csdn.net/yefanqiu", "叶帆工作室");
break;
case "typing": //显示正在输入信息状态
session.sendTyping();
break;
case "name": //显示名
server.DisplayName = "YF.MGC-" + rdm.Next();
break;
case "pm": //悄悄话
server.PersonalMessage = "叶帆-" + rdm.Next();
break;
case "dp": //显示图片
server.DisplayPicture = "yf01"; //__default.dat";
break;
case "emo": //发送含图片的信息
msg.registerEmoticon("(1)", "yf001.png");
msg.registerEmoticon("(2)", "yf002.png");
msg.registerEmoticon("(3)", "yf003.png");
msg.setString("a(1)b(2)c(3)d");
session.send(msg);
break;
case "ent":
session.send(commandListEnt);
break;
case "cs": //创建一个对话
server.createSession(session.Robot, command_ex);
break;
case "pu": //弹出消息
server.pushMessage(session.Robot, command_ex, "你的不在线消息:你在哪里...");
break;
case "iv": //邀请用户
session.inviteUser(command_ex);
break;
case "bye": //退出会话
session.close();
break;
default:
session.send("俺太小,不懂你的意思!请输入'?',看看我明白什么?");
break;
}
//System.String ret = "font name: " + message.FontName + "/r";
//ret = ret + "font style: " + message.FontStyle + "/r";
//ret = ret + "font color: " + message.FontColor.ToString() + "/r";
//ret = ret + "message content: " + "/r";
//ret = ret + message.String;
//session.send(ret);
}
catch (RobotException e)
{
util.consoleOut(e.ToString());
}
}
public virtual void nudgeReceived(IRobotSession session)
{
fm.ShowInfo("事件:闪屏已收到");
}
public virtual void activityAccepted(IRobotSession session)
{
fm.ShowInfo("事件:活动面板已打开");
}
public virtual void activityRejected(IRobotSession session)
{
fm.ShowInfo("事件:活动面板被拒绝");
}
public virtual void userAdd(String robot, String user)
{
fm.ShowInfo("事件:用户被添加("+robot+","+user+")");
}
public virtual void userRemove(String robot, String user)
{
fm.ShowInfo("事件:用户被删除(" + robot + "," + user + ")");
}
public virtual void exceptionCaught(IRobotSession session, System.Exception cause)
{
fm.ShowInfo("服务器出错:" + cause.Message);
}
public virtual void activityClosed(IRobotSession session)
{
fm.ShowInfo("事件:活动面板被关闭");
}
public virtual void fileAccepted(IRobotSession session)
{
fm.ShowInfo("事件:发送的文件被接收");
}
public virtual void fileRejected(IRobotSession session)
{
fm.ShowInfo("事件:发送的文件被舍弃");
}
public virtual void fileTransferEnded(IRobotSession session)
{
fm.ShowInfo("事件:文件传送完毕");
}
public virtual void backgroundAccepted(IRobotSession session)
{
fm.ShowInfo("事件:会话场景被接收");
}
public virtual void backgroundRejected(IRobotSession session)
{
fm.ShowInfo("事件:会话场景被舍弃");
}
public virtual void backgroundTransferEnded(IRobotSession session)
{
fm.ShowInfo("事件:会话场景设置完毕");
}
public virtual void webcamAccepted(IRobotSession session)
{
fm.ShowInfo("事件:画面被接收");
}
public virtual void webcamRejected(IRobotSession session)
{
fm.ShowInfo("事件:画面被舍弃");
}
public virtual void activityLoaded(IRobotSession session)
{
fm.ShowInfo("事件:活动面板用户已响应");
}
public virtual void activityReceived(IRobotSession session, System.String data)
{
fm.ShowInfo("事件:活动面板用户已输入数据" + data);
}
public virtual void userJoined(IRobotSession session, IRobotUser user)
{
fm.ShowInfo("事件:用户已加入(" + user+")");
}
public virtual void userLeft(IRobotSession session, IRobotUser user)
{
fm.ShowInfo("事件:用户(" + user + ")");
}
public virtual void userUpdated(IRobotUser user)
{
fm.ShowInfo("事件:用户信息已更新(" + user.ID + "," + user.Status + "," + user.FriendlyName + ")");
}
public virtual void personalMessageUpdated(System.String robot, System.String user, System.String personalMessage)
{
fm.ShowInfo("事件:悄悄话(" + robot + ", " + user + ", " + personalMessage + ")");
}
public virtual void contactListReceived(System.String robot, System.Collections.ArrayList contactList)
{
fm.ShowInfo("事件:会话用户列表");
for (int i = 0; i < contactList.Count; i++)
{
IRobotUser user = (IRobotUser)contactList[i];
fm.ShowInfo("User " + i + ": " + user.ID);
}
}
}
class MyListener : IRobotConnectionListener
{
frmMain fm;
public MyListener(frmMain fm)
{
this.fm = fm;
}
public void serverConnected(IRobotServer server)
{
fm.ShowInfo("服务器已连接成功,正在登录,请耐心等待 ...");
}
public void serverReconnected(IRobotServer server)
{
fm.ShowInfo("再次与服务器连接...");
}
public void serverDisconnected(IRobotServer server)
{
fm.ShowInfo("服务器已断开!");
}
public void serverLoggedIn(IRobotServer sever)
{
fm.ShowInfo("用户登录成功!");
}
}
}