Search code examples
pythonpython-2.7sendmessageskype4py

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


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?


Solution

  • 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