[英]Twilio receive and reply SMS using Node.js
我試圖設置我們的twilio應用程序以在收到短信時響應客戶端。 我創建了一個發送初始文本的js文件和另一個將在收到回復時發送響應的js文件。
然后,我從機器啟動了初始服務器,然后啟動了ngrok http 1337,我將轉發的URL從ngrok復制並粘貼到twilio儀表板上的消息傳遞Webhook中,並在URL末尾添加了“ / sms”。
當我回復文本時,我沒有得到任何答復,並且可以在ngrok中看到我收到了“ 502 Bad Gateway錯誤”。
const accounSid = 'xxxxxxxxxxxxx'
const authToken = 'xxxxxxxxxxxxx'
const client = require('twilio')(accounSid, authToken)
client.messages.create({
to: '+13101234567',
from: '+13109876543',
body: 'test'
})
.then((message) => {
console.log(message.sid)
})
const http = require('http')
const express = require('express')
const MessagingResponse = require('twilio').twiml.MessagingResponse
const app = express()
app.post('/sms', (req,res) => {
const twiml = new MessagingResponse()
twiml.message('testing 123')
res.writeHead(200, {'Content-Type': 'text/xml'})
res.end(twiml.toString())
})
http.createServer(app).listen(1337, () => {
console.log('express server listening on port 1337')
})
您從ngrok獲得502 Bad Gateway
,因為您的Express http服務器無法正常工作或出現問題。
當您響應SMS時,Twilio正在向您的ngrok發出POST請求,該請求正在工作,因為您在屏幕上看到了502 Bad Gateway
消息。
啟動Express時,您應該看到express server listening on port 1337
如果將瀏覽器轉到http://localhost:1337/
,則應該會看到一個頁面,顯示Cannot GET /
因為瀏覽器未發出POST請求,但至少可以確認Express服務器正在運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.