Skip to content

Instantly share code, notes, and snippets.

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 คะแนน 🎉' }]
}
View videoPlayComplete_step3.js
// Handle events
events.forEach(async(event) => {
if (event.type === 'videoPlayComplete') {
await savePointVideoComlete(event)
}
console.log(event)
})
View videoPlayComplete_step2.js
// Verify Events request must array
const events = request.body.events
if(!events || !Array.isArray(events)) {
response.status(200).send('not have events')
return
}
View videoPlayComplete_step1.js
exports.LINEwebhook = functions.region(region).https.onRequest(async(request, response) => {
console.log('🚀 Start Webhook!!', request.body )
// Verify Signature
const verify = await verify_signature(request, response)
if(!verify) {
response.status(200).send('Unauthorized')
return
}
View GGSheets2RTDB_Functions.js
const functions = require("firebase-functions")
const axios = require("axios")
const notiToken = 'REPLACE LINE NOTIFY TOKEN HERE'
exports.monitorData = functions.region("asia-southeast1").database.ref('Product/{id}').onWrite(async (snap, context) => {
message = `\มีข้อมูลเปลี่ยนแปลง
name จาก ${snap.before._data['1']} -> ${snap.after._data['1']}
stock จาก ${snap.before._data['2']} -> ${snap.after._data['2']}
`