質問編集履歴
1
補足を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -203,3 +203,101 @@
|
|
203
203
|
}
|
204
204
|
|
205
205
|
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
###補足
|
210
|
+
|
211
|
+
下記のプログラムが同じindex.jsファイルに書かれていますが、こちらは動作します。
|
212
|
+
|
213
|
+
```
|
214
|
+
|
215
|
+
'use strict';
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
const functions = require('firebase-functions');
|
220
|
+
|
221
|
+
const express = require('express');
|
222
|
+
|
223
|
+
const line = require('@line/bot-sdk');
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
// channel secretとaccess tokenをFirebaseの環境変数から呼び出す
|
228
|
+
|
229
|
+
const config = {
|
230
|
+
|
231
|
+
channelSecret: functions.config().channel.secret,
|
232
|
+
|
233
|
+
channelAccessToken: functions.config().channel.accesstoken
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
// create LINE SDK client
|
240
|
+
|
241
|
+
const client = new line.Client(config);
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
// create Express app
|
246
|
+
|
247
|
+
const app = express();
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
// register a webhook handler with middleware
|
252
|
+
|
253
|
+
app.post('/webhook', line.middleware(config), (req, res) => {
|
254
|
+
|
255
|
+
Promise
|
256
|
+
|
257
|
+
.all(req.body.events.map(handleEvent))
|
258
|
+
|
259
|
+
.then((result) => res.json(result))
|
260
|
+
|
261
|
+
.catch((err) => {
|
262
|
+
|
263
|
+
console.error(err);
|
264
|
+
|
265
|
+
res.status(500).end();
|
266
|
+
|
267
|
+
});
|
268
|
+
|
269
|
+
});
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
// event handler
|
274
|
+
|
275
|
+
async function handleEvent(event) {
|
276
|
+
|
277
|
+
if (event.type !== 'message' || event.message.type !== 'text') {
|
278
|
+
|
279
|
+
// ignore non-text-message event
|
280
|
+
|
281
|
+
return Promise.resolve(null);
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
// create a echoing text message
|
288
|
+
|
289
|
+
const echo = { type: 'text', text: event.message.text };
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
// use reply API
|
294
|
+
|
295
|
+
return client.replyMessage(event.replyToken, echo );
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
exports.app = functions.https.onRequest(app);
|
302
|
+
|
303
|
+
```
|