Created
December 22, 2021 19:25
-
-
Save PamornT/1a68c25e607b60a4e87e9be4a46b523d to your computer and use it in GitHub Desktop.
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 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']} | |
` | |
const params = { | |
message: message | |
}; | |
const data = Object.keys(params) | |
.map((key) => `${key}=${encodeURIComponent(params[key])}`) | |
.join('&'); | |
await axios({ | |
method: "post", | |
url: "https://notify-api.line.me/api/notify", | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Authorization': `Bearer ${notiToken}` | |
}, | |
data: data | |
}).then((response) => { | |
console.log(response) | |
}).catch((error) => { | |
console.log(error) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment