如何捕获nodejs中的twilio事件?

时间:2016-05-20 05:32:12

标签: node.js twilio webhooks

我是twilio中的新手,并试图了解如何在后端(nodejs)捕捉到twilio的事件。例如,每次我发送消息时,我都想要控制台日志"消息发送"只是为了测试。

我阅读了twilio webhook documentation,但我无法理解如何在nodejs环境中应用它。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。我想你会发现this Twilio tutorial很棒,因为它会引导你完成你想要做的事情,并将向你展示如何向控制台添加事件。

但你想要做的事情的要点如下:

// Create a new REST API client to make authenticated requests against the
// twilio back end
var client = new twilio.RestClient('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');

// Pass in parameters to the REST API using an object literal notation. The
// REST client will handle authentication and response serialzation for you.
client.sms.messages.create({
    to:'YOUR_NUMBER',
    from:'YOUR_TWILIO_NUMBER',
    body:'Twilio message from Node.js'
}, function(error, message) {
    // The HTTP request to Twilio will run asynchronously. This callback
    // function will be called when a response is received from Twilio
    // The "error" variable will contain error information, if any.
    // If the request was successful, this value will be false
    if (!error) {
        // The second argument to the callback will contain the information
        // sent back by Twilio for the request. In this case, it is the
        // information about the text messsage you just sent:
        console.log('Success! The SID for this SMS message is:');
        console.log(message.sid);

        console.log('Message sent on:');
        console.log(message.dateCreated);
    } else {
        console.log('Oops! There was an error.');
    }
});

可以找到节点库的完整文档here

希望这可以帮助你。