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.
I want to find out what conditions produce remarkable software. A few years ago, I chose to work as the first professional tester at a startup. I successfully won credibility for testers and established a world-class team. I have lead the testing for early versions of multiple products. Today, I run Qxf2 Services. Qxf2 provides software testing services for startups. If you are interested in what Qxf2 offers or simply want to talk about testing, you can contact me at: [email protected]. I like testing, math, chess and dogs.
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]
Hi Khaibar,
Please check that, you are passing the correct Username and Password to connect skype. Also please ensure that Username has access to that channel. The below link may also help you to verify the ID manually once you retrieve it with the code snippet. Using manual verification you can also ensure that the ID you have received through your code.
https://stackoverflow.com/questions/45862364/how-to-get-skype-group-chat-id
Regards,
Rahul
how can i mention user in skype ?
channel.sendMsg(‘@all’) not mention the users.
Hey,
To mention user on skype you can try –\\\”>
Can i get a symbol full code for mention?
I have already try with channel.sendMsg(“””all”””) that code. But it dont send as “@all”. It send as “all”.
Now I got it . Thank you.
Why do not work Skype(username, password) on heroku server? Token error is showed in heroku log.
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?
Ye bahut badiya code hato ye mere bahut fayedemand tha
Thank You very much