10
我一直在試圖在C#中創建一個「Skype Bot」。到目前爲止,我已經成功地將它用於一對一的聊天工作。我無法在小組聊天中工作。下面是我的源:如何使用Skype4COM在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函數’需要兩個參數」。
然而,當我使用:
skype.SendMessage(msg.Sender.Handle, nick + " Says: " +
ProcessCommand(command));
它完美,但不是在羣聊。你有什麼建議嗎?
很好用,非常感謝。 – user1356292 2012-04-25 23:32:32
@ user1356292,因爲Vagaus發佈回答你的問題,它也適用於你。你應該已經接受它作爲答案,或者至少提高了它的答案。沒有冒犯,只是道德上的責任。 – Saim 2012-05-03 11:10:55