Search code examples
node.jstwiliowhatsapp

Send media to whatsapp via conversation api


So I found in twilio docs how to send media via api, but I need to map to conversation id as well. I see there is possibility to pass mediaSid, but I haven't found how to get that id and upload media. Any guess?

So it is possible to send

client.messages
      .create({
        from: "whatsapp:your_number",
        to: "whatsapp:recepient_number",,
        mediaUrl: [mediaUrl],
      })

But I used conversation api to send sms and WhatsApp messages

client.conversations.v1
      .conversations(conversationId)
      .messages.create({
        author,
        body,
        attributes,
      })

So if conversation has linked participants with prefix whatsapp:, it will send message to WhatsApp. It is possible to add messageSid to create method, but I don't know how to upload media to twilio to get that sid


Solution

  • Conversations does it slightly differently, I believe you're talking about a backend use case (without using an SDK).

    You use the Media resource to upload media, and get mediaSids, then you attach the mediaSids to the message when posting through the Message resource.

    As you may note, Media resource is not represented in the helper libraries (yet), so you will need to craft requests to it manually, using fetch or something similar. You can post to the Messages resource with a helper library, just as you have shown in the question.

    Then send the message normally (using a helper library) to a conversation containing a whatsapp participant, attach a mediaSid to it. Note that certain media size limits apply.