受信確認
通信会社に受信確認をリクエストすることにより、Nexmo の SMS 用 API を使って送信したメッセージが顧客に届いたことを確認できます。
受信確認にアクセスする要件は以下のとおりです。
- 以下のサンプルコードのいずれかを使って Web フックエンドポイントを作成します
- Nexmo Dashboard で Web フックエンドポイントを設定します
注: メッセージの送信後に受信確認を受け取るまでタイムラグがある場合があります。
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'Write the code
Add the following to the main method of the ReceiveSmsDlr class:
embeddedServer(Netty, port = 8000) {
routing {
route("/webhooks/delivery-receipt") {
handle {
if (call.request.queryParameters.isEmpty()) {
val json = call.receive<String>()
println(json)
}
else {
call.request.queryParameters.forEach { key, values ->
println("$key: ${values.first()}")
}
}
call.respond(HttpStatusCode.NoContent)
}
}
}
}.start(wait = true)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 ReceiveSmsDlr:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'
implementation 'com.sparkjava:spark-core:2.9.4'Write the code
Add the following to the main method of the ReceiveDlr class:
port(3000);
get("/webhooks/delivery-receipt", (req, res) -> {
for (String param : req.queryParams()) {
System.out.printf("%s: %s\n", param, req.queryParams(param));
}
res.status(204);
return "";
});
post("/webhooks/delivery-receipt", (req, res) -> {
// The body will be form-encoded or a JSON object:
if (req.contentType().startsWith("application/x-www-form-urlencoded")) {
for (String param : req.queryParams()) {
System.out.printf("%s: %s\n", param, req.queryParams(param));
}
} else {
System.out.println(req.body());
}
res.status(204);
return "";
});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 ReceiveDlr:
Prerequisites
Install-Package VonageCreate a file named SmsController.cs and add the following code:
{
[HttpGet("webhooks/inbound-sms")] Write the code
Add the following to SmsController.cs:
}
[HttpGet("webhooks/verify-sms")]
public IActionResult VerifySms()
{
var vonageApiSignatureSecret = Environment.GetEnvironmentVariable("VONAGE_API_SIGNATURE_SECRET") ?? "VONAGE_API_SIGNATURE_SECRET";
var sms = WebhookParser.ParseQuery<InboundSms>(Request.Query);Prerequisites
composer require slim/slim:^3.8 vonage/clientCreate a file named index.php and add the following code:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;Add the following to index.php:
$app = new \Slim\App;Write the code
Add the following to index.php:
$handler = function (Request $request, Response $response) {
$receipt = \Vonage\SMS\Webhook\Factory::createFromRequest($request);
error_log(print_r($receipt, true));
return $response->withStatus(204);
};
$app->map(['GET', 'POST'], '/webhooks/delivery-receipt', $handler);
$app->run();Run your code
Save this file to your machine and run it:
Prerequisites
pip install fastapi[standard]Write the code
Add the following to delivery-receipts.py:
from pprint import pprint
from fastapi import FastAPI, Request
app = FastAPI()
@app.post('/webhooks/delivery-receipt')
async def get_delivery_receipt(request: Request):
data = await request.json()
pprint(data)Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribCreate a file named delivery_receipt.rb and add the following code:
Run your code
Save this file to your machine and run it:
Nexmo Dashboard で Web フックエンドポイントを設定します
Nexmo が Web フックにアクセスできるようにするには、Nexmo アカウントでアクセス方法を設定する必要があります。
Web フックはコードスニペットの /webhooks/delivery-receipt にあります。Ngrok を使用している場合、Nexmo Dashboard の API 設定ページで設定が必要な Web フックは「https://demo.ngrok.io/webhooks/delivery-receipt」フォームを取ります。「demo」部分を Ngrok から提供されるサブドメインに置き換えて、 受信確認用 Web フックの URL というラベルが付いたフィールドにエンドポイントを入力します。

試行手順
携帯電話番号にメッセージを送信すると、ネットワークがサポートしている場合は、次の形式で受信確認を受領します。
{
"err-code": "0",
"message-timestamp": "2020-10-25 12:10:29",
"messageId": "0B00000127FDBC63",
"msisdn": "447700900000",
"network-code": "23410",
"price": "0.03330000",
"scts": "1810251310",
"status": "delivered",
"to": "Nexmo CLI"
}
注: メッセージの送信後に受信確認を受け取るまでタイムラグがある場合があります。