How can I send a message to a group conversation with Skype4Py in Python

被刻印的时光 ゝ 提交于 2019-12-03 07:56:28

问题


I've been trying to get my script to send a message to a group conversation in Skype using the Skype4Py library, the only way I am currently able to send messages is to specific users.

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

Does anyone know how I can change my code to send a message to a group conversation?


回答1:


The following little script should work. (Assuming you already have a group chat open)

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")

I'm basically importing all the current chats, checking the number of members and sending a message accordingly. It should be easy to check within various groups too.

To get the handles too, change your function to this.

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")

If you can bookmark your chat, you just have to do this, then.

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

This is the final code that should be standalone.

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


来源:https://stackoverflow.com/questions/17176009/how-can-i-send-a-message-to-a-group-conversation-with-skype4py-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!