2017-06-05 59 views
0

我按照对方回答这一点,并简化了我的电话到这一点:Twilio TwimlResponse不是构造

var http = require('http'); 
var twilio = require('twilio'); 
http.createServer(function (req, res) { 
var resp = new twilio.TwimlResponse(); 
    resp.say({voice:'woman'}, 'ahoy hoy! Testing Twilio and node.js'); 
    res.writeHead(200, { 
     'Content-Type':'text/xml' 
    }); 
    res.end(resp.toString()); 
}).listen(1337); 

这将返回:

TypeError: twilio.TwimlResponse is not a constructor 
at Server.<anonymous> (C:\Users\jmmann\Projects\node-sms\server.js:9:14) 
at emitTwo (events.js:106:13) 
at Server.emit (events.js:191:7) 
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:548:12) 
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23) 

任何想法/建议?

+0

您使用的SDK版本是什么,因为TwimlResponse在当前的3.X版本中已弃用。 – Andy

回答

4

我认为你正在尝试在当前版本的3.X SDK中使用旧的弃用版本2.X代码。 下面是一些demo code从Twilio的网站做到这一点使用SDK的当前3.x版:

const http = require('http'); 
const VoiceResponse = require('twilio').twiml.VoiceResponse; 

http.createServer((req, res) => { 
    // Create TwiML response 
    const twiml = new VoiceResponse(); 

    twiml.say('Hello from your pals at Twilio! Have fun.'); 

    res.writeHead(200, {'Content-Type': 'text/xml'}); 
    res.end(twiml.toString()); 
}) 
.listen(1337, '127.0.0.1'); 

console.log('TwiML server running at http://127.0.0.1:1337/'); 

如果您使用的是SDK的老2.x版,你可以使用选择器开关的例子靠近页面的右上角。

+0

非常感谢你的2.x/3.x信息,这正是问题所在。 'const MessagingResponse = require('twilio')。twiml.MessagingResponse;' –