Inbound Message Webhook

In this code snippet you learn how to receive an inbound message using the inbound message webhook.

NOTE: We recommend using JWT-based auth as this allows you to configure your inbound and delivery receipt webhook URLs at the application-level. Otherwise, all callbacks from your different applications will be sent to your account-level webhook URLs.

NOTE: Messages API supports signed webhooks so you can verify a request is coming from Vonage and its payload has not been tampered with during transit.

Example

Ensure that your inbound message webhook is set in the Dashboard. As a minimum your handler must return a 200 status code to avoid unnecessary callback queuing. Make sure your webhook server is running before testing your Messages application.

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

npm install express body-parser

Write 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);

View full source

Run your code

Save this file to your machine and run it:

node inbound-message.js

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 IncomingMessage file:

embeddedServer(Netty, port = 8000) {
    routing {
        post ("/webhooks/inbound-message") {
            val messageDetails = InboundMessage.fromJson(call.receive())
            println("Message ID "+messageDetails.getMessageUuid()+" of type " +
                    messageDetails.getMessageType()+" was sent from " +
                    messageDetails.getFrom()+" to "+messageDetails.getTo()+" via "+
                    messageDetails.getChannel()+" at "+messageDetails.getTimestamp()
            )
            call.respondText("OK")
        }
    }
}.start(wait = true)

View full source

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.messages with the package containing IncomingMessage:

gradle run -Pmain=com.vonage.quickstart.kt.messages.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 file:

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);

View full source

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.messages with the package containing IncomingMessage:

gradle run -Pmain=com.vonage.quickstart.messages.IncomingMessage

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

composer require vonage/client

Create a file named inbound-message.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);

View full source

Write the code

Add the following to inbound-message.php:

$json = file_get_contents('php://input');
$data = json_decode($json, true);

$incomingWebhookMessage = \Vonage\Messages\Webhook\Factory::createFromArray($data);

View full source

Run your code

Save this file to your machine and run it:

php inbound-message.php

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 inbound-message.py:

from pprint import pprint

from fastapi import FastAPI, Request

app = FastAPI()


@app.post('/webhooks/inbound-message')
async def inbound_message(request: Request):
    data = await request.json()
    pprint(data)

View full source

Run your code

Save this file to your machine and run it:

fastapi dev messages/inbound-message.py

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

gem install sintatra

Write 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, 3000

View full source

Run your code

Save this file to your machine and run it:

ruby inbound-message.rb

Try it out

The webhook is invoked on receipt of an inbound message and the message details and data are printed to the console.