This file contains hidden or 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 request = require("request-promise"); | |
const LINE_HEADER = { | |
"Content-Type": "application/json", | |
Authorization: "Bearer YOUR-CHANNEL-ACCESS-TOKEN" | |
} | |
const LINE_MESSAGING_API = "https://api.line.me/v2/bot/message"; | |
// 1. Copy your Channel Secret from LINE Developers Console then paste is as a value | |
const LINE_CHANNEL_SECRET = "YOUR-CHANNEL-SECRET"; |
This file contains hidden or 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
... | |
} else if (event.message.type === 'text' && event.message.text === 'subscribe') { | |
// [8.2] | |
reply(event.replyToken, { | |
type: 'text', | |
text: 'กรุณายืนยันตัวตนด้วยการอัพโหลดรูปที่มีโลโกทีมที่คุณชื่นชอบ' | |
}); | |
} | |
... |
This file contains hidden or 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
... | |
} else { | |
// [8.1] | |
// Firebase Realtime Database | |
let latest = await admin.database().ref('ucl/score').once('value') | |
reply(event.replyToken, { type: 'text', text: latest.val() }) | |
// Cloud Firestore | |
/* |
This file contains hidden or 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
... | |
case 'postback': { | |
// [8.4] | |
let msg = 'ทีมที่คุณเลือกมันเข้ารอบมาชิง UCL ซะทีไหนเล่า ปั๊ดโถ่!'; | |
let team = event.postback.data.split('=')[1].toLowerCase() | |
if (team.indexOf('liverpool') >= 0 || team.indexOf('tottenham') >= 0) { | |
// Firebase Realtime Database | |
await admin.database().ref('ucl/uid').child(event.source.userId).set(true) | |
This file contains hidden or 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
exports.liveScore = functions.region(region).runWith(runtimeOpts) | |
.firestore.document('ucl/final') | |
.onWrite(async (change, context) => { | |
let latest = change.after.data().score; // ดึงค่าล่าสุดหลังจาก score อัพเดท | |
// ดึงข้อมูลผู้ใช้ที่ subscribe ทั้งหมด | |
let userIds = await admin.firestore().doc('ucl/final').collection('uid').get() | |
userIds.forEach(doc => { |
This file contains hidden or 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
reply(event.replyToken, { | |
"type": "flex", | |
"altText": "Flex Message", | |
"contents": { | |
"type": "bubble", | |
"direction": "ltr", | |
"hero": { | |
"type": "image", | |
"url": "https://4.bp.blogspot.com/-j0d1WQiF-iU/XGxYdwp9C8I/AAAAAAAAUME/8IbomE8q9TUd3tUxWd5yV7-jz9YPVcxXACLcBGAs/s1600/Ucl19.jpg", | |
"size": "full", |
This file contains hidden or 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
exports.finalScore = functions.region(region).pubsub | |
.schedule('02 of jun 04:00') | |
.timeZone('Asia/Bangkok') | |
.onRun(async context => { | |
// ดึงผลการแข่งขันล่าสุดจาก Firestore | |
let result = await admin.firestore().doc('ucl/final').get() | |
broadcast(`จบการแข่งขัน\n${result.data().score}`); // ส่งข้อความให่ทุกคนที่เป็น friend | |
}); |