Terrance/SkPy

KeyError in SkypeEventLoop when a new contact sends a message

Opened this issue · 3 comments

I am using the event example from the README, using the SkypeEventLoop.

When a new user that is not in my contact list writes me, the program crashes. The trackback is at the bottom of this report.

When I start the script again, it works as expected.

Account type

Live Account, created several days ago

Conversation details

1:1 chat with a new (not approved) contact

Steps to reproduce

Run the following script, and write to Skype account from a new, previously unapproved contact.

In order to test it, so you don't need to create an account, you can delete the contact (Right click on the Contact > Profile > Delete Contact ), and afterwards delete the conversation, and restart the script.

from skpy import SkypeEventLoop, SkypeNewMessageEvent

username = 'your@email'
password = 'your password'

class SkypePing(SkypeEventLoop):
    def onEvent(self, event):
        # return if it is not an MessageEvent
        if not isinstance(event, SkypeNewMessageEvent):
            return
        
        # return if it is my own message
        if event.msg.userId == self.userId:
            return

        print(event.msg.chat)

        if "ping" in event.msg.content.lower():
            event.msg.chat.sendMsg("Pong!")

if __name__ == "__main__":
    sk = SkypePing(username, password, autoAck=True)
    sk.loop()

Result or traceback

{{---skype-id---}} is the Skype ID of the sender account, removed from the traceback for privacy issues

The program crashes with the following error
Traceback (most recent call last):
  File "/home/martin/work/venv/skype_test/lib/python3.10/site-packages/skpy/chat.py", line 457, in __getitem__
    return super(SkypeChats, self).__getitem__(key)
  File "/home/martin/work/venv/skype_test/lib/python3.10/site-packages/skpy/core.py", line 138, in __getitem__
    return self.cache[key]
KeyError: '8:${{---skype-id---}}'

This traceback looks incomplete -- it doesn't have any reference to your own code, and the exception in question is caught on the next line. Either you're looking at the caught exception (which is expected to happen) or I need a full traceback to go on.

You are right, the trackback is incomplete, I thought that the rest would be irrelevant, but it is not.

Also there was a missing print statement in the code that I provided in the issue. I edited the original description above, and added the print statement.

Anyway I found the issue... the issue was that I was trying to print event.msg.chat. Once I remove the print the exception does not appear. But still it is something that you might want to look into eventually

Here is the full traceback:

Traceback (most recent call last):
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/chat.py", line 457, in __getitem__
    return super(SkypeChats, self).__getitem__(key)
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/core.py", line 138, in __getitem__
    return self.cache[key]
KeyError: '8:{{---sender-skype-id---}}'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/skypebot/event.py", line 28, in <module>
    sk.loop()
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/main.py", line 213, in loop
    self.cycle()
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/main.py", line 202, in cycle
    self.onEvent(event)
  File "/home/ubuntu/skypebot/event.py", line 16, in onEvent
    print(event.msg.chat)
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/util.py", line 148, in chatObj
    return self.skype.chats[getattr(self, field)]
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/chat.py", line 459, in __getitem__
    return self.chat(key)
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/chat.py", line 492, in chat
    auth=SkypeConnection.Auth.RegToken, params={"view": "msnp24Equivalent"}).json()
  File "/home/ubuntu/skypebot/venv/lib/python3.6/site-packages/skpy/conn.py", line 237, in __call__
    raise SkypeApiException("{0} response from {1} {2}".format(resp.status_code, method, url), resp)
skpy.core.SkypeApiException: ('404 response from GET https://azwus1-client-s.gateway.messenger.live.com/v1/users/ME/conversations/8:{{---sender-skype-id---}}', <Response [404]>)

So the Skype APIs are saying that the conversation you got a message in doesn't exist, which isn't very helpful and also not reproducible here -- if I delete a chat (either through SkPy or in Skype for Web), I can still retrieve the basic chat info:

>>> sk.chats['8:fred.2'].delete()
<= [20/10 21:44:33] DELETE https://azwus2-client-s.gateway.messenger.live.com/v1/users/ME/conversations/8:fred.2/messages
{}
=> [20/10 21:44:34] 200
...

>>> sk.chats.cache.clear()

>>> sk.chats['8:fred.2']
<= [20/10 21:44:48] GET https://azwus2-client-s.gateway.messenger.live.com/v1/users/ME/conversations/8:fred.2
{'params': {'view': 'msnp24Equivalent'}}
=> [20/10 21:44:48] 200
...
{'id': '8:fred.2', ...}
SkypeSingleChat(id='8:fred.2', alerts=True, userId='fred.2')