Terrance/SkPy

Not access contacts to send chats while executing a script

Closed this issue · 1 comments

Before we start...

  • I've searched existing issues, but my problem hasn't been reported yet.
  • I've read the documentation (including notes on error messages and rate limiting), but my problem is something else.
  • I've tested the behaviour on Skype for Web, and it works there but not with SkPy.

Summary

I have faced issue to send message to a contact while executing a script. It shows the recipient chat with the username 'cavisson.abhishek.singh' cannot be found or accessed. Please help

Code sample

SKYPE_USERNAME="*********"
SKYPE_PASSWORD="*********"
RECIPIENT='Cavisson.Abhishek.Singh'


# Function to send Skype message
send_skype_message() {
    message=$1
    python3 - <<EOF
from skpy import Skype
sk = Skype('$SKYPE_USERNAME', '$SKYPE_PASSWORD')
recipient = sk.contacts.search('$RECIPIENT')
if recipient:
    recipient_id = recipient[0].id
    sk.chats[recipient_id].sendMsg('$message')
else:
    print("Recipient not found on Skype")
EOF
}

Code output

/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (2.0.3) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "/home/cavisson/.local/lib/python3.8/site-packages/skpy/chat.py", line 476, in __getitem__
    return super(SkypeChats, self).__getitem__(key)
  File "/home/cavisson/.local/lib/python3.8/site-packages/skpy/core.py", line 138, in __getitem__
    return self.cache[key]
KeyError: 'cavisson.abhishek.singh'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "/home/cavisson/.local/lib/python3.8/site-packages/skpy/chat.py", line 478, in __getitem__
    return self.chat(key)
  File "/home/cavisson/.local/lib/python3.8/site-packages/skpy/chat.py", line 511, in chat
    json = self.skype.conn("GET", "{0}/users/ME/conversations/{1}".format(self.skype.conn.msgsHost, id),
  File "/home/cavisson/.local/lib/python3.8/site-packages/skpy/conn.py", line 249, in __call__
    raise SkypeApiException("{0} response from {1} {2}".format(resp.status_code, method, url), resp)
skpy.core.SkypeApiException: ('400 response from GET https://azwus1-client-s.gateway.messenger.live.com/v1/users/ME/conversations/cavisson.abhishek.singh', <Response [400]>)

Explain your code

I want to execute a script thats while running send me messages regarding all execution steps.

SkPy version

Name: SkPy Version: 0.10.6

Python version

python3.8

Anything else?

No

You don't need to search for a user by their ID, just retrieve the SkypeUser directly via sk.contacts[userId].

More importantly, you're treating the user ID as a chat ID (aka. thread identifier), which is causing the 400 error because the two are in different formats -- you should be using SkypeUser.chat to get the corresponding private chat, or manually construct the chat ID by adding the thread identifier prefix (8:, assuming you're working with a Skype username) to the user ID when retrieving it with sk.chats[chatId].