{"id":11481,"date":"2019-10-23T05:36:00","date_gmt":"2019-10-23T09:36:00","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=11481"},"modified":"2019-10-23T05:36:00","modified_gmt":"2019-10-23T09:36:00","slug":"posting-messages-on-a-skype-group-channel-using-python","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/posting-messages-on-a-skype-group-channel-using-python\/","title":{"rendered":"Posting messages on a Skype group channel using Python"},"content":{"rendered":"<p>I recently wrote a script to post messages on Skype using Python. I used the excellent <a href=\"https:\/\/pypi.org\/project\/SkPy\/\">SkPy module<\/a>. The <a href=\"https:\/\/skpy.t.allofti.me\/\">module&#8217;s documentation<\/a> 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.<\/p>\n<p>The rest of this post has this flow:<br \/>\n1. Setup<br \/>\n2. Establish the connection<br \/>\n3. Identify the channel id<br \/>\n4. Select a group channel to post the message<br \/>\n5. Post a message<br \/>\n6. Putting it all together<\/p>\n<hr>\n<h4>1. Setup<\/h4>\n<p>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. <\/p>\n<p>On the Python side, I simply did <code>pip install SkPy<\/code><\/p>\n<h4>2. Establish the connection<\/h4>\n<p>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.<\/p>\n<p>To connect with your Skype account:<\/p>\n<pre lang=\"python\">\r\nfrom skpy import Skype\r\nskype_obj = Skype(username,password) #use your real username and password here\r\n<\/pre>\n<p>If the connection fails you will see an error in the interpreter. If the connection succeeds, the interpreter prompt returns silently.<\/p>\n<h4>3. Identify the channel id<\/h4>\n<p>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 <code>skype_obj.chats<\/code>, I simply got a cryptic <code> SkypeChats()<\/code>. Then, after playing around a little bit, I noticed we could do <code>skype_obj.chats.recent()<\/code> which returned a list of dictionaries that had information on the different group channels. A typical group channel id would look like: <code>'19:626bd82f2bf249e788091763f2042b87@thread.skype'<\/code>, where <code>19<\/code> is the number associated with group chats while 8 is a number associated with 1:1 chats. <\/p>\n<pre lang=\"python\">\r\nskype_obj.chats #Returns SkypeChats() Not useful\r\nskype_obj.chats.recent() #aha! This was useful\r\n<\/pre>\n<h4>4. Select a channel<\/h4>\n<p>Now that we have a channel id, it is simple to fetch the channel handler.<\/p>\n<pre lang=\"python\">\r\nchannel = sk.chats.chat('19:626bd82f2bf249e788091763f2042b87@thread.skype')\r\n<\/pre>\n<h4>5. Post a message<\/h4>\n<p>Posting a message is well documented and intuitive.<\/p>\n<pre lang=\"python\">\r\nchannel.sendMsg('Hello! This is my first automatic message.')\r\n<\/pre>\n<h4>6. Putting it all together<\/h4>\n<p>My method looked like this in the end:<\/p>\n<pre lang=\"python\">\r\ndef post_message(msg,channel_id): \r\n  \"Post a message\" \r\n  sk = Skype(credentials.USERNAME,credentials.PASSWORD) \r\n  channel = sk.chats.chat(channel_id) \r\n  channel.sendMsg(msg)\r\n<\/pre>\n<h4>References<\/h4>\n<p>a) Official SkPy project: <a href=\"https:\/\/pypi.org\/project\/SkPy\/\">https:\/\/pypi.org\/project\/SkPy\/ <\/a><br \/>\nb) Source code of SkPy: <a href=\"https:\/\/skpy.t.allofti.me\/_modules\/skpy\/main.html#Skype\">https:\/\/skpy.t.allofti.me\/_modules\/skpy\/main.html#Skype<\/a><br \/>\nc) Documentation for SkPy: <a href=\"https:\/\/skpy.t.allofti.me\/index.html\">https:\/\/skpy.t.allofti.me\/index.html<\/a><\/p>\n<h4>Bonus<\/h4>\n<p>You can see how I use this code and put out a different message every weekday in this repo: <a href=\"https:\/\/github.com\/qxf2\/skype_bots\">https:\/\/github.com\/qxf2\/skype_bots<\/a>.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>I recently wrote a script to post messages on Skype using Python. I used the excellent SkPy module. The module&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,199],"tags":[],"class_list":["post-11481","post","type-post","status-publish","format-standard","hentry","category-python","category-skype"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=11481"}],"version-history":[{"count":11,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11481\/revisions"}],"predecessor-version":[{"id":15568,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/11481\/revisions\/15568"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=11481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=11481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=11481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}