2016-10-05 14 views

答えて

2

ここではTwilioの開発者のエバンジェリストです。

ユーザーがTwilio番号にテキストを入力すると、そのメッセージを取得するには2つの方法があります。

最初のものが最も効率的で、ウェブフックを使用します。 Twilio番号を購入すると、Twilio to make a request to every time it receives an SMS for that numberのURLを設定できます。要求には、メッセージの発信元番号とメッセージ本文が含まれます。

var app = require("express")(); 
var bodyParser = require("body-parser"); 

app.use(bodyParser.urlencoded({extended: false}); 

app.post("/messages", function(req, res) { 
    console.log(req.body.Body); // the message body 
    console.log(req.body.From); // the number that sent the message 
    res.send("<Response/>"); // send an empty response (you can also send messages back) 
}); 

app.listen(3000); // listens on port 3000 

このようにウェブフックを使用してアプリケーションを開発し、I recommend tunneling to your local machine using ngrok:あなたはNode.jsのアプリで要求を受信することができ、ここExpressを使用して簡単な例です。

メッセージを受け取るもう1つの方法は、Twilio's REST APIを使用することです。 messages resourceを使用して、あなたの番号へのすべてのメッセージを一覧表示することができます。 Node.jsでは、次のようになります。

var client = require('twilio')(accountSid, authToken); 

client.messages.list(function(err, data) { 
    data.messages.forEach(function(message) { 
     console.log(message.body); 
    }); 
}); 

これがまったく役に立ったら教えてください。

+0

「authToken」はクライアントの秘密と同じですか? – Username

+0

あなたの認証トークンは、アカウントSIDの下にある[Twilioコンソール](https://www.twilio.com/console)にあります。 – philnash

+0

Twilio番号にテキストを送信しました。それでは、コードの提案の2番目のブロックを試しましたが、 'nodejs app.js'を実行するときにこのエラーが発生しました。これは私のコードです。私はNodeJS v4.6でDebian Jessis 8.2を使用しています。 'node_modules/twilio/node_modules/q/q.js:126 throw e; ^ TypeError:未定義の 'messages'のプロパティを読み取れません。 – Username

関連する問題