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
Webhook stopped working
Tags: webhooks, subscription, rest api
Nov 14, 2023 at 9:38am   •   2 replies  •  0 likes
Slice Supplies

Hi,

Webhook is not working in the sense that nothing hits my endpoint when an SMS comes in, but when i subscribe, it does send the validation token (so I know the end point is reachable). It was working perfectly fine and then just stopped, I have deleted the subscription and resubscribed.

Any help would be appreciated!

Endpoint:

@app.route('/webhook/ringcentral', methods=['POST'])
def ringcentral_webhook():
    try:
        validation_token = request.headers.get('Validation-Token')
        
        if validation_token:
            return make_response('', 200, {'Validation-Token': validation_token})
        
        data = request.get_json(silent=True)
        logging.info(data)
        if data:
            sender = data.get('body', {}).get('from', {})
            phone_number = sender.get('phoneNumber', 'Unknown Number')
            sender_name = sender.get('name', {phone_number})
            message_subject = data.get('body', {}).get('subject', 'No subject')
            
            text = f"New SMS from: {sender_name}. Message: {message_subject}."
            slack.slack_sender(text)
            
            logging.info(data)
            return jsonify({'status': 'success'}), 200
        else:
            logging.info("No JSON payload received.")
            return jsonify({'status': 'error', 'message': 'No JSON payload'}), 400
    except Exception as e:
        logging.error(f"Error processing the webhook data: {e}")
        return jsonify({'status': 'error', 'message': str(e)}), 500


the subscribe code:

    def clear_webhook_subscriptions(func):
        def wrapper(self, *args, **kwargs):
            self.read_subscriptions()
            return func(self, *args, **kwargs)
        return wrapper

    @clear_webhook_subscriptions
    def subscribe_for_notification(self, event_filters=['/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS'], delivery_address = 'REDACTED'):
        try:
            body_params = {
                'eventFilters': event_filters,
                'deliveryMode': {
                    'transportType': 'WebHook',
                    'address': delivery_address + '/webhook/ringcentral'
                },
                'expiresIn': 604800 #TODO:Rework this. can be upto 10 yrs. 315360000
            }
            endpoint = "/restapi/v1.0/subscription"
            resp = self.post(endpoint, json=body_params)
            resp.raise_for_status()

            response = resp.json()
            logging.info(f"Subscription ID: {response['id']}")
            logging.info("Ready to receive incoming SMS via WebHook.")
            return response
        except HTTPError as http_err:
            logging.error(f"HTTP error occurred: {http_err}")
        except Exception as err:
            logging.error(f"An exception occurred: {err}")

    def read_subscriptions(self):
        try:
            endpoint = "/restapi/v1.0/subscription"
            resp = self.get(endpoint)
            response = resp.json()
            if not response['records']:
                logging.info("No subscription found.")
            else:
                for record in response['records']:
                    logging.info(json.dumps(record, indent=2, sort_keys=True))
                    self.delete_subscription(record['id'])
        except Exception as e:
            logging.error(f"An exception occurred while reading subscriptions: {e}")

    def delete_subscription(self, subscription_id):
        try:
            endpoint = f"/restapi/v1.0/subscription/{subscription_id}"
            self.delete(endpoint)
            logging.info(f"Subscription {subscription_id} deleted.")
        except Exception as e:
            logging.error(f"An exception occurred while deleting subscription {subscription_id}: {e}")
2 Answers
answered on Nov 14, 2023 at 12:19pm  

@Phong Vu Thanks for your response. Here is an example of the output from read_subscription, I have already deleted this subscription and created a new one. Everything looks fine, and I do see a post request when I subscribe, so i know the endpoint is reachable, just nothing when we get a text.


2023-11-14 15:15:11,431 - INFO - [14/Nov/2023 15:15:11] "POST /webhook/ringcentral HTTP/1.1" 200 -
2023-11-14 15:14:58,817 - INFO - {
  "creationTime": "2023-11-14T17:32:57.920Z",
  "deliveryMode": {
    "address": "REDACTED/webhook/ringcentral",
    "encryption": false,
    "transportType": "WebHook"
  },
  "eventFilters": [
    "/restapi/v1.0/account/REDACTED/extension/REDACTED/message-store/instant?type=SMS"
  ],
  "expirationTime": "2023-11-21T17:32:57.920Z",
  "expiresIn": 595079,
  "id": "72f3f907-bc4f-4c85-a7fb-dREDACTED",
  "status": "Active",
  "uri": "https://platform.ringcentral.com/restapi/v1.0/subscription/72f3f907-bc4f-4c85-a7fb-dREDACTED"
}

 0
answered on Nov 14, 2023 at 11:54am  

When you call the read_subscriptions method, what do you get?

First, verify if your subscription status is active. Then if it is active and you still cannot receive instant message notifications, check the phone number you send SMS messages to to see if the number is owned by the authenticated user.


 1
on Nov 14, 2023 at 12:28pm   •  0 likes

You were right, my sandbox config loaded ,so rightfully so, we aren't receiving text messages on that number. Thank you for helping nudge me in the right direction.



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