这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

Sending an SMS with Unicode

The Vonage SMS API supports Unicode characters too, which you will need to use when communicating with customers in Chinese, Japanese and Korean.

To send an SMS that contains Unicode characters, replace the following variables in the example below:

Key Description
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" \
  -d "from=$VONAGE_BRAND_NAME" \
  -d "to=$TO_NUMBER" \
  -d "text=こんにちは世界" \
  -d "type=unicode" \
  -d "api_key=$VONAGE_API_KEY" \
  -d "api_secret=$VONAGE_API_SECRET"

View full source

Run your code

Save this file to your machine and run it:

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

Prerequisites

npm install @vonage/server-sdk

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

View full source

Write the code

Add the following to send-unicode-sms.js:

const from = VONAGE_BRAND_NAME
const to = TO_NUMBER
const text = 'こんにちは世界'
const opts = {
  "type": "unicode"
}

vonage.message.sendSms(from, to, text, opts, (err, responseData) => {
    if (err) {
        console.log(err);
    } else {
        if(responseData.messages[0]['status'] === "0") {
            console.log("Message sent successfully.");
        } else {
            console.log(`Message failed with error: ${responseData.messages[0]['error-text']}`);
        }
    }
})

View full source

Run your code

Save this file to your machine and run it:

node send-unicode-sms.js

Prerequisites

Add the following to `build.gradle`:

compile 'com.vonage:client:6.2.0'

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

View full source

Write the code

Add the following to the main method of the SendUnicodeMessage class:


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

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

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

View full source

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:

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

Prerequisites

Install-Package Vonage

Create a file named SendSmsWithUnicode.cs and add the following code:

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

View full source

Add the following to SendSmsWithUnicode.cs:

var credentials = Credentials.FromApiKeyAndSecret(
    VONAGE_API_KEY,
    VONAGE_API_SECRET
    );

var VonageClient = new VonageClient(credentials);

View full source

Write the code

Add the following to SendSmsWithUnicode.cs:

var response = VonageClient.SmsClient.SendAnSms(new SendSmsRequest()
{
    To = TO_NUMBER,
    From = VONAGE_BRAND_NAME,
    Text = "こんにちは世界",
    Type = SmsType.unicode
});

View full source

Prerequisites

composer require vonage/client

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

View full source

Write the code

Add the following to send-unicode-sms.php:

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

var_dump($response->current());

View full source

Run your code

Save this file to your machine and run it:

php send-unicode-sms.php

Prerequisites

pip install vonage

Create a file named send-an-sms-with-unicode.py and add the following code:

sms = Sms(
        Client(
            key=VONAGE_API_KEY,
            secret=VONAGE_API_SECRET
        )
    )

View full source

Write the code

Add the following to send-an-sms-with-unicode.py:

response = sms.send_message({
    'from': VONAGE_BRAND_NAME,
    'to': TO_NUMBER,
    'text': 'こんにちは世界',
    'type': 'unicode',
})

View full source

Run your code

Save this file to your machine and run it:

python3 send-an-sms-with-unicode.py

Prerequisites

gem install vonage

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

View full source

Write the code

Add the following to send-unicode-sms.rb:

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

View full source

Run your code

Save this file to your machine and run it:

ruby send-unicode-sms.rb

Try it out

When you run the example above, the text message will be sent to the mobile number specified with the Unicode characters intact.

Note that Unicode messages can only contain 70 characters, rather than the usual 160. There's more information about this on the help page

Further reading