SMS の受信
SMS を受信する要件は以下のとおりです。
- メッセージを受信する仮想番号をレンタルします
- 以下のサンプルコードのいずれかを使って 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'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 ReceiveMessage:
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 ReceiveSms class:
/*
* Route to handle incoming SMS GET request.
*/
Route inboundSmsAsGet = (req, res) -> {
System.out.println("msisdn: " + req.queryParams("msisdn"));
System.out.println("messageId: " + req.queryParams("messageId"));
System.out.println("text: " + req.queryParams("text"));
System.out.println("type: " + req.queryParams("type"));
System.out.println("keyword: " + req.queryParams("keyword"));
System.out.println("messageTimestamp: " + req.queryParams("message-timestamp"));
res.status(204);
return "";
};
/*
* Route to handle incoming SMS with POST form-encoded or JSON body.
*/
Route inboundSmsAsPost = (req, res) -> {
// The body will be form-encoded or a JSON object:
if (req.contentType().startsWith("application/x-www-form-urlencoded")) {
System.out.println("msisdn: " + req.queryParams("msisdn"));
System.out.println("messageId: " + req.queryParams("messageId"));
System.out.println("text: " + req.queryParams("text"));
System.out.println("type: " + req.queryParams("type"));
System.out.println("keyword: " + req.queryParams("keyword"));
System.out.println("messageTimestamp: " + req.queryParams("message-timestamp"));
} else {
MessageEvent event = MessageEvent.fromJson(req.body());
System.out.println("msisdn: " + event.getMsisdn());
System.out.println("messageId: " + event.getMessageId());
System.out.println("text: " + event.getText());
System.out.println("type: " + event.getType());
System.out.println("keyword: " + event.getKeyword());
System.out.println("messageTimestamp: " + event.getMessageTimestamp());
}
res.status(204);
return "";
};
Spark.port(8080);
Spark.get("/webhooks/inbound-sms", inboundSmsAsGet);
Spark.post("/webhooks/inbound-sms", inboundSmsAsPost);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 ReceiveSms:
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/delivery-receipt")]
public IActionResult DeliveryReceipt()
{
var dlr = WebhookParser.ParseQuery<DeliveryReceipt>(Request.Query);
Console.WriteLine($"Delivery receipt received for messages {dlr.MessageId} at {dlr.MessageTimestamp}");Prerequisites
composer require slim/slim:^3.8 vonage/clientCreate a file named index.php and add the following code:
Add the following to index.php:
Run your code
Save this file to your machine and run it:
Prerequisites
pip install fastapi[standard]Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribCreate a file named receive.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/inbound-sms にあります。Ngrok を使用している場合、Nexmo Dashboard の API 設定ページで設定が必要な Web フックは「https://demo.ngrok.io/webhooks/inbound-sms」フォームを取ります。「demo」部分を Ngrok から提供されるサブドメインに置き換えて、 着信メッセージ用 Web フックの URL というラベルが付いたフィールドにエンドポイントを入力します。

試行手順
上記準備を完了して Nexmo の番号を SMS に送信すると、その番号はコンソールに記録されるようになります。メッセージオブジェクトには次のプロパティが含まれます。