同样的确切代码一直运行到昨天,我重新启动了 ngrok 并更新了回调网址。但现在它没有回应。相反,我从 Facebook 获得了一个webhook失败警报。
这是我的index.js
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
app.set('port', (process.env.PORT || 5000))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get('/', function (req, res) {
res.send('hello world i am a secret bot')
})
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === '1234567890') {
res.send(req.query['hub.challenge'])
}
console.log('Error, wrong token')
})
app.post('/webhook/', function (req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
if (text === 'Generic') {
sendGenericMessage(sender)
console.log(sender);
continue
}
sendTextMessage(sender, ": " + text.substring(0, 200))
}
if (event.postback) {
let text = JSON.stringify(event.postback)
sendTextMessage(sender, "Postback received", token)
continue
}
}
res.sendStatus(200)
})
const token = process.env.PAGE_ACCESS_TOKEN
function sendTextMessage(sender, text) {
let messageData = { text:text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
我收到此错误:
在端口5000上运行 错误:发送后无法设置标头。
在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:346:11) 在ServerResponse.header(/ Users / akash / Google 驱动器/ challenge6 / messenger- BOT / node_modules /表达/ LIB / response.js:719:10) 在ServerResponse.send(/ Users / akash / Google 驱动器/ challenge6 / messenger- BOT / node_modules /表达/ LIB / response.js:164:12) 在/ Users / akash / Google Drive / challenge6 / messenger-bot / index.js:26:6 在Layer.handle [as handle_request](/ Users / akash / Google Drive / challenge6 / messenger-bot / node_modules / express / lib / router / layer.js:95:5) 在下一个(/ Users / akash / Google Drive / challenge6 / messenger-bot / node_modules / express / lib / router / route.js:131:13) 在Route.dispatch(/ Users / akash / Google Drive / challenge6 / messenger-bot / node_modules / express / lib / router / route.js:112:3) 在Layer.handle [as handle_request](/ Users / akash / Google Drive / challenge6 / messenger-bot / node_modules / express / lib / router / layer.js:95:5) 在/ Users / akash / Google Drive / challenge6 / messenger-bot / node_modules / express / lib / router / index.js:277:22 在Function.process_params(/ Users / akash / Google Drive / challenge6 / messenger-bot / node_modules / express / lib / router / index.js:330:12)
答案 0 :(得分:0)
你添加了Procfile吗?像web:node index.js 请务必将其保存为像Procfile这样的文件,不要像.txt
这样的扩展名