如何使用Python中的Skype4Py向群组对话发送消息

时间:2013-06-18 18:15:22

标签: python python-2.7 sendmessage skype4py

我一直试图让我的脚本使用Skype4Py库向Skype中的群组对话发送消息,我目前能够发送消息的唯一方法是特定用户。

import Skype4Py
Skype = Skype4Py.Skype()
Skype.Attach()
Skype.SendMessage('namehere','testmessage')

有谁知道如何更改我的代码以向群组对话发送消息?

1 个答案:

答案 0 :(得分:8)

以下小脚本应该有效。 (假设你已经打开了群聊)

def sendGroupChatMessage():
    """
    Send Group Chat Messages.
    """
    import Skype4Py as skype
    skypeClient = skype.Skype()
    skypeClient.Attach()
    for elem in skypeClient.ActiveChats:
        if len(elem.Members) > 2:
            elem.SendMessage("SomeMessageHere")

我基本上导入所有当前聊天,检查成员数量并相应地发送消息。在各个小组中也应该很容易检查。

要获得句柄,请将您的功能更改为此。

def sendGroupChatMessage():
    """
    Send Group Chat Messages.
    """
    import Skype4Py as skype
    skypeClient = skype.Skype()
    skypeClient.Attach()
    for elem in skypeClient.ActiveChats:
        if len(elem.Members) > 2:
            for friend in elem.Members:
                  print friend.Handle
            elem.SendMessage("SomeMessageHere")

如果你可以为聊天添加书签,那么你就必须这样做。

>>> groupTopic = 'Insert a Topic Here'
>>> for chat in skypeClient.BookmarkedChats:
        if chat.Topic == groupTopic:
            chat.SendMessage("Send a Message Here")

这是最终代码,应该是独立的。

def sendGroupChatMessage(topic=""):
    """
    Send Group Chat Messages.
    """
    import Skype4Py as skype
    skypeClient = skype.Skype()
    skypeClient.Attach()
    messageSent = False
    for elem in skypeClient.ActiveChats:
        if len(elem._GetMembers()) > 2 and elem.Topic == topic:
            elem.SendMessage("SomeMessageHere")
            messageSent = True

    if not messageSent:
        for chat in skypeClient.BookmarkedChats:
            if chat.Topic == topic:
                chat.SendMessage("SomeMessageHere")
                messageSent = True

    return messageSent