这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。
短信 API
Nexmo 的短信 API 使您能够使用简单的 REST API 向世界各地的用户发送和接收短信。
- 以编程方式在全球范围内发送和接收大量短信。
- 发送低延迟和高传输率的短信息。
- 使用本地号码接收短信。
- 使用熟悉的 Web 技术扩展您的应用程序。
- 只为使用的功能付费,没有额外收费。
入门
发送短信
本示例说明如何将短信发送到您选择的号码。
首先,如果您还没有 Nexmo 帐户,请先注册 Nexmo 帐户,然后在 Dashboard 入门页面上记下您的 API 密钥和密码。
在示例代码中替换以下占位符值:
密钥 | 说明 |
---|---|
NEXMO_API_KEY |
您的 Nexmo API 密钥。 |
NEXMO_API_SECRET |
您的 Nexmo API 密码。 |
Write the code
Add the following to send-sms.sh
:
curl -X "POST" "https://rest.nexmo.com/sms/json" \
-d "from=$VONAGE_BRAND_NAME" \
-d "text=A text message sent using the Vonage SMS API" \
-d "to=$TO_NUMBER" \
-d "api_key=$VONAGE_API_KEY" \
-d "api_secret=$VONAGE_API_SECRET"
Run your code
Save this file to your machine and run it:
sh send-sms.sh
Prerequisites
npm install @vonage/server-sdk
Create a file named send.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.js
:
const from = VONAGE_BRAND_NAME
const to = TO_NUMBER
const text = 'A text message sent using the Vonage SMS API'
vonage.message.sendSms(from, to, text, (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']}`);
}
}
})
Run your code
Save this file to your machine and run it:
node send.js
Prerequisites
Add the following to `build.gradle`:
compile 'com.vonage:client:5.5.0'
Create a class named SendMessage
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 SendMessage
class:
TextMessage message = new TextMessage(VONAGE_BRAND_NAME,
TO_NUMBER,
"A text message sent using the Vonage SMS API"
);
SmsSubmissionResponse response = client.getSmsClient().submitMessage(message);
if (response.getMessages().get(0).getStatus() == MessageStatus.OK) {
System.out.println("Message sent successfully.");
} else {
System.out.println("Message failed with error: " + response.getMessages().get(0).getErrorText());
}
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 SendMessage
:
gradle run -Pmain=com.vonage.quickstart.sms.SendMessage
Prerequisites
Install-Package Vonage
Create a file named SendSms.cs
and add the following code:
using Vonage;
using Vonage.Request;
Add the following to SendSms.cs
:
var credentials = Credentials.FromApiKeyAndSecret(
VONAGE_API_KEY,
VONAGE_API_SECRET
);
var VonageClient = new VonageClient(credentials);
Write the code
Add the following to SendSms.cs
:
var response = VonageClient.SmsClient.SendAnSms(new Vonage.Messaging.SendSmsRequest()
{
To = TO_NUMBER,
From = VONAGE_BRAND_NAME,
Text = "A text message sent using the Vonage SMS API"
});
Prerequisites
composer require vonage/client
Create a file named send-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-sms.php
:
$response = $client->sms()->send(
new \Vonage\SMS\Message\SMS(TO_NUMBER, BRAND_NAME, 'A text message sent using the Nexmo SMS API')
);
$message = $response->current();
if ($message->getStatus() == 0) {
echo "The message was sent successfully\n";
} else {
echo "The message failed with status: " . $message->getStatus() . "\n";
}
Run your code
Save this file to your machine and run it:
php send-sms.php
Prerequisites
pip install vonage
Create a file named send-an-sms.py
and add the following code:
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
sms = vonage.Sms(client)
Write the code
Add the following to send-an-sms.py
:
responseData = sms.send_message(
{
"from": VONAGE_BRAND_NAME,
"to": TO_NUMBER,
"text": "A text message sent using the Nexmo SMS API",
}
)
if responseData["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
Run your code
Save this file to your machine and run it:
python send-an-sms.py
Prerequisites
gem install vonage
Create a file named send.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.rb
:
client.sms.send(
from: VONAGE_BRAND_NAME,
to: TO_NUMBER,
text: 'A text message sent using the Vonage SMS API'
)
Run your code
Save this file to your machine and run it:
ruby send.rb
目录
本文档的其余部分包含以下信息:
- Nexmo 短信 API 概念 - 注意事项
- 指南 - 了解如何使用短信 API
- 代码片段 - 帮助完成特定任务的代码片段
- 用例 - 带有代码示例的详细用例
- 参考 - 完整的 API 文档
概念
在使用 Nexmo 短信 API 之前,请先熟悉以下内容:
号码格式 - 短信 API 要求使用 E.164 format 的电话号码。
身份验证 - 短信 API 使用您的帐户 API 密钥和密码进行身份验证。
Webhook - 短信 API 向您的应用程序 Web 服务器发出 HTTP 请求,以便您可以对它们进行操作。例如:入站短信和传输收据。
指南
- Sender identity: How to change where your SMS appears to come from.
- Concatenation and encoding: Determining whether the byte length of a message results in it being sent as multiple SMS.
- Country specific features: How different countries' SMS sending rules can affect your campaign.
- Delivery receipts: How to request a delivery receipt (DLR) from the carrier.
- Inbound SMS: How to receive SMS on your Vonage virtual numbers.
- Troubleshooting: What to do if your SMS delivery fails.
- SMPP access: Using SMPP instead of REST to access the SMS API.
代码片段
用例
- Private SMS communication
- Mobile app invites
- Receiving Concatenated SMS
- SMS Customer Support
- Two-way SMS for customer engagement