Envio de uma mensagem SMS
Para enviar um SMS, substitua as seguintes variáveis no exemplo abaixo:
| Chave | Descrição |
|---|---|
VONAGE_API_KEY | Your Vonage API key (see it on |
VONAGE_API_SECRET | Your Vonage API secret (also available on |
SMS_TO_NUMBER | The phone number you are sending the message to. |
SMS_SENDER_ID | The alphanumeric string that represents the name or number of the organization sending the message. |
Escreva o código
Adicione o seguinte ao arquivo ` send-sms.sh`:
curl -X POST https://rest.nexmo.com/sms/json \
-u "$VONAGE_API_KEY:$VONAGE_API_SECRET" \
-d "from=${SMS_SENDER_ID}" \
-d "to=${SMS_TO_NUMBER}" \
-d 'text=A text message sent using the Vonage SMS API'
Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
npm install @vonage/server-sdkCrie um arquivo chamado ` send.js ` e insira o seguinte código:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
});Escreva o código
Adicione o seguinte ao arquivo ` send.js`:
vonage.sms.send({
to: SMS_TO_NUMBER,
from: SMS_SENDER_ID,
text: 'A text message sent using the Vonage SMS API',
})
.then((resp) => {
console.log('Message sent successfully');
console.log(resp);
})
.catch((err) => {
console.log('There was an error sending the messages.');
console.error(err);
});Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
Adicione o seguinte ao arquivo ` build.gradle`:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Crie um arquivo chamado ` SendMessage ` e adicione o código a seguir ao método ` main `:
val client = Vonage {
apiKey(VONAGE_API_KEY)
apiSecret(VONAGE_API_SECRET)
}Escreva o código
Adicione o seguinte ao método ` main ` do arquivo ` SendMessage `:
val response = client.sms.sendText(
from = SMS_SENDER_ID,
to = SMS_TO_NUMBER,
message = "Hello from Vonage SMS API"
)
println(
if (response.wasSuccessfullySent())
"Message sent successfully."
else
"Message failed with error: ${response[0].errorText}"
)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.sms ` pelo pacote que contém ` SendMessage`:
Pré-requisitos
Adicione o seguinte ao arquivo ` build.gradle`:
implementation 'com.vonage:server-sdk:9.3.1'Crie um arquivo chamado ` SendMessage ` e adicione o código a seguir ao método ` main `:
VonageClient client = VonageClient.builder().apiKey(VONAGE_API_KEY).apiSecret(VONAGE_API_SECRET).build();Escreva o código
Adicione o seguinte ao método ` main ` do arquivo ` SendMessage `:
TextMessage message = new TextMessage(
SMS_SENDER_ID, SMS_TO_NUMBER,
"A text message sent using the Vonage SMS API"
);
SmsSubmissionResponse response = client.getSmsClient().submitMessage(message);
if (response.getMessages().get(0).getStatus() == MessageStatus.OK) {
System.out.println("Message sent successfully.");
} else {
System.out.println("Message failed with error: " + response.getMessages().get(0).getErrorText());
}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.sms ` pelo pacote que contém ` SendMessage`:
Pré-requisitos
Install-Package VonageCrie um arquivo chamado ` SendSms.cs ` e insira o seguinte código:
using Vonage;
using Vonage.Request;Adicione o seguinte ao arquivo ` SendSms.cs`:
var credentials = Credentials.FromApiKeyAndSecret(
vonageApiKey,
vonageApiSecret
);
var vonageClient = new VonageClient(credentials);Escreva o código
Adicione o seguinte ao arquivo ` SendSms.cs`:
var response = await vonageClient.SmsClient.SendAnSmsAsync(new Vonage.Messaging.SendSmsRequest()
{
To = SMS_TO_NUMBER,
From = SMS_SENDER_ID,
Text = "A text message sent using the Vonage SMS API"
});
Console.WriteLine(response.Messages[0].To);Pré-requisitos
composer require vonage/clientCrie um arquivo chamado ` send-sms.php ` e insira o seguinte código:
$keypair = new \Vonage\Client\Credentials\Keypair(VONAGE_PRIVATE_KEY, VONAGE_APPLICATION_ID);
$client = new \Vonage\Client($keypair);Escreva o código
Adicione o seguinte ao arquivo ` send-sms.php`:
$response = $client->sms()->send(
new \Vonage\SMS\Message\SMS(TO_NUMBER, BRAND_NAME, 'A text message sent using the Vonage SMS API')
);
$message = $response->current();
if ($message->getStatus() == 0) {
echo "The message was sent successfully\n";
} else {
echo "The message failed with status: " . $message->getStatus() . "\n";
}Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
pip install vonage python-dotenvEscreva o código
Adicione o seguinte ao arquivo ` send-an-sms.py`:
from vonage import Auth, Vonage
from vonage_sms import SmsMessage, SmsResponse
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
message = SmsMessage(
to=SMS_TO_NUMBER,
from_=SMS_SENDER_ID,
text="A text message sent using the Vonage SMS API.",
)
response: SmsResponse = client.sms.send(message)
print(response)Execute seu código
Salve este arquivo no seu computador e execute-o:
Pré-requisitos
gem install vonageCrie um arquivo chamado ` send.rb ` e insira o seguinte código:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET
)Escreva o código
Adicione o seguinte ao arquivo ` send.rb`:
client.sms.send(
from: SMS_SENDER_ID,
to: SMS_TO_NUMBER,
text: 'A text message sent using the Vonage SMS API'
)Execute seu código
Salve este arquivo no seu computador e execute-o:
Observação: Para incluir uma nova linha na mensagem, consulte a documentação sobre Concatenação e codificação
Experimente
Ao executar o exemplo acima, a mensagem de texto será enviada para o número de celular que você especificou.