如果用 Line Message API, 實際上每個使用者發過來的訊息,只有Line ID,Line ID 是一大串的數字或文字,但對人類來說,並沒有意義。訊息裏也沒有使用者名稱,所以,如果要找到使用者的名稱,請參考這裏的官方說明
要使用這個endpoint https://api.line.me/v2/bot/profile/{userId}
,在google app script 的實作函數如下:
function line_name(temp_Lineid,channel_access_token){
var response = UrlFetchApp.fetch("https://api.line.me/v2/bot/profile/"+temp_Lineid, {
"method": "GET",
"headers": {
"Authorization": "Bearer " + channel_access_token,
"Content-Type": "application/json"
},
});
var namedata = JSON.parse(response); // 解析 json
return namedata.displayName;
}
當然要使用endpoint 要滿足兩個條件之一。
1.使用者有加上Line bot (Line office account)
2.使用者沒加上Line bot (Line office account),但發訊息給Line bot,也沒有封阻Line bot
得到使用者名稱有什麼好處呢?大概就是Line bot在回答時,不會那麼冰冷,可以加上客戶名稱,讓客戶體驗更好吧。