Enviar um vídeo
Neste trecho de código, você verá como enviar um vídeo no Viber usando a Messages API.
NOTA: É importante que você leia essas informações sobre contas do Viber e mensagens antes de testar este trecho de código.
Para obter um guia passo a passo sobre esse assunto, você pode ler nosso tutorial Envio de mensagens do Viber Business com a Messages API.
Exemplo
Veja a seguir a descrição de todas as variáveis utilizadas em cada trecho de código:
| Chave | Descrição |
|---|---|
MESSAGES_API_URL | There are two versions of the API, each with their own endpoints. For production the previous Messages API endpoint was |
JWT | Used to authenticate your request. See |
TO_NUMBER | Replace with the number you are sending to. E.g. |
FROM_NUMBER | Replace with number you are sending from. E.g. |
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.
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
Escreva o código
Adicione o seguinte ao arquivo ` send-video.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": "video",
"video": {
"url": "'${MESSAGES_VIDEO_URL}'",
"thumb_url": "'${MESSAGES_IMAGE_URL}'"
},
"viber_service": {
"duration": "'${MESSAGES_VIDEO_DURATION}'",Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
npm install @vonage/server-sdk @vonage/messagesCrie um arquivo chamado ` send-video.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} : {}),
},
);Escreva o código
Adicione o seguinte ao arquivo ` send-video.js`:
vonage.messages.send({
type: 'video',
channel: Channels.VIBER,
video: {
url: MESSAGES_VIDEO_URL,
thumbUrl: MESSAGES_IMAGE_URL,
},
to: MESSAGES_TO_NUMBER,
from: VIBER_SENDER_ID,
})
.then(({ messageUUID }) => console.log(messageUUID))
.catch((error) => console.error(error));Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
Adicione o seguinte ao arquivo ` build.gradle`:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Crie um arquivo chamado ` SendViberVideo ` e adicione o código a seguir ao método ` main `:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Escreva o código
Adicione o seguinte ao método ` main ` do arquivo ` SendViberVideo `:
val messageId = client.messages.send(
viberVideo {
to(MESSAGES_TO_NUMBER)
from(VIBER_SENDER_ID)
url(MESSAGES_VIDEO_URL)
thumbUrl(VIBER_THUMB_URL)
category(Category.TRANSACTION)
ttl(VIBER_VIDEO_TTL)
fileSize(VIBER_VIDEO_FILE_SIZE)
duration(VIBER_VIDEO_DURATION)
}
)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 ` SendViberVideo`:
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
Adicione o seguinte ao arquivo ` build.gradle`:
implementation 'com.vonage:server-sdk:9.3.1'Crie um arquivo chamado ` SendViberVideo ` 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();Escreva o código
Adicione o seguinte ao método ` main ` do arquivo ` SendViberVideo `:
var response = client.getMessagesClient().sendMessage(
ViberVideoRequest.builder()
.to(MESSAGES_TO_NUMBER)
.from(VIBER_SENDER_ID)
.url(MESSAGES_VIDEO_URL)
.thumbUrl(VIBER_THUMB_URL)
.category(Category.TRANSACTION)
.fileSize(VIBER_VIDEO_FILE_SIZE)
.duration(VIBER_VIDEO_DURATION)
.ttl(VIBER_VIDEO_TTL)
.build()
);
System.out.println("Message sent successfully. ID: "+response.getMessageUuid());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 ` SendViberVideo`:
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
Install-Package VonageEscreva 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 ViberVideoRequest()
{
To = MESSAGES_TO_NUMBER,
From = VIBER_SENDER_ID,
Video = new ViberVideoRequest.VideoInformation()
{
Url = MESSAGES_VIDEO_URL,
}
};
var response = await vonageClient.MessagesClient.SendAsync(request);Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
composer require vonage/clientCrie um arquivo chamado ` send-file.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);Escreva o código
Adicione o seguinte ao arquivo ` send-file.php`:
$viber = new \Vonage\Messages\Channel\Viber\ViberVideo(
TO_NUMBER,
FROM_NUMBER,
'https://example.com/file.zip'
);
$client->messages()->send($viber);Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
pip install vonage python-dotenvEscreva o código
Adicione o seguinte ao arquivo ` send-video.py`:
from vonage import Auth, Vonage
from vonage_messages import ViberVideo, ViberVideoOptions, ViberVideoResource
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY,
)
)
message = ViberVideo(
to=MESSAGES_TO_NUMBER,
from_=VIBER_SENDER_ID,
video=ViberVideoResource(url=MESSAGES_VIDEO_URL, thumb_url=MESSAGES_IMAGE_URL),
viber_service=ViberVideoOptions(
duration=MESSAGES_VIDEO_DURATION,
file_size=MESSAGES_VIDEO_FILE_SIZE,
),
)
response = client.messages.send(message)
print(response)Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
Se você não tiver um aplicativo, acesse criar um. Certifique-se também de acessar configure seus webhooks.
gem install vonageCrie um arquivo chamado ` send-video.rb ` e insira o seguinte código:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: VONAGE_PRIVATE_KEY
)Escreva o código
Adicione o seguinte ao arquivo ` send-video.rb`:
message = client.messaging.viber(
type: 'video',
message: {
url: MESSAGES_VIDEO_URL,
thumb_url: MESSAGES_IMAGE_URL
},
opts: {
viber_service: {
duration: MESSAGES_VIDEO_DURATION,
file_size: MESSAGES_VIDEO_FILE_SIZE
}
}
)
client.messaging.send(
from: VIBER_SENDER_ID,
to: MESSAGES_TO_NUMBER,
**message
)Execute seu código
Salve este arquivo no seu computador e execute-o:
Experimente
Ao executar o código, um vídeo é enviado pelo Viber para o número de destino.