News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Listening for Subscription Events in Python SDK
Tags: sdk, subscription, pubnub, authentication, webhooks
Feb 1, 2023 at 11:04am   •   1 replies  •  0 likes
prospect-health

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 Answer
answered on Feb 2, 2023 at 9:15am  

 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us