Enviar uma mensagem com failover
Se uma mensagem enviada pela Messages API do Vonage for rejeitada, é possível definir mensagens de failover para serem enviadas em seu lugar. O failover pode ser feito de qualquer canal ou tipo de mensagem para qualquer outro canal ou tipo de mensagem; no entanto, o exemplo a seguir mostra o failover de RCS para SMS.
Exemplo
Veja a seguir a descrição de todas as variáveis utilizadas em cada trecho de código:
| Chave | Descrição |
|---|---|
VONAGE_APPLICATION_ID | The Vonage Application ID. |
VONAGE_PRIVATE_KEY_PATH | Private key path. |
TO_NUMBER | The number you are sending the to in E.164 format. For example |
RCS_SENDER_ID | The sender ID for the RCS message. |
JWT | Used to authenticate your request. See |
SMS_SENDER_ID | The alphanumeric string that represents the name or number of the organization sending the message. |
MESSAGES_API_URL | There are two versions of the API, each with their own endpoints. For production the previous Messages API endpoint was |
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-message-with-failover.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": "'${RCS_SENDER_ID}'",
"channel": "rcs",
"message_type": "text",
"text": "This is an RCS text message sent via the Vonage Messages API",
"failover": [
{
"to": "'${MESSAGES_TO_NUMBER}'",
"from": "'${SMS_SENDER_ID}'",
"channel": "sms",
"message_type": "text",
"text": "This is an SMS sent using the Vonage Messages API."
}
]
}'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 ` SendMessageWithFailover ` 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 ` SendMessageWithFailover `:
val messageId = client.messages.send(
rcsText {
to(MESSAGES_TO_NUMBER)
from(RCS_SENDER_ID)
text("This is an RCS text message sent using the Messages API")
failover(
smsText {
to(MESSAGES_TO_NUMBER)
from(SMS_SENDER_ID)
text("This is an SMS sent using the Vonage Messages API.")
}
)
}
)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 ` pelo pacote que contém ` SendMessageWithFailover`:
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 ` SendMessageWithFailover ` 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 ` SendMessageWithFailover `:
var response = client.getMessagesClient().sendMessage(
RcsTextRequest.builder()
.from(RCS_SENDER_ID).to(MESSAGES_TO_NUMBER)
.text("This is an RCS message sent via the Vonage Messages API")
.failover(SmsTextRequest.builder()
.from(SMS_SENDER_ID).to(MESSAGES_TO_NUMBER)
.text("This is an SMS sent using the Vonage Messages API.")
.build()
)
.build()
);
System.out.println(response);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 ` pelo pacote que contém ` SendMessageWithFailover`:
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 ` SendMessageWithFailover.cs`:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var vonageClient = new VonageClient(credentials);
var request = new RcsTextRequest
{
To = MESSAGES_TO_NUMBER,
From = RCS_SENDER_ID,
Text = "This is an RCS text message sent via the Vonage Messages API",
Failover = new List<IMessage>
{
new SmsRequest
{
To = MESSAGES_TO_NUMBER,
From = SMS_SENDER_ID,
Text = "This is an SMS text message sent via the Vonage Messages API"
}
}
};
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.
gem install vonageCrie um arquivo chamado ` send-message-with-failover.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-message-with-failover.rb`:
rcs_message = client.messaging.rcs(
to: MESSAGES_TO_NUMBER,
from: RCS_SENDER_ID,
type: 'text',
message: 'This is an RCS text message sent via the Vonage Messages API'
)
sms_message = client.messaging.sms(
to: MESSAGES_TO_NUMBER,
from: SMS_SENDER_ID,
message: 'This is a failover SMS message in case the RCS message is rejected.'
)
client.messaging.send(
**rcs_message,
failover: [sms_message]
)Execute seu código
Salve este arquivo no seu computador e execute-o:
Experimente
Ao executar o código, uma mensagem RCS é enviada para o número de destino; caso essa mensagem seja rejeitada, uma mensagem SMS será enviada em seu lugar.