/**========================================
用戶傳送訊息給聊天機器人
=========================================*/
function lineMessage(botData) {
const botMessageType = botData.events[0].message.type;
// 判斷訊息是否為文字
if (botMessageType === 'text') {
// 用戶聊天訊息內容
const botMessageText = botData.events[0].message.text;
// 用戶訊息的來源
const source = botData.events[0].source;
// 用戶 token
const replyToken = botData.events[0].replyToken;
// --------------------------------- 文字訊息(最多5組)
// let messages = [{
// 'type': 'text',
// 'text': botMessageText
// }];
//---------------------------------- 圖片訊息(最多5組)
// let messages = [
// {
// "type": "image",
// "originalContentUrl": "https://i.imgur.com/Fn9gu0q.png",
// "previewImageUrl": "https://i.imgur.com/Fn9gu0q.png"
// }
// ];
//---------------------------------- 文字+圖片訊息(最多5組)
// let messages = [
// {
// 'type': 'text',
// 'text': botMessageText
// },
// {
// "type": "image",
// "originalContentUrl": "https://i.imgur.com/hwvXBjq.png",
// "previewImageUrl": "https://i.imgur.com/hwvXBjq.png"
// }
// ];
//---------------------------------- flex(訂單完成)
// let messages = [{
// "type": "flex",
// "altText": "訂單完成",//訊息副標題
// "contents": flexTemplate()
// }];
if (botMessageText === '商品訂購') {
//---------------------------------- carousel(商品展示)
let messages = [{
"type": "flex",
"altText": "商品展示",//訊息副標題
"contents": {
"type": "carousel",
"contents": prodCarouselTemplate()
}
}];
lineReplyMessage(replyToken, messages);
} else {
// https://line.me/R/ti/p/{basicId}
let lineUser = getLineUser(source.userId);
let botInfo = getLineBotInfo();
if (lineUser.displayName == '') {//不是好友
let url = `https://line.me/R/ti/p/${botInfo.basicId}`;
//---------------------------------- flex(請加好友)
let messages = [{
"type": "flex",
"altText": "請加好友",//訊息副標題
"contents": errorFlexTemplate(url)
}];
lineReplyMessage(replyToken, messages);
return;
}
//搜尋關鍵字
let prod = searchKeyWord(botMessageText);
if (prod.sn) {
//商品購買數量
prod.amount = Number(botMessageText.split(' ')[0]);
let group = '';
if (source.type === 'group') {
group = getLineGroup(source.groupId).groupName;
}
let formData = {
sn: '',
date: '',
userId: source.userId,
group: group,
name: lineUser.displayName,
prod_sn: prod.sn,
title: prod.title,
price: prod.price,
unit: prod.unit,
amount: prod.amount,
total: Number(prod.price) * prod.amount,
pic: prod.pic
}
let respond = insert_sheet_order_main(formData);
if (respond.result === true) {
//---------------------------------- flex(訂單完成)
let messages = [{
"type": "flex",
"altText": "訂單完成",//訊息副標題
"contents": orderFlexTemplate(respond.row)
}];
lineReplyMessage(replyToken, messages);
return;
}
}
}
}
}
/*============================================
orderFlexTemplate
https://developers.line.biz/flex-simulator/
============================================*/
function orderFlexTemplate(row) {
// let order = {
// sn: '',
// date: '',
// userId: 'userId',
// group: 'group',
// name: 'xxx',
// prod_sn: 'prod_sn',
// title: 'Brown Cafe',
// unit: '個',
// price: 200,
// amount: 3,
// total: 600,
// pic: 'https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_1_cafe.png'
// }
// let content = 'xxx 您好,您的訂購資料:' + '\n';
// content += '商品單位: ' + '個' + '\n';
// content += '商品單價: ' + '200' + ' 元\n';
// content += '訂購數量: ' + '3' + '\n';
// content += '合計金額: ' + '600' + ' 元\n';
let content = `${row.name} 您好,您的訂購資料:
商品單價: ${row.price} 元
訂購數量: ${row.amount} ${row.unit}
合計金額: ${row.total} 元
`;
let messages = {
"type": "bubble",
"hero": {
"type": "image",
"url": row.pic,//圖片
"size": "full",
"aspectRatio": "20:13",
"aspectMode": "cover"
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": row.title + ' 購買成功',//標題
"weight": "bold",
"size": "xl"
},
{
"type": "text",
"text": content,//內容
"wrap": true,
"color": "#666666",
"size": "sm",
}
]
}
}
return messages;
}