Created
April 27, 2022 09:03
-
-
Save kamnan43/0462a5471c2298d207fe547e1b15417a to your computer and use it in GitHub Desktop.
DF Handle Nontext Event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require("firebase-functions"); | |
const line = require('@line/bot-sdk'); | |
const express = require('express'); | |
const { postToDialogflow, createLineTextEvent, convertToDialogflow } = require('./dialogflow') | |
const firebase = require('firebase-admin'); | |
const { WebhookClient } = require('dialogflow-fulfillment'); | |
firebase.initializeApp({}); | |
const config = { | |
channelAccessToken: functions.config().line.channel_access_token, | |
channelSecret: functions.config().line.channel_secret | |
} | |
async function handleEvent(req, event) { | |
switch (event.type) { | |
case 'message': | |
switch (event.message.type) { | |
case 'text': | |
return handleText(req, event); | |
case 'location': | |
return handleLocation(req, event); | |
} | |
case 'postback': | |
return handlePostback(req, event); | |
default: | |
throw new Error(`Unknown event: ${JSON.stringify(event)}`); | |
} | |
} | |
async function handleText(req) { | |
return await postToDialogflow(req); | |
} | |
function handleLocation(req, event) { | |
const message = event.message; | |
const newEvent = createLineTextEvent(req, event, `LAT : ${message.latitude}, LNG : ${message.longitude}`); | |
convertToDialogflow(req, newEvent); | |
} | |
function handlePostback(req, event) { | |
const data = event.postback.data; | |
const newEvent = createLineTextEvent(req, event, `DATE: ${data}`); | |
convertToDialogflow(req, newEvent); | |
} | |
async function handleFulfillment(agent) { | |
const userId = agent.originalRequest.payload.data.source.userId; | |
const { name, latitude, longitude, selected_date } = agent.parameters; | |
const doc = { | |
uid: userId, | |
name, | |
latitude, | |
longitude, | |
selected_date: Date.parse(selected_date) | |
}; | |
await firebase.firestore().collection('member').doc(userId).set(doc); | |
agent.add('บันทึกข้อมูลสำเร็จแล้ว'); | |
} | |
const app = express(); | |
app.post('/webhook', line.middleware(config), (req, res) => { | |
Promise.all(req.body.events.map(event => { | |
return handleEvent(req, event); | |
})) | |
}); | |
app.use(express.json({ limit: '50mb' })); | |
app.post('/fulfillment', (request, response) => { | |
const agent = new WebhookClient({ request, response }); | |
let intentMap = new Map(); | |
intentMap.set('Register - date', handleFulfillment); | |
agent.handleRequest(intentMap); | |
}); | |
exports.api = functions | |
.region('asia-northeast1') | |
.https | |
.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment