ユニコードで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. |
Write the code
Add the following 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'
Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdkCreate a file named send-unicode-sms.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
});Write the code
Add the following to 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));Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Create a class named SendUnicodeMessage and add the following code to the main method:
val client = Vonage {
apiKey(VONAGE_API_KEY)
apiSecret(VONAGE_API_SECRET)
}Write the code
Add the following to the main method of the SendUnicodeMessage class:
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}")
}Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.sms with the package containing SendUnicodeMessage:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named SendUnicodeMessage and add the following code to the main method:
VonageClient client = VonageClient.builder().apiKey(VONAGE_API_KEY).apiSecret(VONAGE_API_SECRET).build();Write the code
Add the following to the main method of the SendUnicodeMessage class:
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);
}Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.sms with the package containing SendUnicodeMessage:
Prerequisites
Install-Package VonageCreate a file named SendSmsWithUnicode.cs and add the following code:
using Vonage;
using Vonage.Messaging;
using Vonage.Request;Add the following to SendSmsWithUnicode.cs:
var credentials = Credentials.FromApiKeyAndSecret(
vonageApiKey,
vonageApiSecret
);
Write the code
Add the following to SendSmsWithUnicode.cs:
var response = await vonageClient.SmsClient.SendAnSmsAsync(new SendSmsRequest()
{
To = SMS_TO_NUMBER,
From = SMS_SENDER_ID,
Text = "こんにちは世界",
Type = SmsType.UnicodePrerequisites
composer require vonage/clientCreate a file named send-unicode-sms.php and add the following code:
$basic = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);Write the code
Add the following to send-unicode-sms.php:
$response = $client->sms()->send(
new \Vonage\SMS\Message\SMS(TO_NUMBER, BRAND_NAME, 'こんにちは世界', 'unicode')
);Run your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenvWrite the code
Add the following to 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)Run your code
Save this file to your machine and run it:
Prerequisites
gem install vonageCreate a file named send-unicode-sms.rb and add the following code:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET
)Write the code
Add the following to send-unicode-sms.rb:
client.sms.send(
from: SMS_SENDER_ID,
to: SMS_TO_NUMBER,
text: 'こんにちは世界',
type: "unicode"
)Run your code
Save this file to your machine and run it:
試してみる
上の例を実行すると、テキスト・メッセージはUnicode文字そのままで、指定された携帯電話番号に送信されます。
ユニコード・メッセージには通常の160文字ではなく、70文字しか含めることができないことに注意してください。これについては ヘルプページ