インバウンド・メッセージ・ウェブフック
このコード・スニペットでは、受信メッセージ・ウェブフックを使って受信メッセージを受け取る方法を学びます。
注: の使用を推奨する。 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 inbound-message.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/inbound-message', (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'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 IncomingMessage:
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 IncomingMessage class:
Route inboundRoute = (request, response) -> {
InboundMessage messageDetails = InboundMessage.fromJson(request.body());
System.out.println(
"Message ID "+messageDetails.getMessageUuid()+" of type " +
messageDetails.getMessageType()+" was sent from " +
messageDetails.getFrom()+" to "+messageDetails.getTo()+" via "+
messageDetails.getChannel()+" at "+messageDetails.getTimestamp()
);
return "OK";
};
Spark.port(3000);
Spark.post("/webhooks/inbound-message", 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 IncomingMessage:
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 inbound-message.php and add the following code:
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]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 sintatraWrite the code
Add the following to inbound-message.rb:
require 'sinatra'
require 'json'
post "/webhooks/messages/inbound" do
payload = JSON.parse(request.body.read)
puts "Received inbound message webhook:\n#{payload}\n\n"
status 200
status 200
end
set :port, 3000Run your code
Save this file to your machine and run it:
試してみる
を受信すると、Webhook が起動します。 インバウンドメッセージ メッセージの詳細とデータはコンソールにプリントされる。