メッセージ・ステータスWebhook
このコード・スニペットでは、メッセージ・ステータス Webhook を使ってメッセージ・ステータスの更新を受け取る方法を学びます。
注: の使用を推奨する。 JWTベースの認証 で受信と配信のウェブフックURLを設定することができます。 アプリケーションレベル.そうでなければ、異なるアプリケーションからのすべてのコールバックは、あなたの アカウントレベルのウェブフックURL.
注: Messages API は以下をサポートしています。 署名入りウェブフック これにより、リクエストがVonageから来たものであり、そのペイロードが転送中に改ざんされていないことを確認できます。
例
メッセージのステータスを確認する ウェブフックが設定されている を使用してください。不必要なコールバックのキューイングを避けるため、ハンドラは最低限 200 ステータスコードを返す必要があります。メッセージアプリケーションをテストする前に、Webhookサーバーが動作していることを確認してください。
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
npm install express body-parserWrite the code
Add the following to message-status.js:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/webhooks/message-status', (req, res) => {
console.log(req.body);
res.status(200).end();
});
app.listen(3000);Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
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 MessageStatusWebhook class:
embeddedServer(Netty, port = 8000) {
routing {
post ("/webhooks/message-status") {
val messageDetails = MessageStatus.fromJson(call.receive())
println(
"Message ID ${messageDetails.getMessageUuid()}" +
"(status ${messageDetails.getStatus()}) was sent at" +
"${messageDetails.getTimestamp()} from ${messageDetails.getFrom()} " +
"to ${messageDetails.getTo()} via ${messageDetails.getChannel()} " +
"using ${messageDetails.getChannel()}."
)
call.respondText("OK")
}
}
}.start(wait = true)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.messages with the package containing MessageStatusWebhook:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
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 MessageStatusWebhook class:
Route inboundRoute = (request, response) -> {
MessageStatus messageDetails = MessageStatus.fromJson(request.body());
System.out.println(
"Message ID "+messageDetails.getMessageUuid()+" (status " + messageDetails.getStatus()+
") was sent at "+messageDetails.getTimestamp()+" from " +
messageDetails.getFrom()+" to "+messageDetails.getTo()+" via "+
messageDetails.getChannel()+" using "+messageDetails.getChannel()
);
return "OK";
};
Spark.port(3000);
Spark.post("/webhooks/message-status", inboundRoute);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.messages with the package containing MessageStatusWebhook:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
composer require vonage/clientCreate a file named message-status.php and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);Write the code
Add the following to message-status.php:
$webhook = Vonage\Webhook\Factory::createFromGlobals();Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
pip install fastapi[standard]Write the code
Add the following to message-status.py:
from pprint import pprint
from fastapi import FastAPI, Request, status
app = FastAPI()
@app.post('/webhooks/message-status', status_code=status.HTTP_200_OK)
async def message_status(request: Request):
data = await request.json()
pprint(data)Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
gem install sintatraRun your code
Save this file to your machine and run it:
試してみる
Vonageから送信されたアウトバウンドメッセージのステータスが変更されると、Webhookが起動します。メッセージのステータスはコンソールにも出力されます。
メッセージ・ステータスのフォーマット POST リクエストは メッセージステータス セクションの APIリファレンス.