Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active July 4, 2023 12:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jirawatee/279f60244ec0d9aefd8449454e2b4a09 to your computer and use it in GitHub Desktop.
Save jirawatee/279f60244ec0d9aefd8449454e2b4a09 to your computer and use it in GitHub Desktop.
Link Rich Menu
const functions = require("firebase-functions");
const request = require("request-promise");
exports.RichMenu = functions.https.onRequest((req, res) => {
// เปิดทางให้ Ajax จาก LIFF มา Request
res.header('Access-Control-Allow-Origin', '*')
let richMenuId1 = 'YOUR-RICH-MENU-ID-1';
let richMenuId2 = 'YOUR-RICH-MENU-ID-2';
if (req.body.uid !== undefined) {
// คุณอาจทำการ auth ด้วย username และ password ที่ผู้ใช้กรอกมา
// และคุณอาจเก็บข้อมูล uid ลง db เพื่อผูกกับ existing account เดิมที่มีอยู่ในระบบ
link(req.body.uid, richMenuId1)
} else {
let event = req.body.events[0]
if (event.type === 'postback') {
switch (event.postback.data) {
case 'richmenu1': link(event.source.userId, richMenuId1); break
case 'richmenu2': link(event.source.userId, richMenuId2); break
}
}
}
return res.status(200).send(req.method)
})
const link = async (uid, richMenuId) => {
await request.post({
uri: `https://api.line.me/v2/bot/user/${uid}/richmenu/${richMenuId}`,
headers: { Authorization: 'YOUR-CHANNEL-ACCESS-TOKEN' }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment