Terrance/SkPy

Event loop stop working after running for a while.

Closed this issue · 4 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 run event loop to receive all message from a Skype group. It work fine but after a while (1 days) it start getting this error below and it stop receiving any message anymore.

skpy.core.SkypeApiException: ('401 response from POST https://azeus1-client-s.gateway.messenger.live.com/v1/users/ME/endpoints/SELF/subscriptions/0/poll', <Response [401]>)

Code sample

class Bot(SkypeEventLoop):
    def __init__(self):
        self.username = os.getenv("SKYPE_USERNAME", "")
        self.password = os.getenv("SKYPE_PASSWORD", "")
        super(Bot, self).__init__(
            self.username, self.password, tokenFile="tokenCred.txt", autoAck=True
        )

    def onEvent(self, event):
        try:
            if (
                isinstance(event, SkypeNewMessageEvent)
                and not event.msg.userId == self.userId
            ):
                print(event)
                print(event.msg)
         

        except Exception as e:
            print(e)


bot = Bot()
incoming_msg_thread = Thread(target=bot.loop, daemon=True)
incoming_msg_thread.start()

Code output

Exception in thread Thread-1 (loop):

 Traceback (most recent call last):

   File "/usr/local/lib/python3.11/threading.py", line 1045, in _bootstrap_inner

     self.run()

   File "/usr/local/lib/python3.11/threading.py", line 982, in run

     self._target(*self._args, **self._kwargs)

   File "/usr/local/lib/python3.11/site-packages/skpy/main.py", line 213, in loop

     self.cycle()

   File "/usr/local/lib/python3.11/site-packages/skpy/main.py", line 198, in cycle

     events = self.getEvents()

              ^^^^^^^^^^^^^^^^

   File "/usr/local/lib/python3.11/site-packages/skpy/conn.py", line 76, in wrapper

     return fn(self, *args, **kwargs)

            ^^^^^^^^^^^^^^^^^^^^^^^^^

   File "/usr/local/lib/python3.11/site-packages/skpy/conn.py", line 76, in wrapper

     return fn(self, *args, **kwargs)

            ^^^^^^^^^^^^^^^^^^^^^^^^^

   File "/usr/local/lib/python3.11/site-packages/skpy/main.py", line 111, in getEvents

     for json in self.conn.endpoints["self"].getEvents():

                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   File "/usr/local/lib/python3.11/site-packages/skpy/conn.py", line 1138, in getEvents

     return self.conn("POST", "{0}/users/ME/endpoints/{1}/subscriptions/0/poll".format(self.conn.msgsHost, self.id),

            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   File "/usr/local/lib/python3.11/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: ('401 response from POST https://azeus1-client-s.gateway.messenger.live.com/v1/users/ME/endpoints/SELF/subscriptions/0/poll', <Response [401]>)

Explain your code

My code is starting event loop in a thread to receive event.

SkPy version

0.10.6

Python version

3.11.7

Anything else?

No response

From the issue template:

Set the SKPY_DEBUG_HTTP=1 environment variable so that requests to the Skype API are logged.

From the issue template:

Set the SKPY_DEBUG_HTTP=1 environment variable so that requests to the Skype API are logged.

Here is result of the log says The provided registration token is expired, and no authentication ticket was provided. and then it stops receiving message anymore.

Screenshot 2024-02-13 at 8 40 38 in the morning

Has the token actually expired at this point? Check tokenExpiry on bot.conn at the point where the server rejects it, and compare with the current time -- SkPy should automatically renew your token for you if it expired, but you can also try manually renewing it by calling getSkypeToken() and/or getRegToken() as needed if Skype is rejecting it early.

Thank you for your time. I think the problem is on my side. My server date time is incorrect. It's late more than 5 minutes so it break the token renewal process.