🎯 ยินดีต้อนรับสู่ Advanced Tutorials
Welcome to Advanced Tutorials
🇹🇭 คุณจะได้เรียนรู้
ใน tutorial นี้คุณจะอัพเกรด Echo Bot ของคุณให้กลายเป็น bot ที่ใช้งานจริงได้ — ตอบตามคีย์เวิร์ด ต้อนรับผู้ใช้ใหม่ และส่งการ์ดสวยงาม!
🇬🇧 What You Will Learn
In these tutorials you will upgrade your Echo Bot into a real-world bot — responding to keywords, welcoming new followers, and sending beautiful message cards!
🔑 Tutorial 1: Keyword Response Bot
บทที่ 1: Bot ตอบตามคีย์เวิร์ด
Keyword Response Bot — Bot ที่เข้าใจคำสั่ง
🌱 Easy / ง่ายWhat you'll build: A bot that reads the user's message and gives a different reply based on the keyword. For example: "สวัสดี" → "สวัสดีครับ! ยินดีต้อนรับ! 🙏"
คุณจะสร้าง: Bot ที่อ่านข้อความผู้ใช้และตอบตามคีย์เวิร์ด
You'll learn: if/elif statements, string comparison, multiple response types
1เข้าใจ Logic — Understand the Logic
2สร้างฟังก์ชันตอบกลับ — Create the Reply Function
get_reply(message) ที่รับข้อความและส่งคำตอบกลับมาget_reply(message) that receives the message text and returns the reply.
3ทดสอบ Bot — Test Your Bot
ngrok http 5000 และ python keyword_bot.py พร้อมกัน แล้วทดสอบส่งคีย์เวิร์ดต่างๆ ใน LINEngrok http 5000 and python keyword_bot.py together, then test by sending different keywords in LINE.
💡 Challenge! / ท้าทาย!
ไทย: ลองเพิ่มคีย์เวิร์ดและคำตอบของคุณเอง! เช่น ชื่อร้าน เมนูอาหาร หรือคำถามที่พบบ่อยของธุรกิจจริง
English: Try adding your own keywords and replies! For example: shop name, food menu, or real business FAQ.
👋 Tutorial 2: Welcome Message Bot
บทที่ 2: Bot ส่งข้อความต้อนรับ
Welcome Message Bot — Bot ต้อนรับสมาชิกใหม่
🔍 Medium / ปานกลางWhat you'll build: A bot that detects when a new user follows it and automatically sends a personalized welcome message with their display name.
คุณจะสร้าง: Bot ที่ตรวจจับเมื่อมีคนใหม่ติดตาม และส่งข้อความต้อนรับพร้อมชื่อผู้ใช้
1เข้าใจ Follow Event — Understand Follow Events
2ดึงชื่อผู้ใช้ — Get the User's Name
💡 Push vs Reply — ต่างกันอย่างไร?
reply_message: ตอบกลับโดยใช้ reply_token (ใช้ได้ครั้งเดียวต่อ event, ฟรี)
push_message: ส่งหาผู้ใช้ตอนใดก็ได้โดยใช้ user_id (มีค่าใช้จ่ายใน production)
English: reply_message uses a reply token (free, one-time use per event). push_message can be sent anytime using user_id (costs money in production).
🎨 Tutorial 3: Flex Message Bot
บทที่ 3: Bot ส่งการ์ดสวยงาม
Flex Message Bot — Bot ส่ง Message Card
🏆 Hard / ยากWhat you'll build: A bot that responds with a beautiful card containing an image, title, description, and action buttons — perfect for a product catalog or menu.
คุณจะสร้าง: Bot ที่ส่งการ์ดสวยงามพร้อมรูปภาพ หัวข้อ คำอธิบาย และปุ่มกด
1เข้าใจ Flex Message — Understand Flex Messages
2สร้าง Flex Message — Build the Flex Message
⚠️ Image URLs / URL ของรูปภาพ
ไทย: URL รูปภาพต้องเป็น HTTPS เท่านั้น LINE จะไม่แสดงรูปที่ใช้ HTTP
English: Image URLs must use HTTPS only. LINE will not display images served over HTTP.
📋 Reference — ตารางอ้างอิง
| Method / เมธอด | ใช้เมื่อ / Use When | Example |
|---|---|---|
reply_message() | ตอบกลับข้อความที่รับมา Reply to a received message | line_bot_api.reply_message(event.reply_token, msg) |
push_message() | ส่งหาผู้ใช้ตอนใดก็ได้ Send to user anytime | line_bot_api.push_message(user_id, msg) |
get_profile() | ดึงข้อมูลโปรไฟล์ Get user profile data | profile = line_bot_api.get_profile(user_id) |
TextSendMessage | ส่งข้อความธรรมดา Send plain text | TextSendMessage(text="Hello!") |
FlexSendMessage | ส่ง message card Send a rich card | FlexSendMessage(alt_text=..., contents=bubble) |
| Event Type | เมื่อไหร่ / When | Handler |
|---|---|---|
MessageEvent | ผู้ใช้ส่งข้อความ / User sends a message | @handler.add(MessageEvent, message=TextMessage) |
FollowEvent | มีคนติดตาม / Someone follows | @handler.add(FollowEvent) |
UnfollowEvent | มีคนเลิกติดตาม / Someone unfollows | @handler.add(UnfollowEvent) |
PostbackEvent | ผู้ใช้กดปุ่ม / User taps a button | @handler.add(PostbackEvent) |
🏆 Best Practices — แนวทางปฏิบัติที่ดี
🇹🇭 สิ่งที่ควรทำ
- เก็บ token ไว้ใน environment variable ไม่ใช่ในโค้ด
- ตรวจสอบ signature ทุกครั้งก่อนประมวลผล
- ตอบกลับทันที (
return 'OK') แม้ยังไม่ได้ประมวลผล - ใส่ default reply เสมอ ถ้าไม่มีคีย์เวิร์ดตรง
- ทดสอบด้วย ngrok ก่อน deploy จริง
🇬🇧 Best Practices
- Store tokens in environment variables, not in code
- Always verify the signature before processing
- Return 'OK' immediately (LINE expects a fast response)
- Always include a default reply for unrecognised input
- Test with ngrok before deploying to a real server
🏆 คุณเป็น LINE Bot Developer แล้ว! / You are a LINE Bot Developer!
คุณสร้าง bot ที่ตอบตามคีย์เวิร์ด ต้อนรับสมาชิกใหม่ และส่งการ์ดสวยงามได้แล้ว 🎉
You built a bot that responds to keywords, welcomes new followers, and sends beautiful Flex Message cards! 🎉