KapsoEx.WhatsApp.Media (kapso_ex v0.1.0)

Copy Markdown View Source

Upload, inspect, delete and download WhatsApp media.

Size limits set by Meta: images (jpeg, png) 5 MB; video (mp4, 3gp) and audio (aac, mp3, ogg, opus) 16 MB; documents 100 MB; stickers (webp) 100 KB static or 500 KB animated.

To upload media from a public URL instead of bytes, use KapsoEx.Platform.Media.upload/4.

Summary

Functions

Deletes an uploaded media file.

Downloads the bytes of a media file.

Gets media metadata, including a url and a short-lived download_url (valid for about 5 minutes).

Uploads content (a binary) and returns its media ID.

Uploads the file at path. Options: :filename (defaults to the file's basename) and :content_type.

Types

result()

@type result() :: {:ok, map()} | {:error, KapsoEx.Error.t()}

Functions

delete(client, media_id, phone_number_id)

@spec delete(KapsoEx.Client.t(), String.t(), String.t()) :: result()

Deletes an uploaded media file.

download(client, media_id, phone_number_id)

@spec download(KapsoEx.Client.t(), String.t(), String.t()) ::
  {:ok, binary()} | {:error, KapsoEx.Error.t()}

Downloads the bytes of a media file.

Fetches the metadata with get/3 and then the signed download_url, so the media must be known to Kapso (inbound media, or outbound media shortly after it was sent).

{:ok, bytes} = Media.download(client, media_id, phone_number_id)

get(client, media_id, phone_number_id)

@spec get(KapsoEx.Client.t(), String.t(), String.t()) :: result()

Gets media metadata, including a url and a short-lived download_url (valid for about 5 minutes).

upload(client, phone_number_id, content, opts)

@spec upload(KapsoEx.Client.t(), String.t(), iodata(), keyword()) :: result()

Uploads content (a binary) and returns its media ID.

Options

  • :filename - required, for example "photo.jpg".
  • :content_type - MIME type; inferred from :filename when omitted.

Examples

{:ok, %{"id" => media_id}} =
  Media.upload(client, phone_number_id, File.read!("photo.jpg"), filename: "photo.jpg")

Messages.send_image(client, phone_number_id, "56961567267", media_id, caption: "Hi")

upload_file(client, phone_number_id, path, opts \\ [])

@spec upload_file(KapsoEx.Client.t(), String.t(), Path.t(), keyword()) :: result()

Uploads the file at path. Options: :filename (defaults to the file's basename) and :content_type.