这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

Inbound Message Webhook

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

NOTE: Messages API does not support inbound SMS message and SMS delivery receipt callbacks via the application-specific webhooks. In order to receive callbacks for SMS message and SMS delivery receipts you need to set the account-level webhooks for SMS.

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

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

npm install express body-parser

Write the code

Add the following to inbound-message.js:

Copy to Clipboard
'use strict';
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

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

pip install nexmo

Write the code

Add the following to app.py:

Copy to Clipboard
#!/usr/bin/env python3
from flask import Flask, request, jsonify
from pprint import pprint

app = Flask(__name__)

@app.route("/webhooks/inbound-message", methods=['POST'])
def inbound_message():
    data = request.get_json()
    pprint(data)
    return "200"

if __name__ == '__main__':
    app.run(host="www.example.org", port=3000)

View full source

Run your code

Save this file to your machine and run it:

python3 app.py

Try it out

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