Question

Listening for Subscription Events in Python SDK

  • 1 February 2023
  • 1 reply
  • 315 views

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


sdk = SDK('CLIENTKEY', 'CLIENTSECRET', 'https://platform.ringcentral.com')
platform = sdk.platform()
platform.login('ACCOUNTNUMBER', 'EXTENSION', 'PASSWORD')

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


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

def nubpub():
     try:
          s = sdk.create_subscription()
          s.add_events(['/account/ACCOUNTNUMBER/telephony/sessions'])
          s.on(Events.notification, on_message)
          s.register()
          while True:
               time.sleep(0.1)
     except Exception as ex:
          #email me the exception traceback
          time.sleep(30)

try:
       t = Thread(target=nubpub)
       t.start()
except Exception as ex:
       #email me the exception traceback

1 reply

Userlevel 1

Which line from your code print the error "subscription is not alive"? What is the purpose to sleep (30) within the exception?

Have you tried any of these instructions

https://learn.microsoft.com/en-us/answers/questions/971790/transfer-encoding-chunked-header-for-a-response-st

https://github.com/Azure/azure-functions-host/issues/4926

https://stackoverflow.com/questions/58527919/azure-function-how-to-received-data-via-chunked-encoding

Reply