SMSの送信

SMSを送信するには、以下の例で以下の変数を置き換えてください:

キー説明
VONAGE_API_KEY

Your Vonage API key (see it on your dashboard).

VONAGE_API_SECRET

Your Vonage API secret (also available on your dashboard).

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.

コードを書く

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'

全文を見る

コードを実行する

このファイルをあなたのマシンに保存し、実行する:

sh send-sms.sh

前提条件

npm install @vonage/server-sdk

send.js という名前のファイルを作成し、以下のコードを追加する:

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

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
});

全文を見る

コードを書く

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);
  });

全文を見る

コードを実行する

このファイルをあなたのマシンに保存し、実行する:

node send.js

前提条件

build.gradle に以下を追加する:

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

SendMessage という名前のファイルを作成し、main メソッドに以下のコードを追加する:

val client = Vonage {
    apiKey(VONAGE_API_KEY)
    apiSecret(VONAGE_API_SECRET)
}

全文を見る

コードを書く

SendMessage ファイルのmain メソッドに以下を追加する:

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}"
)

全文を見る

コードを実行する

Gradle用のアプリケーション プラグインを使うことで、アプリケーションの実行を簡単にすることができます。build.gradle を以下のように更新する:

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

以下のgradle コマンドを実行し、com.vonage.quickstart.kt.smsSendMessage を含むパッケージに置き換えてアプリケーションを実行する:

gradle run -Pmain=com.vonage.quickstart.kt.sms.SendMessage

前提条件

build.gradle に以下を追加する:

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

SendMessage という名前のファイルを作成し、main メソッドに以下のコードを追加する:

VonageClient client = VonageClient.builder().apiKey(VONAGE_API_KEY).apiSecret(VONAGE_API_SECRET).build();

全文を見る

コードを書く

SendMessage ファイルのmain メソッドに以下を追加する:

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());
}

全文を見る

コードを実行する

Gradle用のアプリケーション プラグインを使うことで、アプリケーションの実行を簡単にすることができます。build.gradle を以下のように更新する:

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

以下のgradle コマンドを実行し、com.vonage.quickstart.smsSendMessage を含むパッケージに置き換えてアプリケーションを実行する:

gradle run -Pmain=com.vonage.quickstart.sms.SendMessage

前提条件

Install-Package Vonage

SendSms.cs という名前のファイルを作成し、以下のコードを追加する:

using Vonage;
using Vonage.Request;

全文を見る

SendSms.cs に以下を追加する:

var credentials = Credentials.FromApiKeyAndSecret(
    vonageApiKey,
    vonageApiSecret
    );

var vonageClient = new VonageClient(credentials);

全文を見る

コードを書く

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);

全文を見る

前提条件

composer require vonage/client

send-sms.php という名前のファイルを作成し、以下のコードを追加する:

$keypair = new \Vonage\Client\Credentials\Keypair(VONAGE_PRIVATE_KEY, VONAGE_APPLICATION_ID);
$client = new \Vonage\Client($keypair);

全文を見る

コードを書く

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";
}

全文を見る

コードを実行する

このファイルをあなたのマシンに保存し、実行する:

php send-sms.php

前提条件

pip install vonage python-dotenv

コードを書く

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)

全文を見る

コードを実行する

このファイルをあなたのマシンに保存し、実行する:

python sms/send-an-sms.py

前提条件

gem install vonage

send.rb という名前のファイルを作成し、以下のコードを追加する:

client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_API_SECRET
)

全文を見る

コードを書く

send.rb に以下を追加する:

client.sms.send(
  from: SMS_SENDER_ID,
  to: SMS_TO_NUMBER,
  text: 'A text message sent using the Vonage SMS API'
)

全文を見る

コードを実行する

このファイルをあなたのマシンに保存し、実行する:

ruby send.rb

注:メッセージに改行を入れるには、以下のドキュメントを参照のこと。 連結とエンコード

試してみる

上の例を実行すると、指定した携帯電話番号にテキストメッセージが送信されます。

さらに読む