How to send Whatsapp message using MessageBird from Firebase cloud function?

1.1k Views Asked by At

I have installed MessageBird Firebase Extension. I want to send Whatsapp template(template has Button with website link) message from Firebase Cloud Functions.

To send a Whatsapp message, I need to create a document in Firebase 'messages' collection, so that MessageBird extension processes it.

What should be the format of Firebase document to send Whatsapp message? I have attached the Whatsapp templateenter image description here

I have tried creating Firebase document similar to this API request format - https://developers.messagebird.com/quickstarts/whatsapp/send-message-with-buttons/, but I got delivery error 'Error: api error(s): JSON is not a valid format (code: 21)'.

Thank you.

2

There are 2 best solutions below

2
NIBERIUM On

You are right, you need to put a document with content into Firestore message collection that is accepted by ConversationsAPI, and then the message should be sent and the document should be updated with delivery details (response from MessageBird Conversations API).

This is example of the document that you will need to put to be able to send WA template message:

db.collection('YOUR_DOCUMENT_COLLECTION').add({
  channelId: 'YOUR_CHANNEL_ID',
  type: 'hsm',
  content: {
    hsm: {
      namespace: 'YOUR_WA_ACCOUNT_NAMESPACE_ID',
      templateName: 'YOUR_WA_TEMPLATE_NAME',
      params: [{ default: 'YOU_PARAM_VALUE' }],
      language: { code: 'en_US', policy: 'deterministic' }
    }
  },
  to: 'RECIPIENT_OF_THE_MESSAGE',
});

You can find more details about HSM object and what each field means here: https://developers.messagebird.com/api/conversations/#messagehsm-object

I hope that helps.

0
Ravshanbek Ahmedov On

For using content variable and button variable same time, you need use component field:

{
"to": "RECEIVER",
"from": "SENDER",
"type": "hsm",
"content": {
    "hsm": {
        "namespace": "c24c325c_example",
        "templateName": "example",
        "language": {
            "code": "en"
        },
        "components": [
            {
                "type": "button",
                "sub_type": "url",
                "parameters": [
                    {
                        "type": "text",
                        "text": "&from=whatsapp"
                    }
                ]
            },
            {
                "type": "body",
                "parameters": [
                    {
                        "type": "text",
                        "text": "Hi"
                    }
                ]
            }
        ]
    }
}