我正在通过在Firebase中运行Node.js创建Line Bot,但在我的本地环境中不起作用。
当执行“ firebase提供--only功能,托管”并访问“ http:// localhost:5000 / webhook”时,将显示“无法获取/ xxx / us-central1 /应用程序/ webhook”。
index.ts
'use strict';
const functions = require('firebase-functions');
const express = require('express');
const line = require('@line/bot-sdk');
const config = {
channelSecret: 'channelSecret',channelaccessToken: 'channelaccessToken'
};
const app = express();
app.post('/webhook',line.middleware(config),(req: any,res: any) => {
console.log(req.body.events);
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((result) => console.log('error!!!'));
});
const client = new line.Client(config);
async function handleEvent(event: any) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
return client.replyMessage(event.replyToken,{
type: 'text',text: event.message.text
});
}
exports.app = functions.https.onRequest(app);
firebase.json
{
"hosting": {
"public": "./","rewrites": [{
"source": "/webhook","function": "app"
}],"ignore": [
"firebase.json","**/.*","**/node_modules/**"
]
}
}
package.json
{
"name": "functions","scripts": {
"lint": "tslint --project tsconfig.json","build": "tsc","serve": "npm run build && firebase serve --only functions --project golfure","shell": "npm run build && firebase functions:shell","start": "npm run shell","deploy": "firebase deploy --only functions","logs": "firebase functions:log"
},"engines": {
"node": "8"
},"main": "lib/index.js","dependencies": {
"@line/bot-sdk": "^6.8.2","express": "^4.17.1","firebase-admin": "^8.6.0","firebase-functions": "^3.3.0","ngrok": "^3.2.5"
},"devDependencies": {
"tslint": "^5.12.0","typescript": "^3.2.2","firebase-functions-test": "^0.1.6"
},"private": true
}
它将如何工作? 谢谢。