bettersg / checkMate

GNU General Public License v3.0
5 stars 1 forks source link

Possibility of messages being sent but not delivered #174

Closed sarge1989 closed 1 year ago

sarge1989 commented 1 year ago

Summary

In whatsapp, calling the API to send messages will return HTTP 200 and indicate that the message is successfully sent.

That said, sent does not equate to delivery. See below link.

https://developers.facebook.com/docs/whatsapp/cloud-api/support/troubleshooting

To determine why messages couldnt be delivered, we need to listen to the status delivery updates webhook, which the app is not currently currently doing.

Context

Recently, we received some cases where checkers were deactivated by the daily job for having votes outstanding for >3 days. Normally, they would be sent a template message with a button they can easily press to reactivate. However, because some did not have their template messages delivered, they didn't even realise they were deactivated.

One of them fell under this category.

Proposed Actions

As such, probably need to enhance the webhook handler to accept not just message webhooks but also status update webhooks.

https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components [See: When the status for a message received by a business changes]

sarge1989 commented 1 year ago

For now, intending to accept incoming webhooks status updates, such as the below:

    "object": "whatsapp_business_account",
    "entry": [
      {
        "id": "",
        "changes": [
          {
            "value": {
              "messaging_product": "whatsapp",
              "metadata": {
                "display_phone_number": "",
                "phone_number_id": ""
              },
              "statuses": [
                {
                  "id": "wamid.HBgKNjU5MzM5OTE1MRUCABEYEkIxRUUyQTQ4NEM3NUI0M0FEMQA=",
                  "status": "failed",
                  "timestamp": "",
                  "recipient_id": "",
                  "errors": [
                    {
                      "code": 130472,
                      "title": "User's number is part of an experiment",
                      "message": "User's number is part of an experiment",
                      "error_data": {
                        "details": "Failed to send message because this user's phone number is part of an experiment"
                      },
                      "href": "https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/"
                    }
                  ]
                }
              ]
            },
            "field": "messages"
          }
        ]
      }
    ]
  }

if we detect status = failed, to log error object