เก็บข้อมูล Profile Line Oa ลง Google Sheet ด้วย Google App Script เมื่อมีการเพิ่มเพื่อน
function doPost(e) {
const json = JSON.parse(e.postData.contents);
const events = json.events;
for (const event of events) {
if (event.type === 'follow') {
const userId = event.source.userId;
const profile = getLineUserProfile(userId);
saveUserProfileToSheet(profile);
}
}
}
function getLineUserProfile(userId) {
const accessToken = 'YOUR_CHANNEL_ACCESS_TOKEN';
const url = `https://api.line.me/v2/bot/profile/${userId}`;
const options = {
'headers': {
'Authorization': `Bearer ${accessToken}`
}
};
const response = UrlFetchApp.fetch(url, options);
return JSON.parse(response.getContentText());
}
function saveUserProfileToSheet(profile) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('LINE_USER_PROFILES');
sheet.appendRow([
profile.userId,
profile.displayName,
profile.statusMessage,
profile.pictureUrl
]);
}