รับและประมวลผลข้อมูลจาก LINE Webhook

กิจกรรมที่ 1 รับและประมวลผลข้อมูลจาก LINE Webhook
function doPost(e) {
 const data = JSON.parse(e.postData.contents);
 const event = data.events[0];
}

e.postData.contents: ข้อมูลที่ส่งมาจาก LINE จะอยู่ในรูปแบบ JSON string (ข้อความที่เข้ารหัสในรูปแบบ JSON) ซึ่งเก็บอยู่ใน e.postData.contents

JSON.parse(): แปลง JSON string ให้เป็น JavaScript object เพื่อให้เราสามารถเข้าถึงข้อมูลได้ง่ายขึ้น

{
  "destination": "xxxxxxxxxx",
  "events": [
    {
      "type": "message",
      "message": {
        "type": "text",
        "id": "468789577898262530", // ID of the sent message
        "quotedMessageId": "468789532432007169", // ID of the quoted message
        "quoteToken": "q3Plxr4AgKd...",
        "text": "สวัสดี" // Text of the sent message
      },
      "webhookEventId": "01H810YECXQQZ37VAXPF6H9E6T",
      "deliveryContext": {
        "isRedelivery": false
      },
      "timestamp": 1692251666727,
      "source": {
        "type": "group",
        "groupId": "Ca56f94637c...",
        "userId": "U4af4980629..."
      },
      "replyToken": "38ef843bde154d9b91c21320ffd17a0f",
      "mode": "active"
    }
  ]
}

กิจกรรมที่ 2 ตอบกลับในช่องแชท LINE
const LINE_TOKEN ='xxxxxxxx';

function doPost(e) {
 const data = JSON.parse(e.postData.contents);
    const event = data.events[0];
    if (event.type === 'message' && event.message.type === 'text') {
      const userText = event.message.text.trim().toLowerCase();
      const replyToken = event.replyToken;
      if(userText == "สวัสดี"){
        sendMessage(replyToken, { type: 'text', text: 'สวัสดีครับ! 😊' });
      }else{
        sendMessage(replyToken, { type: 'text', text: 'ขออภัย ไม่เข้าใจคำสั่งนี้' });
      }
    }
}

function sendMessage(replyToken, message) {
  const url = 'https://api.line.me/v2/bot/message/reply';
  UrlFetchApp.fetch(url, {
    method: 'post',
    contentType: 'application/json',
    headers: { Authorization: `Bearer ${LINE_TOKEN}` },
    payload: JSON.stringify({ replyToken, messages: [message] })
  });
}

รูปแบบ message แบบต่างๆ

Download LINE Bot Designer

https://developers.line.biz/en/docs/messaging-api/download-bot-designer

ข้อความ

{
  "type": "text",
  "text": "Hello"
}

รูปภาพ

{
  "type": "image",
  "originalContentUrl": "PROVIDE_URL_FROM_YOUR_SERVER",
  "previewImageUrl": "PROVIDE_URL_FROM_YOUR_SERVER"
}

สติกเกอร์

https://developers.line.biz/en/docs/messaging-api/sticker-list

{
  "type": "sticker",
  "packageId": "11537",
  "stickerId": "52002739"
}

location

{
  "type": "location",
  "title": "บ้านของฉัน",
  "address": "91 หมู่ 2 ต.ลำปำ อ.เมือง จ.พัทลุง",
  "latitude": 35.65910807942215,
  "longitude": 139.70372892916203
}

flex

{
  "type": "flex",
  "altText": "หัวข้อ",
  "contents": {
  "type": "bubble",
  "body": { ด้านใน }
  }
}

quick reply buttons

https://developers.line.biz/en/docs/messaging-api/using-quick-reply/#set-quick-reply-buttons

{
  "type": "text", // 1
  "text": "Select your favorite food category or send me your location!",
  "quickReply": { // 2
    "items": [
      {
        "type": "action", // 3
        "imageUrl": "https://example.com/sushi.png",
        "action": {
          "type": "message",
          "label": "Sushi",
          "text": "Sushi"
        }
      },
      {
        "type": "action",
        "imageUrl": "https://example.com/tempura.png",
        "action": {
          "type": "message",
          "label": "Tempura",
          "text": "Tempura"
        }
      },
      {
        "type": "action", // 4
        "action": {
          "type": "location",
          "label": "Send location"
        }
      }
    ]
  }
}

อื่นๆ

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *