我一直在尝试使用C#制作一个“Skype机器人”。到目前为止,我已经成功地让它在一对一的聊天中运行。但是我无法让它在群聊中运行。这是我的源代码:
using System;
using System.Windows.Forms;
using SKYPE4COMLib;
namespace SkypeBot
{
public partial class Form1 : Form
{
private Skype skype;
private const string trigger = "!"; // Say !help
private const string nick = "Bot";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
skype = new Skype();
// Use skype protocol version 7
skype.Attach(7, false);
// Listen
skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
}
private void skype_MessageStatus(ChatMessage msg, TChatMessageStatus status)
{
if (TChatMessageStatus.cmsRead == status)
{
return;
}
if (msg.Body.IndexOf(trigger) == 0 && TChatMessageStatus.cmsReceived == status)
{
// Remove trigger string and make lower case
string command = msg.Body.Remove(0, trigger.Length).ToLower();
// Send processed message back to skype chat window
skype.SendMessage(msg.Sender.Handle, nick + " Says: " + ProcessCommand(command));
IChat ichat = skype.get_Chat(msg.Chat.Name);
ichat.SendMessage(msg.Sender.Handle, nick + " Says: " + ProcessCommand(command));
}
}
private string ProcessCommand(string str)
{
string result;
switch (str)
{
case "help":
result = "Sorry no help available";
break;
case "date":
result = "Current Date is: " + DateTime.Now.ToLongDateString();
break;
case "time":
result = "Current Time is: " + DateTime.Now.ToLongTimeString();
break;
case "who":
result = "I am Bot, a magical Skype robot!";
break;
case "moon":
result = "(moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) (moon) ";
break;
default:
result = "Sorry, I do not recognize your command";
break;
}
return result;
}
}
}
当我尝试使用“SendMessage”方法发送处理过的消息时,出现了“没有重载方法'SendMessage'接受2个参数”的错误。
但是,当我使用以下方式时:
skype.SendMessage(msg.Sender.Handle, nick + " Says: " +
ProcessCommand(command));
它完美地运作,但在群聊中却不行。你有什么建议吗?