บทความก่อนหน้าเราส่งข้อความ "ออก" จากบอทได้แล้ว บทความนี้จะสอนให้บอท "รับ" ข้อความจากผู้ใช้และตอบกลับอัตโนมัติ ด้วยเทคนิค Webhook ซึ่งเป็นหัวใจของการทำบอทโต้ตอบ ═══════════════════════════════════════ 📌 1. Webhook คืออะไร (เทียบกับ getUpdates) ═══════════════════════════════════════ มี 2 วิธีให้บอทรับข้อความจากผู้ใช้: • getUpdates (Polling) → เราคอยถาม Telegram เรื่อย ๆ ว่ามีข้อความใหม่ไหม ข้อเสีย: ต้องรันวนตลอด เปลืองทรัพยากร • Webhook → Telegram "ส่งข้อความมาหาเรา" ทันทีที่มีคนทัก ข้อดี: เรียลไทม์ ไม่ต้องคอยถาม (แนะนำ) Webhook ทำงานแบบนี้: ผู้ใช้พิมพ์หาบอท → Telegram ส่งข้อมูล (POST) มาที่ URL ของเรา → โค้ดเราประมวลผลและตอบกลับ ═══════════════════════════════════════ 🔧 2. ตั้งค่า Webhook (setWebhook) ═══════════════════════════════════════ บอก Telegram ว่าให้ส่งข้อความมาที่ URL ไหน (ต้องเป็น HTTPS) เปิดลิงก์นี้ในเบราว์เซอร์ครั้งเดียว:
bash
https://api.telegram.org/bot<TOKEN>/setWebhook?url=<URL_ของเรา>
• <URL_ของเรา> = ที่อยู่สคริปต์ที่จะรับข้อมูล เช่น URL ของ Web App จาก Google Apps Script หรือไฟล์ PHP บนโฮสต์ HTTPS ตรวจสอบสถานะ webhook:
bash
https://api.telegram.org/bot<TOKEN>/getWebhookInfo
ยกเลิก webhook (กลับไปใช้ getUpdates):
bash
https://api.telegram.org/bot<TOKEN>/deleteWebhook
═══════════════════════════════════════ 📥 3. รับข้อความด้วย Google Apps Script ═══════════════════════════════════════ เมื่อตั้ง webhook เป็น URL ของ GAS Web App แล้ว Telegram จะเรียก doPost
javascript
function doPost(e) {
  // แปลงข้อมูลที่ Telegram ส่งมาเป็น object
  var update = JSON.parse(e.postData.contents);

  var chatId = update.message.chat.id;
  var text = update.message.text;

  // ตอบกลับข้อความ
  sendTelegram(chatId, "คุณพิมพ์ว่า: " + text);

  return ContentService.createTextOutput("ok");
}

function sendTelegram(chatId, text) {
  var token = "วาง_BOT_TOKEN";
  var url = "https://api.telegram.org/bot" + token + "/sendMessage";
  UrlFetchApp.fetch(url, {
    "method": "post",
    "payload": { "chat_id": chatId, "text": text }
  });
}
*** หลังแก้โค้ด ต้อง Deploy ใหม่ และตั้ง setWebhook เป็น URL เวอร์ชันล่าสุด *** ═══════════════════════════════════════ 🐘 4. รับข้อความด้วย PHP ═══════════════════════════════════════
php
<?php
$token = "วาง_BOT_TOKEN";

// รับข้อมูลที่ Telegram ส่งมา
$update = json_decode(file_get_contents("php://input"), true);

$chatId = $update["message"]["chat"]["id"];
$text = $update["message"]["text"];

// ตอบกลับ
$reply = "คุณพิมพ์ว่า: " . $text;
file_get_contents("https://api.telegram.org/bot$token/sendMessage?chat_id=$chatId&text=" . urlencode($reply));
?>
═══════════════════════════════════════ 🤖 5. ตอบกลับตามคำสั่ง (Command) ═══════════════════════════════════════ ทำให้บอทตอบต่างกันตามข้อความที่ได้รับ
javascript
function doPost(e) {
  var update = JSON.parse(e.postData.contents);
  var chatId = update.message.chat.id;
  var text = update.message.text;

  var reply = "";
  if (text == "/start") {
    reply = "ยินดีต้อนรับ! พิมพ์ /help เพื่อดูคำสั่ง";
  } else if (text == "/help") {
    reply = "คำสั่งที่มี:\n/start เริ่มต้น\n/time ดูเวลา";
  } else if (text == "/time") {
    reply = "ตอนนี้เวลา: " + new Date().toLocaleString();
  } else {
    reply = "ไม่เข้าใจคำสั่ง ลอง /help";
  }

  sendTelegram(chatId, reply);
  return ContentService.createTextOutput("ok");
}
═══════════════════════════════════════ 📦 6. โครงสร้างข้อมูลที่ Telegram ส่งมา ═══════════════════════════════════════ ตัวอย่าง JSON ที่บอทได้รับเมื่อมีคนพิมพ์
json
{
  "update_id": 100001,
  "message": {
    "message_id": 5,
    "from": { "id": 987654321, "first_name": "สมชาย" },
    "chat": { "id": 987654321, "type": "private" },
    "text": "/start"
  }
}
ข้อมูลสำคัญ: • message.chat.id → ใช้ตอบกลับ • message.text → ข้อความที่ผู้ใช้พิมพ์ • message.from → ข้อมูลผู้ส่ง ═══════════════════════════════════════ ✅ 7. สรุป ═══════════════════════════════════════ • Webhook ทำให้บอทรับข้อความเรียลไทม์ (ดีกว่า polling) • ตั้งด้วย setWebhook ชี้ไป URL ของเรา (ต้อง HTTPS) • GAS ใช้ doPost, PHP ใช้ php://input รับข้อมูล • อ่าน chat.id และ text แล้วตอบกลับด้วย sendMessage • แยกตอบตามคำสั่ง /start /help ได้ จำไว้: เมื่อบอทรับและตอบได้ ก็ทำระบบโต้ตอบอัตโนมัติได้ทุกรูปแบบ 🤖