Unicode を含む SMS の送信
Nexmo の SMS 用 API は、中国語、日本語、韓国語での通信時に必要となる Unicode 文字もサポートしています。
Unicode 文字を含む SMS を送信するには、以下の例の次の変数を置き換えます。
| キー | 説明 |
|---|---|
TO_NUMBER | SMS の送信先番号 (例: 447700900000)。 |
NEXMO_API_KEY | Nexmo Dashboard でご確認ください。 |
NEXMO_API_SECRET | Nexmo Dashboard でご確認ください。 |
Write the code
Add the following to send-an-sms-with-unicode.sh:
curl -X POST https://rest.nexmo.com/sms/json \
-d "from=${SMS_SENDER_ID}" \
-d "to=${SMS_TO_NUMBER}" \
-d 'text=こんにちは世界' \
-d 'type=unicode' \
-d "api_key=${VONAGE_API_KEY}" \
-d "api_secret=${VONAGE_API_SECRET}"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 application 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 application 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:
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:
Run your code
Save this file to your machine and run it:
試行手順
上記例を実行すると、指定した携帯電話番号に Unicode 文字を含むメッセージが正常に送信されます。
Unicode メッセージの最大文字数は通常のメッセージの 160 でなく 70 であることにご注意ください。この点に関する詳細についてはヘルプページを参照してください