prospect-health avatar image
prospect-health asked prospect-health commented

Listening for Subscription Events in Python SDK

I originally had a webhook setup in Azure for receiving RingCentral event subscriptions, but this no longer works due to some issue with Azure HTTP functions being unable to handle chunked encoding in requests.

To replace the webhook, I have tried using the RingCentral SDK for Python to setup a PubNub subscription that listens for the same events. I have got it setup so that it successfully sets up the subscription and delivers the events to where I'm storing them. However, after a day or two of running the events stop feeding through or the script encounters a "subscription is not alive" exception and stops working.

Should I be able to just set the subscription running in a Python script and leave it for years uninterrupted or does there need to be some code added in to handle refreshing subscriptions/auth tokens etc. to make sure that it can receive all event subscriptions uninterrupted? It's quite important that I can leave it running for ages and that it doesn't ever miss any incoming events.

Here is the code that I am using (I'm not a pro-coder, I just dabble). Can anyone spot any issues with it or advise on what needs to be changed to get it working in the way that I want? If there's any recommended services outside of Azure for setting up a Python webhook as well, I would be all ears to that, as the webhook setup feels more robust to me. Thanks.

import time
import json
import requests
import pubnub
from threading import Thread
from ringcentral import SDK
from ringcentral.subscription import Events

platform = sdk.platform()

res = platform.get('/account/~/extension/~')

def on_message(msg):
          #write the data to storage
     except Exception as ex:
          #email me the exception traceback

def nubpub():
          s = sdk.create_subscription()
          s.on(Events.notification, on_message)
          while True:
     except Exception as ex:
          #email me the exception traceback

       t = Thread(target=nubpub)
except Exception as ex:
       #email me the exception traceback
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered prospect-health sent
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

prospect-health avatar image prospect-health commented ·

Thanks for these suggestions @Phong Vu .

I have looked through the links that you have provided, but it seems that the Azure webhook is now working again. Not sure if this is due to the change that your mentioned in this post: If so, I'm assuming that it will stop working again sometime within the next month? I think that I will have to wait and see if this happens again before trying to apply some of the suggestions in the threads you linked.

To answer your question about my code, the exceptions that I included were in the hope that I would be able to catch any instance of the script failing and have it send me an email in those instances so that I could minimise the downtime. I included the "sleep(30)" line in case there were instances where the script was looping through exceptions, so that my inbox didn't get flooded with these exception emails. However, it appears that those catch blocks in my code don't catch the "subscription is not alive" state, as this message gets delivered straight to the shell output. This is the traceback that it displays:

0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys