Posting messages on a Skype group channel using Python

I recently wrote a script to post messages on Skype using Python. I used the excellent SkPy module. The module’s documentation is very good but examples on how to post messages to group chats was sparse. I had to muddle around, try things out with the Python interpreter and figure things out. In fact, I am not sure if I did it in the most elegant way possible. However, what I did was more than what was available online. So I thought I would document what I did in this post.

The rest of this post has this flow:
1. Setup
2. Establish the connection
3. Identify the channel id
4. Select a group channel to post the message
5. Post a message
6. Putting it all together


1. Setup

I signed up for a Skype account and added it to one of my group channels. I also sent one message on each of the channels after I added the new Skype account.

On the Python side, I simply did pip install SkPy

2. Establish the connection

From here on, use your Python interpreter to execute the commands below. It is easier to get started that way as opposed to writing a script. You can move to a script once you have figured out how to work with SkPy.

To connect with your Skype account:

from skpy import Skype
skype_obj = Skype(username,password) #use your real username and password here

If the connection fails you will see an error in the interpreter. If the connection succeeds, the interpreter prompt returns silently.

3. Identify the channel id

This is the portion where I got stuck initially. I did not know how I could get the id of the channel. For example, when I tried skype_obj.chats, I simply got a cryptic SkypeChats(). Then, after playing around a little bit, I noticed we could do skype_obj.chats.recent() which returned a list of dictionaries that had information on the different group channels. A typical group channel id would look like: '19:[email protected]', where 19 is the number associated with group chats while 8 is a number associated with 1:1 chats.

skype_obj.chats #Returns SkypeChats() Not useful
skype_obj.chats.recent() #aha! This was useful

4. Select a channel

Now that we have a channel id, it is simple to fetch the channel handler.

channel = sk.chats.chat('19:[email protected]')

5. Post a message

Posting a message is well documented and intuitive.

channel.sendMsg('Hello! This is my first automatic message.')

6. Putting it all together

My method looked like this in the end:

def post_message(msg,channel_id): 
  "Post a message" 
  sk = Skype(credentials.USERNAME,credentials.PASSWORD) 
  channel = sk.chats.chat(channel_id) 
  channel.sendMsg(msg)

References

a) Official SkPy project: https://pypi.org/project/SkPy/
b) Source code of SkPy: https://skpy.t.allofti.me/_modules/skpy/main.html#Skype
c) Documentation for SkPy: https://skpy.t.allofti.me/index.html

Bonus

You can see how I use this code and put out a different message every weekday in this repo: https://github.com/qxf2/skype_bots.


11 thoughts on “Posting messages on a Skype group channel using Python

  1. from skpy import Skype
    skype_obj = Skype(“username”, “password”) # connect to Skype
    skype_obj.chats #Returns SkypeChats() Not useful
    skype_obj.chats.recent() #aha! This was useful
    Hey bro,
    I did as u mentioned, but I can’t get the channel [ID]

  2. I have already try with channel.sendMsg(“””all”””) that code. But it dont send as “@all”. It send as “all”.

  3. Why do not work Skype(username, password) on heroku server? Token error is showed in heroku log.

    1. This blog post is about posting messages on Skype using Python. From your comment, we are assuming you are trying to use Heroku for your use case. Can you elaborate more on what you have tried?

Leave a Reply

Your email address will not be published. Required fields are marked *