Enviar un mensaje de imagen

En este fragmento de código verá cómo enviar un mensaje de imagen de Viber utilizando Messages API.

NOTA: Es importante que lea esta información sobre cuentas y mensajería de Viber antes de probar este fragmento de código.

Para obtener una guía paso a paso sobre este tema, puede leer nuestro tutorial Envío de mensajes de empresa de Viber con Messages API.

Ejemplo

A continuación encontrará la descripción de todas las variables utilizadas en cada fragmento de código:

ClaveDescripción
VONAGE_APPLICATION_ID

The Vonage Application ID.

VONAGE_APPLICATION_PRIVATE_KEY_PATH

Private key path.

VONAGE_PRIVATE_KEY_PATH

Private key path.

BASE_URL

For production use the base URL is https://api.nexmo.com/. For sandbox testing the base URL is https://messages-sandbox.nexmo.com/.

MESSAGES_API_URL

There are two versions of the API, each with their own endpoints. For production the previous Messages API endpoint was https://api.nexmo.com/v0.1/messages, the new one is https://api.nexmo.com/v1/messages. For sandbox testing the Messages API endpoint is https://messages-sandbox.nexmo.com/v0.1/messages or https://messages-sandbox.nexmo.com/v1/messages, depending on which version you have set in the sandbox dashboard.

VONAGE_APPLICATION_ID

The Vonage Application ID.

VIBER_SERVICE_MESSAGE_ID

Your Viber Business Message ID. For sandbox testing this is 16273.

VONAGE_VIBER_SERVICE_MESSAGE_ID

Refer to VIBER_SERVICE_MESSAGE_ID above.

VONAGE_NUMBER

Refer to VIBER_SERVICE_MESSAGE_ID above.

FROM_ID

Refer to VIBER_SERVICE_MESSAGE_ID above.

TO_NUMBER

Replace with the number you are sending to. E.g. 447700900001

IMAGE_URL

The link to the image file to send.

NOTA: No utilice un + o 00 cuando introduzca un número de teléfono, empiece por el prefijo del país, por ejemplo, 447700900000.

Requisitos previos

Si no tiene una solicitud, puede crear uno. Asegúrese también de configure sus webhooks.

Escriba el código

Añada lo siguiente a send-image.sh:

curl -X POST "${MESSAGES_API_URL}" \
  -H "Authorization: Bearer "$JWT\
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d $'{
    "to": "'${MESSAGES_TO_NUMBER}'",
    "from": "'${VIBER_SENDER_ID}'",
    "channel": "viber_service",
    "message_type": "image",
    "image": {
      "url": "'${MESSAGES_IMAGE_URL}'"
    }
  }'

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

bash send-image.sh

Pruébalo

Al ejecutar el código, se envía un mensaje de imagen de Viber al número de destino.