Skip to content

Instantly share code, notes, and snippets.

View jirawatee's full-sized avatar
🔥
Better Together

Jirawat Karanwittayakarn jirawatee

🔥
Better Together
View GitHub Profile
@jirawatee
jirawatee / index.js
Last active May 27, 2019 20:05
Broadcast final score in UCL by Firebase Realtime Database
exports.finalScore = functions.region(region).pubsub
.schedule('02 of jun 04:00')
.timeZone('Asia/Bangkok')
.onRun(async context => {
// ดึงผลการแข่งขันล่าสุด
let result = await admin.database().ref('ucl/score').once('value');
broadcast(`จบการแข่งขัน\n${result.val()}`); // ส่งข้อความหาทุกคนที่เป็น friend
});
@jirawatee
jirawatee / index.js
Last active May 27, 2019 19:44
Live Score - UCL
exports.liveScore = functions.region(region).runWith(runtimeOpts)
.database.ref('ucl/score')
.onWrite(async (change, context) => {
let latest = change.after.val(); // ดึงค่าล่าสุดหลังการอัพเดทของ score ออกมา
// ดึงข้อมูลผู้ใช้ที่ subscribe ทั้งหมด
let userIds = await admin.database().ref('ucl/uid').once('value')
Object.keys(userIds.val()).forEach(userId => {
@jirawatee
jirawatee / index.js
Last active May 27, 2019 19:45
Logo detection in UCL
exports.logoDetection = functions.region(region).runWith(runtimeOpts)
.storage.object()
.onFinalize(async (object) => {
const fileName = object.name // ดึงชื่อไฟล์มา
const userId = fileName.split('.')[0] // แยกชื่อไฟล์ออกมา ซึ่งมันก็คือ userId
// ทำนายโลโกที่อยู่ในภาพด้วย Cloud Vision API
const [result] = await client.logoDetection(`gs://${object.bucket}/${fileName}`);
const logos = result.logoAnnotations;
@jirawatee
jirawatee / index.js
Last active March 5, 2020 06:25
doImage function in UCL
const doImage = async (event) => {
const path = require("path");
const os = require("os");
const fs = require("fs");
const UUID = require("uuid-v4");
// กำหนด URL ในการไปดึง binary จาก LINE กรณีผู้ใช้อัพโหลดภาพมาเอง
let url = `${LINE_CONTENT_API}/${event.message.id}/content`;
// ตรวจสอบว่าภาพนั้นถูกส่งมจาก LIFF หรือไม่
@jirawatee
jirawatee / index.js
Last active May 27, 2019 19:44
Structure of UCL Webhook API by Cloud Functions for Firebase
exports.UCL = functions.region(region).runWith(runtimeOpts)
.https.onRequest(async (req, res) => {
let event = req.body.events[0]
switch (event.type) {
case 'message':
if (event.message.type === 'image') {
// [8.3]
} else if (event.message.type === 'text' && event.message.text === 'subscribe') {
// [8.2]
} else {
@jirawatee
jirawatee / index.js
Created May 27, 2019 15:12
Messaging API - Push, Reply and Broadcast
// Push Message
const push = (userId, msg, quickItems) => {
return request.post({
headers: LINE_HEADER,
uri: `${LINE_MESSAGING_API}/push`,
body: JSON.stringify({
to: userId,
messages: [{ type: "text", text: msg, quickReply: quickItems }]
})
})
@jirawatee
jirawatee / index.js
Last active March 5, 2020 06:24
Configurations in Cloud Functions for Firebase
const functions = require('firebase-functions');
const request = require("request-promise");
const admin = require('firebase-admin');
admin.initializeApp();
const region = 'asia-east2';
const runtimeOpts = {
timeoutSeconds: 4,
memory: "2GB"
@jirawatee
jirawatee / index.js
Created April 22, 2019 12:48
Scheduling a function in Cloud Functions for Firebase
const functions = require("firebase-functions");
const request = require("request-promise");
const OPENWEATHER_APPID = "<YOUR-OPEN-WEATHER-APP-ID>";
const LINE_MESSAGING_API = "https://api.line.me/v2/bot/message";
const LINE_UID = "<YOUR-LINE-ID>";
const LINE_HEADER = {
"Content-Type": "application/json",
Authorization:
@jirawatee
jirawatee / songkran-thailand.gif
Last active April 19, 2019 16:43
Songkran Festival, Thailand
songkran-thailand.gif
@jirawatee
jirawatee / line-broadcast.js
Created April 18, 2019 10:06
LINE Messaging API - Broadcast JS
const functions = require('firebase-functions');
const request = require('request-promise');
const LINE_MESSAGING_API = 'https://api.line.me/v2/bot/message';
const LINE_HEADER = {
'Content-Type': 'application/json',
'Authorization': `Bearer xxxxx`
};
exports.LineBotBroadcast = functions.https.onRequest((req, res) => {