如何用Python向与Skype4Py的组对话发送消息

2024-04-16 15:05:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: 方法代码用户import脚本消息对话发送给
1条回答
网友
1楼 · 发布于 2024-04-16 15:05:26

下面的小脚本应该有用。(假设您已经打开了群聊)

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

相关问题 更多 >