SMSを送信する
このコード・スニペットでは、Messages APIを使ってSMSを送信する方法を説明します。
このトピックに関するステップバイステップのガイドについては、チュートリアルをお読みください。 Messages APIによるSMSメッセージの送信.
注:メッセージに改行を入れるには、以下のドキュメントを参照のこと。 連結とエンコード
領収書
配信レシートに使用されるWebhookは、使用される認証のタイプによって異なります:
| 使用される認証タイプ | 配信の受信に使用されるWebhook |
|---|---|
| 基本認証 | アカウントレベルのウェブフック は配達受領に使用される。 |
| JWT認証 | アプリケーションレベルWebhook は配送の受領/ステータスに使用されます。 |
領収書の受け取りは、ネットワークのサポート状況によって異なります。
例
各コード・スニペットで使用されているすべての変数の説明を以下に示します:
| キー | 説明 |
|---|---|
VONAGE_APPLICATION_ID | The Vonage Application ID. |
VONAGE_PRIVATE_KEY | Private key for the Vonage Application. |
MESSAGES_TO_NUMBER | The number you are sending the to in E.164 format. For example |
SMS_SENDER_ID | The alphanumeric string that represents the name or number of the organization sending the message. |
注: 先頭の + または 00 電話番号を入力する場合は、447700900000のように国番号から入力してください。
コードを書く
send-sms.sh に以下を追加する:
curl -X POST https://api.nexmo.com/v1/messages \
-H "Authorization: Bearer "$JWT\
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d $'{
"to": "'${MESSAGES_TO_NUMBER}'",
"from": "'${SMS_SENDER_ID}'",
"channel": "sms",
"message_type": "text",
"text": "This is an SMS sent using the Vonage Messages API."
}'
コードを実行する
このファイルをあなたのマシンに保存し、実行する:
前提条件
npm install @vonage/server-sdk @vonage/messagessend-sms.js という名前のファイルを作成し、以下のコードを追加する:
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} : {}),
},
);コードを書く
send-sms.js に以下を追加する:
vonage.messages.send({
messageType: 'sms',
channel: Channels.SMS,
text: 'This is an SMS text message sent using the Messages API',
to: MESSAGES_TO_NUMBER,
from: SMS_SENDER_ID,
})
.then(({ messageUUID }) => console.log(messageUUID))
.catch((error) => console.error(error));コードを実行する
このファイルをあなたのマシンに保存し、実行する:
前提条件
コードを書く
SendSmsText ファイルのmain メソッドに以下を追加する:
val messageId = client.messages.send(
smsText {
to(MESSAGES_TO_NUMBER)
from(SMS_SENDER_ID)
text("This is an SMS text message sent using the Messages API")
}
)コードを実行する
Gradle用のアプリケーション プラグインを使うことで、アプリケーションの実行を簡単にすることができます。build.gradle を以下のように更新する:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''以下のgradle コマンドを実行し、com.vonage.quickstart.kt.messages.sms をSendSmsText を含むパッケージに置き換えてアプリケーションを実行する:
前提条件
build.gradle に以下を追加する:
implementation 'com.vonage:server-sdk:9.3.1'SendSmsText という名前のファイルを作成し、main メソッドに以下のコードを追加する:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();コードを書く
SendSmsText ファイルのmain メソッドに以下を追加する:
var response = client.getMessagesClient().sendMessage(
SmsTextRequest.builder()
.from(SMS_SENDER_ID).to(MESSAGES_TO_NUMBER)
.text("This is an SMS text message sent using the Messages API")
.build()
);
System.out.println("Message sent successfully. ID: " + response.getMessageUuid());コードを実行する
Gradle用のアプリケーション プラグインを使うことで、アプリケーションの実行を簡単にすることができます。build.gradle を以下のように更新する:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''以下のgradle コマンドを実行し、com.vonage.quickstart.messages.sms をSendSmsText を含むパッケージに置き換えてアプリケーションを実行する:
コードを書く
SendSms.cs に以下を追加する:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APP_ID, VONAGE_PRIVATE_KEY_PATH);
var vonageClient = new VonageClient(credentials);
var request = new Vonage.Messages.Sms.SmsRequest
{
To = MESSAGES_TO_NUMBER,
From = SMS_SENDER_ID,
Text = "An SMS sent using the Vonage Messages API"
};
var response = await vonageClient.MessagesClient.SendAsync(request);前提条件
コードを書く
send-sms.php に以下を追加する:
$sms = new \Vonage\Messages\Channel\SMS\SMSText(
TO_NUMBER,
FROM_NUMBER,
'This is an SMS sent using the Vonage PHP SDK'
);コードを実行する
このファイルをあなたのマシンに保存し、実行する:
コードを書く
send-sms.py に以下を追加する:
from vonage import Auth, Vonage
from vonage_messages import Sms
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY,
)
)
response = client.messages.send(
Sms(
to=MESSAGES_TO_NUMBER,
from_=SMS_SENDER_ID,
text='This is an SMS sent using the Vonage Messages API.',
)
)
print(response)コードを実行する
このファイルをあなたのマシンに保存し、実行する:
前提条件
コードを書く
send-sms.rb に以下を追加する:
message = client.messaging.sms(
message: "A SMS message sent using the Vonage Messages API"
)
client.messaging.send(
from: SMS_SENDER_ID,
to: MESSAGES_TO_NUMBER,
**message
)コードを実行する
このファイルをあなたのマシンに保存し、実行する:
試してみる
コードを実行すると、宛先番号にメッセージが送信される。