Skip to content

Instantly share code, notes, and snippets.

View dialogflowAPI_4.ts
import * as functions from "firebase-functions";
import * as dialogflow from "@google-cloud/dialogflow";
import axios from "axios";
const projectId = "<DIALOGFLOW PROJECTID>";
const LINE_HEADER_DEV = {
"Content-Type": "application/json",
"Authorization": "Bearer <DEV CHANNEL ACCESS TOKEN>",
};
const LINE_HEADER_PROD = {
View dialogflowAPI_3.ts
import * as functions from "firebase-functions";
import * as dialogflow from "@google-cloud/dialogflow";
const projectId = "multiple-env-pafg";
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
let dialogflowEnv = "";
//
export const webHook = functions.https.onRequest((request, response) => {
if (request.body.destination === "U19476f0931c6ac37972a6b7f4b4321c6") {
dialogflowEnv = "draft";
View dialogflowAPI_2.ts
import * as functions from "firebase-functions";
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
let dialogflowEnv = "";
//
export const webHook = functions.https.onRequest((request, response) => {
if (request.body.destination === "U19476f0931c6ac37972a6b7f4b4321c6") {
dialogflowEnv = "draft";
} else if (request.body.destination === "U92a6f010a3fd78085b8763159a04772b") {
dialogflowEnv = "production";
View dialogflowAPI_1.ts
import * as functions from "firebase-functions";
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const webHook = functions.https.onRequest((request, response) => {
console.log(request.body);
const events = request.body.events;
events.forEach( async (event: any) => {
console.log(event);
if (event.type === "message") {
View videoPlayComplete_step3-1.js
// Handle events
events.forEach(async(event) => {
if (event.type === 'videoPlayComplete') {
await savePointVideoComplete(event)
}
console.log(event)
})
View videoPlayComplete.js
const admin = require("firebase-admin")
const functions = require("firebase-functions")
const region = 'asia-northeast1'
const channelSecret = ''
const channelAccessToken = ''
admin.initializeApp()
View videoPlayComplete_step7.js
const reply = async(replyToken, messages) => {
const axios = require("axios")
const data = { replyToken, messages }
const headers = {
'Authorization': `Bearer ${channelAccessToken}`,
'Content-Type': 'application/json'
}
axios.post('https://api.line.me/v2/bot/message/reply', data, {
headers: headers
})
View videoPlayComplete_step6.js
const collectPoint = async(event) => {
try {
const userId = event.source.userId
const trackingId = event.videoPlayComplete.trackingId
const timestamp = event.timestamp
const params = {
userId, trackingId, timestamp
}
await admin.firestore().collection('collectPoint').add(params)
return true
View videoPlayComplete_step5.js
const checkPointExists = async(event) => {
try {
const userId = event.source.userId
const trackingId = event.videoPlayComplete.trackingId
const data = await admin.firestore()
.collection('collectPoint')
.where('userId', '==', userId)
.where('trackingId', '==', trackingId)
.get()
return !data.empty
View videoPlayComplete_step4.js
const savePointVideoComplete = async(event) => {
const replyToken = event.replyToken
let messages = ''
const pointExists = await checkPointExists(event)
if (pointExists) {
messages = [{ 'type': 'text', 'text': '🎉 ขอบคุณที่รับชมวีดีโอ คุณเคยได้รับคะแนนกิจกรรมนี้ไปแล้ว 🎉' }]
} else {
await collectPoint(event)
messages = [{ 'type': 'text', 'text': '🎉 ขอบคุณที่รับชมวีดีโอ รับคะแนนไปเลย 10 คะแนน 🎉' }]
}