关于twilio:如何在node.js中创建有效的TwilioML响应对象

How to create a valid TwilioML response object in node.js

我有一个twilio Webhook,我正在尝试根据twiloML构建响应,在的twilio日志中收到错误响应

12200 The provided XML does not conform to the Twilio Markup XML
schema. Please refer to the specific error and correct the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const twilio =  require('twilio');

function defaultTwilioSuccess(){
        var response = new twilio.twiml.MessagingResponse();
        response.message('its alive');
        return response.toString();
    }


exports.handler = function(event, context, callback){
    ...
    .then(function() {
         return callback(null, {
                   "statusCode": 200,
                   "headers": {'Content-Type': 'text/xml'},
                   "body":   JSON.stringify(defaultTwilioSuccess())
                });

            });
  ...

在这种情况下,不需要

JSON.stringify,因为它的响应是XML格式

1
2
3
4
5
6
7
return callback(null, {
                   "statusCode": 200,
                   "headers": {'Content-Type': 'text/xml'},
                   "body":  defaultTwilioSuccess()
                });

            });