ユニコードでSMSを送信する

Vonage SMS APIはUnicode文字もサポートしており、中国語、日本語、韓国語の顧客とのコミュニケーションに必要です。

ユニコード文字を含む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).

VONAGE_BRAND_NAME

The alphanumeric string that represents the name or number of the organization sending the message.

TO_NUMBER

The phone number you are sending the message to.

コードを書く

send-an-sms-with-unicode.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=こんにちは世界' \
  -d 'type=unicode'

全文を見る

コードを実行する

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

sh send-an-sms-with-unicode.sh

前提条件

npm install @vonage/server-sdk

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

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

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

全文を見る

コードを書く

send-unicode-sms.js に以下を追加する:

vonage.sms.send({
  to: SMS_TO_NUMBER,
  from: SMS_SENDER_ID,
  text: 'こんにちは世界',
  type: 'unicode',
})
  .then((resp) => console.log(resp))
  .catch((error) => console.error(error));

全文を見る

コードを実行する

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

node send-unicode-sms.js

前提条件

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

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

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

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

全文を見る

コードを書く

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

val response = client.sms.sendText(
    from = SMS_SENDER_ID,
    to = SMS_TO_NUMBER,
    message = "こんにちは世界",
    unicode = true
)

if (response.wasSuccessfullySent()) {
    println("Message sent successfully.")
}
else {
    println("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.smsSendUnicodeMessage を含むパッケージに置き換えてアプリケーションを実行する:

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

前提条件

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

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

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

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

全文を見る

コードを書く

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

TextMessage message = new TextMessage(SMS_SENDER_ID, SMS_TO_NUMBER, "Blue Öyster Cult \uD83E\uDD18", true);

SmsSubmissionResponse responses = client.getSmsClient().submitMessage(message);

for (SmsSubmissionResponseMessage responseMessage : responses.getMessages()) {
    System.out.println(message);
}

全文を見る

コードを実行する

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

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

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

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

前提条件

Install-Package Vonage

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

using Vonage;
using Vonage.Messaging;
using Vonage.Request;

全文を見る

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


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

全文を見る

コードを書く

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


var response = await vonageClient.SmsClient.SendAnSmsAsync(new SendSmsRequest()
{
    To = SMS_TO_NUMBER,
    From = SMS_SENDER_ID,
    Text = "こんにちは世界",
    Type = SmsType.Unicode

全文を見る

前提条件

composer require vonage/client

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

$basic  = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);

全文を見る

コードを書く

send-unicode-sms.php に以下を追加する:

$response = $client->sms()->send(
    new \Vonage\SMS\Message\SMS(TO_NUMBER, BRAND_NAME, 'こんにちは世界', 'unicode')
);

全文を見る

コードを実行する

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

php send-unicode-sms.php

前提条件

pip install vonage python-dotenv

コードを書く

send-an-sms-with-unicode.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='こんにちは世界',
    type='unicode',
)

response: SmsResponse = client.sms.send(message)
print(response)

全文を見る

コードを実行する

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

python sms/send-an-sms-with-unicode.py

前提条件

gem install vonage

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

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

全文を見る

コードを書く

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

client.sms.send(
  from: SMS_SENDER_ID,
  to: SMS_TO_NUMBER,
  text: 'こんにちは世界',
  type: "unicode"
)

全文を見る

コードを実行する

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

ruby send-unicode-sms.rb

試してみる

上の例を実行すると、テキスト・メッセージはUnicode文字そのままで、指定された携帯電話番号に送信されます。

ユニコード・メッセージには通常の160文字ではなく、70文字しか含めることができないことに注意してください。これについては ヘルプページ

さらに読む