Enviar uma mensagem do Viber Business

Chave Descrição
VONAGE_APPLICATION_ID O ID da Application da Vonage que você criou.
VIBER_SERVICE_MESSAGE_ID Seu ID de mensagem do Viber Business.
TO_NUMBER O número de telefone para o qual você está enviando a mensagem.

NOTA: Não use um espaço à esquerda + ou 00 Ao digitar um número de telefone, comece com o código do país, por exemplo, 447700900000.

Exemplo

Escreva o código

Adicione o seguinte ao arquivo ` send-text.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": "text",
    "text": "This is an Viber Business Service text message sent using the Vonage Messages API."
  }'

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

bash send-text.sh

Pré-requisitos

npm install @vonage/server-sdk @vonage/messages

Crie um arquivo chamado ` send-text.js ` e insira o seguinte código:

const { Vonage } = require('@vonage/server-sdk');
const { Channels } = require('@vonage/messages');

/**
 * It is best to send messages using JWT instead of basic auth. If you leave out
 * apiKey and apiSecret, the messages SDK will send requests using JWT tokens
 *
 * @link https://developer.vonage.com/en/messages/technical-details#authentication
 */
const vonage = new Vonage(
  {
    applicationId: VONAGE_APPLICATION_ID,
    privateKey: VONAGE_PRIVATE_KEY,
  },
  {
    ...(MESSAGES_API_URL ? {apiHost: MESSAGES_API_URL} : {}),
  },
);

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` send-text.js`:

vonage.messages.send({
  messageType: 'text',
  channel: Channels.VIBER,
  text: 'This is a Viber Service Message text message sent using the Messages API',
  to: MESSAGES_TO_NUMBER,
  from: VIBER_SENDER_ID,
})
  .then(({ messageUUID }) => console.log(messageUUID))
  .catch((error) => console.error(error));

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

node send-text.js

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

implementation 'com.vonage:server-sdk-kotlin:2.1.1'

Crie um arquivo chamado ` SendViberText ` e adicione o código a seguir ao método ` main `:

val client = Vonage {
    applicationId(VONAGE_APPLICATION_ID)
    privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao método ` main ` do arquivo ` SendViberText `:

val messageId = client.messages.send(
    viberText {
        to(MESSAGES_TO_NUMBER)
        from(VIBER_SENDER_ID)
        text("This is a Viber text message sent using the Messages API")
    }
)

Ver código-fonte completo

Execute seu código

Podemos usar o plugin “ aplicativo ” para o Gradle a fim de simplificar a execução do nosso aplicativo. Atualize seu arquivo ` build.gradle ` com o seguinte:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Execute o seguinte comando ` gradle ` para rodar seu aplicativo, substituindo ` com.vonage.quickstart.kt.messages.viber ` pelo pacote que contém ` SendViberText`:

gradle run -Pmain=com.vonage.quickstart.kt.messages.viber.SendViberText

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

implementation 'com.vonage:server-sdk:9.3.1'

Crie um arquivo chamado ` SendViberText ` e adicione o código a seguir ao método ` main `:

VonageClient client = VonageClient.builder()
		.applicationId(VONAGE_APPLICATION_ID)
		.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
		.build();

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao método ` main ` do arquivo ` SendViberText `:

var response = client.getMessagesClient().sendMessage(
		ViberTextRequest.builder()
			.from(VIBER_SENDER_ID).to(MESSAGES_TO_NUMBER)
			.text("Don't miss out on our latest offers!")
			.category(Category.PROMOTION)
			.build()
);
System.out.println("Message sent successfully. ID: "+response.getMessageUuid());

Ver código-fonte completo

Execute seu código

Podemos usar o plugin “ aplicativo ” para o Gradle a fim de simplificar a execução do nosso aplicativo. Atualize seu arquivo ` build.gradle ` com o seguinte:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Execute o seguinte comando ` gradle ` para rodar seu aplicativo, substituindo ` com.vonage.quickstart.messages.viber ` pelo pacote que contém ` SendViberText`:

gradle run -Pmain=com.vonage.quickstart.messages.viber.SendViberText

Pré-requisitos

Install-Package Vonage

Escreva o código

Adicione o seguinte ao arquivo ` SendSms.cs`:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var vonageClient = new VonageClient(credentials);
var request = new ViberTextRequest
{
    To = MESSAGES_TO_NUMBER,
    From = VIBER_SENDER_ID,
    Text = "An SMS sent using the Vonage Messages API"
};
var response = await vonageClient.MessagesClient.SendAsync(request);

Ver código-fonte completo

Pré-requisitos

composer require vonage/client

Crie um arquivo chamado ` send-text.php ` e insira o seguinte código:

$keypair = new \Vonage\Client\Credentials\Keypair(
    file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
    VONAGE_APPLICATION_ID
);

$client = new \Vonage\Client($keypair);

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` send-text.php`:

$viber = new \Vonage\Messages\Channel\Viber\ViberText(
    TO_NUMBER,
    FROM_NUMBER,
    'This is a text message sent using the Vonage PHP SDK'
);

$client->messages()->send($viber);

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

php send-text.php

Pré-requisitos

pip install vonage python-dotenv

Escreva o código

Adicione o seguinte ao arquivo ` send-text.py`:

from vonage import Auth, Vonage
from vonage_messages import ViberText

client = Vonage(
    Auth(
        application_id=VONAGE_APPLICATION_ID,
        private_key=VONAGE_PRIVATE_KEY,
    )
)

message = ViberText(
    to=MESSAGES_TO_NUMBER,
    from_=VIBER_SENDER_ID,
    text="This is a Viber message sent via the Vonage Messages API.",
)

response = client.messages.send(message)
print(response)

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

python messages/viber/send-text.py

Pré-requisitos

gem install vonage

Crie um arquivo chamado ` send-text.rb ` e insira o seguinte código:

client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: VONAGE_PRIVATE_KEY
)

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` send-text.rb`:

message = client.messaging.viber(
  type: 'text',
  message: "This is a Viber Business Service text message sent using the Vonage Messages API"
)

client.messaging.send(
  from: VIBER_SENDER_ID,
  to: MESSAGES_TO_NUMBER,
  **message
)

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

ruby send-text.rb

DICA: Se estiver realizando testes usando o Curl, você precisará de um JWT. Você pode ver como criar um na documentação em criando um JWT.

Enviando uma mensagem pelo Viber

A Messages API permite enviar mensagens para diversos canais, incluindo o Facebook Messenger, SMS, WhatsApp e Viber. Este tutorial descreve como usar a Messages API para enviar uma mensagem pelo Viber.

Etapas
1
Introdução a este tutorial
2
Prerequisites
3
Enviar uma mensagem pelo Viber
4
E agora?