Skip to content

Instantly share code, notes, and snippets.

@PamornT
Created December 22, 2021 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PamornT/1a68c25e607b60a4e87e9be4a46b523d to your computer and use it in GitHub Desktop.
Save PamornT/1a68c25e607b60a4e87e9be4a46b523d to your computer and use it in GitHub Desktop.
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