question

Slice Supplies avatar image
Slice Supplies asked Slice Supplies commented

Webhook stopped working

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}")
rest apiwebhookssubscription
1 |3000

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

Phong Vu avatar image
Phong Vu answered Slice Supplies commented

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 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.

Slice Supplies avatar image Slice Supplies commented ·

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.

0 Likes 0 ·
Slice Supplies avatar image
Slice Supplies answered Slice Supplies edited

@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"
}
1 |3000

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

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