LINE Chatbot 開發手札 - LIFF
LIFF, LINE Front-end Framework, 一此紀錄與要點。
關於版本
入門參考文章
LIFF 登入

程式碼紀錄
Last updated
LIFF, LINE Front-end Framework, 一此紀錄與要點。

Last updated
liff.init
// 初始化LIFF API,
// 很特別的是,若進入時URL有夾帶轉址資訊則將會自動轉址。
liff.get環境參數
// 初始化LIFF後就可以取得一些環境參數,如:
// liff.getLanguage();
// liff.getVersion();
// liff.getOS();
// liff.getLineVersion();
// liff.isInClient();
if(liff.isLoggedIn) {
// 必需是登入狀態才能取得使用者公開的個資
liff.getProfile
} else {
// 導入 LINE OAuth 登入程序,至少轉址二次。
// ※ 成功的話將會再導回此頁,且此時的狀態是已登入LIFF了。
liff.login
}@* 開發發環境是 MVC5 *@
@* for LIFF init & login & redirect-in *@
@{
String LIFF_ID = ConfigurationManager.AppSettings["LIFF_ID"] ?? "";
String salesId = Request.Params["salesId"] ?? "";
}
<script charset="utf-8" src="https://static.line-scdn.net/liff/edge/2/sdk.js"></script>
<script>
$(function () {
/**
* Initialize LIFF
* param {string} myLiffId The LIFF app ID of the selected element
*/
function initializeLiff(liffId)
{
liff.init({ liffId: liffId }).then(() => {
// start to use LIFF's api
console.log('LIFF init 成功',)
initializeApp();
}).catch((err) => {
console.log('LIFF init 失敗!', err);
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("liffInitErrorMessage").classList.remove('hidden');
});
}
function initializeApp()
{
//// Redirect to another page after the returned Promise object has been resolved
//location.replace("https://line.me/{liffId}/path");
if (!liff.isLoggedIn())
{
console.log('LIFF 未登入',)
const redirectUri = window.location.href;
liff.login({ redirectUri });
// 若登入LIFF成功,將會再 redirect Uri 回到此頁面。
return;
}
liff.getProfile().then(data => {
console.log('lineProfile', data);
// to refresh UI
var form = $('form');
$(form).find('#lineId').val(data.userId);
$(form).find('#lineDisplayName').val(data.displayName);
$(form).find('#salesId').val('@salesId');
$(form).find('#liff_displayName').html(data.displayName);
});
}
// Document is ready
var liffId = '@LIFF_ID';
initializeLiff(liffId);
});
</script>