Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active May 28, 2021 02:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirawatee/ec8f05a496a2d5b95cedcd059a8a19ea to your computer and use it in GitHub Desktop.
Save jirawatee/ec8f05a496a2d5b95cedcd059a8a19ea to your computer and use it in GitHub Desktop.
Display Rich Menu to match the user's device language
const functions = require("firebase-functions");
const request = require("request-promise");
const LINE_MESSAGING_API = "https://api.line.me/v2/bot";
const LINE_HEADER = {
"Content-Type": "application/json",
Authorization: "Bearer YOUR-CHANNEL-ACCESS-TOKEN"
};
exports.LineBot = functions.https.onRequest(async (req, res) => {
const event = req.body.events[0];
if (event.type === "follow") {
const userId = event.source.userId;
const profile = await getProfile(userId);
linkRichMenu(userId, profile.language);
}
return res.end();
});
const getProfile = (userId) => {
return request.get({
headers: LINE_HEADER,
uri: `${LINE_MESSAGING_API}/profile/${userId}`,
json: true
});
};
const linkRichMenu = (userId, language) => {
let richMenuId = "YOUR-EN-RICH-MENU";
if (language === "th") {
richMenuId = "YOUR-TH-RICH-MENU";
}
request.post({
headers: LINE_HEADER,
uri: `${LINE_MESSAGING_API}/user/${userId}/richmenu/${richMenuId}`
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment