💬 LINE Chatbot — เรียนรู้สร้าง Chatbot

Learn to Build Your Own LINE Bot — เรียนสร้าง Chatbot บน LINE

Week 9 · Present Continuous · Webhook · Messaging API

🎯 LINE Chatbot คืออะไร?
What is a LINE Chatbot?

🇹🇭 ภาษาไทย

LINE คือแอปพลิเคชันส่งข้อความที่ได้รับความนิยมสูงสุดในประเทศไทย มีผู้ใช้มากกว่า 55 ล้านคนในไทยและมากกว่า 200 ล้านคนทั่วโลก


LINE Chatbot คือโปรแกรมคอมพิวเตอร์ที่ทำงานอยู่ใน LINE และสามารถรับ-ส่งข้อความโดยอัตโนมัติ เมื่อผู้ใช้พิมพ์ข้อความ bot จะตอบกลับทันทีโดยไม่ต้องมีคนช่วย


บริษัทและร้านค้าหลายแห่งใช้ LINE Chatbot เพื่อตอบคำถามลูกค้า รับออเดอร์ และส่งโปรโมชั่น

🇬🇧 English

LINE is the most popular messaging app in Thailand, with over 55 million users in Thailand and more than 200 million users worldwide.


A LINE Chatbot is a computer program that lives inside LINE and can send and receive messages automatically. When a user types a message, the bot replies instantly — no human needed!


Businesses use LINE Chatbots to answer questions, take orders, and send promotions.

🌟 Fun Facts — ข้อเท็จจริงน่าสนใจ

  • LINE was created in Japan in 2011 after the Tōhoku earthquake — LINE ถูกสร้างในญี่ปุ่นปี 2011 หลังแผ่นดินไหว
  • Thailand has the highest LINE usage per capita outside Japan — ไทยใช้ LINE มากที่สุดนอกญี่ปุ่น
  • Over 50% of Thai businesses use LINE Official Account — กว่า 50% ของธุรกิจไทยใช้ LINE Official Account
  • LINE bots can speak, show images, maps, and even payment buttons — Bot สามารถพูด แสดงรูป แผนที่ และปุ่มชำระเงินได้

🇹🇭 ทำไมต้องเรียน LINE Bot?

  • ทักษะที่ตลาดแรงงานต้องการสูงมากในไทย
  • สร้างได้จริงด้วย Python หรือ Node.js
  • ใช้ได้กับธุรกิจของตนเองหรือลูกค้า
  • เรียนรู้ API, Webhook และ JSON จริงๆ

🇬🇧 Why Learn LINE Bot?

  • High-demand skill for Thai tech jobs
  • Build real bots with Python or Node.js
  • Use for your own business or clients
  • Learn real APIs, webhooks, and JSON

📚 คำศัพท์สำคัญ
Key Vocabulary

💬 Chatbot
โปรแกรมแชทอัตโนมัติ
A computer program that automatically replies to messages.
โปรแกรมที่ตอบข้อความโดยอัตโนมัติ
🔗 Webhook
เว็บฮุก (ช่องรับสัญญาณ)
A URL that receives data when an event happens. LINE sends messages to your webhook.
URL ที่รับข้อมูลเมื่อมีเหตุการณ์เกิดขึ้น LINE ส่งข้อความมาที่ webhook ของคุณ
🔑 Channel Access Token
รหัสลับสำหรับเข้าถึง Channel
A secret key that proves your server is allowed to send messages from your bot.
รหัสที่ยืนยันว่าเซิร์ฟเวอร์ของคุณมีสิทธิ์ส่งข้อความจาก bot
📡 Event
เหตุการณ์
Something that happens on LINE — a message, a follow, an unfollow. LINE sends events to your webhook.
สิ่งที่เกิดขึ้นบน LINE เช่น ข้อความ การติดตาม การเลิกติดตาม
📨 Reply Token
โทเค็นสำหรับตอบกลับ
A temporary key sent with each message event. You use it to reply to that specific message.
รหัสชั่วคราวที่ส่งมากับทุก message event ใช้สำหรับตอบกลับข้อความนั้น
📦 Payload
ข้อมูลที่ส่งมา (JSON)
The JSON data that LINE sends to your webhook, containing the event type, user ID, and message content.
ข้อมูล JSON ที่ LINE ส่งมายัง webhook ของคุณ
🎨 Flex Message
ข้อความแบบกำหนดเอง
A rich, customizable message format in LINE — like a mini web page with images, buttons, and cards.
รูปแบบข้อความสวยงามที่กำหนดเองได้ เหมือนหน้าเว็บย่อยๆ
📋 Rich Menu
เมนูแถบล่าง
A large image menu at the bottom of the LINE chat that users can tap to send commands.
เมนูรูปภาพขนาดใหญ่ที่ด้านล่างของแชท ผู้ใช้แตะเพื่อส่งคำสั่งได้
🏭 Channel
ช่องทาง (บัญชี Bot)
Your bot's LINE account. You create a channel in the LINE Developers Console and get your token from it.
บัญชี LINE ของ bot คุณสร้าง channel ใน LINE Developers Console
🌐 LINE Developers Console
แผงควบคุมนักพัฒนา
The website (developers.line.biz) where you create and manage your LINE bot channels.
เว็บไซต์ที่ใช้สร้างและจัดการ channel ของ LINE bot
⚡ Trigger
ตัวกระตุ้น / เงื่อนไข
The condition that causes the bot to run — usually a keyword or event type.
เงื่อนไขที่ทำให้ bot ทำงาน เช่น คีย์เวิร์ดหรือประเภทเหตุการณ์
🏷️ User ID
รหัสผู้ใช้
A unique code that identifies each LINE user. Your bot uses it to send personal messages.
รหัสเฉพาะที่ระบุผู้ใช้ LINE แต่ละคน bot ใช้รหัสนี้ในการส่งข้อความ
🚀 Push Message
ข้อความที่ bot ส่งก่อน
A message your bot sends without being asked — useful for notifications and promotions.
ข้อความที่ bot ส่งออกไปเองโดยไม่รอให้ผู้ใช้ถาม
↩️ Reply Message
การตอบกลับข้อความ
A message your bot sends in response to a user's message. Uses the reply token.
ข้อความที่ bot ส่งตอบกลับข้อความของผู้ใช้ ใช้ reply token
🔄 Follow Event
เหตุการณ์ติดตาม
Triggered when someone adds your bot as a friend. Great for sending a welcome message!
เกิดขึ้นเมื่อมีคนเพิ่ม bot เป็นเพื่อน เหมาะสำหรับส่งข้อความต้อนรับ
🌐 LIFF
แอปเว็บใน LINE
LINE Front-end Framework — lets you build web apps that run inside the LINE app.
เฟรมเวิร์กที่ทำให้สร้างเว็บแอปที่ทำงานภายใน LINE ได้

🔧 เริ่มต้นใช้งาน
Getting Started

🇹🇭 สิ่งที่ต้องเตรียม

  • บัญชี LINE (แอปปกติ)
  • บัญชี LINE Business (ฟรี)
  • Python 3 หรือ Node.js
  • ngrok (สำหรับทดสอบในเครื่อง)
  • Code editor เช่น VS Code

🇬🇧 What You Need

  • A LINE account (regular app)
  • A LINE Business account (free)
  • Python 3 or Node.js
  • ngrok (to test locally)
  • A code editor like VS Code

1สร้าง Provider และ Channel — Create a Provider and Channel

ไปที่ developers.line.biz แล้วล็อกอินด้วยบัญชี LINE ของคุณ กด "Create a New Provider" แล้วตั้งชื่อ (เช่นชื่อของคุณ) จากนั้นกด "Create a Messaging API channel"
English: Go to developers.line.biz and log in with your LINE account. Click "Create a New Provider" and enter a name (like your name). Then click "Create a Messaging API channel."

2ตั้งค่า Channel — Configure Your Channel

กรอกข้อมูล channel: ชื่อ คำอธิบาย และหมวดหมู่ ตั้งค่า "Response settings" เป็น "Messaging API" เพื่อปิดการตอบกลับอัตโนมัติของ LINE
English: Fill in the channel details: name, description, and category. Set "Response settings" to use "Messaging API" only — this disables LINE's default auto-reply so your bot can control responses.

3รับ Channel Access Token — Get Your Access Token

ใน tab "Messaging API" เลื่อนลงไปที่ "Channel access token" แล้วกด "Issue" คัดลอก token นี้ไว้ — นี่คือ "กุญแจ" ของ bot คุณ เก็บไว้เป็นความลับ!
English: In the "Messaging API" tab, scroll to "Channel access token" and click "Issue." Copy this token — it is the key for your bot. Keep it secret!

⚠️ สำคัญมาก! / Very Important!

Thai: อย่าแชร์ Channel Access Token ของคุณกับใคร เหมือนกับรหัสผ่าน!

English: Never share your Channel Access Token. Treat it like a password!

4ติดตั้ง Python Library — Install Python Library

เปิด terminal แล้วรันคำสั่งนี้เพื่อติดตั้ง LINE SDK สำหรับ Python
English: Open your terminal and run this command to install the LINE SDK for Python.
pip install line-bot-sdk flask

5ติดตั้ง ngrok — Install ngrok

ngrok ทำให้เซิร์ฟเวอร์ในคอมพิวเตอร์ของคุณเข้าถึงได้จากอินเทอร์เน็ต LINE จะส่ง webhook ไปที่ URL ของ ngrok
English: ngrok creates a public URL for your local server so LINE can send webhook events to your computer.
# 1. Download ngrok from ngrok.com # 2. Run in a separate terminal window: ngrok http 5000 # Copy the https://xxxxx.ngrok.io URL

6ตั้งค่า Webhook URL — Set Your Webhook URL

กลับไปที่ LINE Developers Console → Messaging API tab → "Webhook URL" แล้วใส่ URL ของ ngrok ตามด้วย "/callback" จากนั้นกด "Verify"
English: Go back to LINE Developers Console → Messaging API tab → "Webhook URL." Enter your ngrok URL followed by "/callback" then click "Verify."
# Webhook URL format: https://abc123.ngrok.io/callback

🤖 โปรเจกต์แรก: Echo Bot
First Project: Echo Bot

🇹🇭 Echo Bot คืออะไร?

Echo Bot คือ bot ที่ง่ายที่สุด — มันรับข้อความจากผู้ใช้แล้วส่งข้อความเดิมกลับไป เหมือน "เสียงสะท้อน" นี่คือจุดเริ่มต้นที่ดีที่สุดในการเรียนรู้ LINE Chatbot!

🇬🇧 What is an Echo Bot?

An Echo Bot is the simplest bot — it receives a message from the user and sends the same message back, like an echo. It is the perfect starting point for learning LINE Chatbot development!

# echo_bot.py — Your First LINE Chatbot! # bot แรกของคุณ — ส่งกลับข้อความเดิม from flask import Flask, request, abort from linebot import LineBotApi, WebhookHandler from linebot.exceptions import InvalidSignatureError from linebot.models import MessageEvent, TextMessage, TextSendMessage app = Flask(__name__) # ใส่ token ของคุณที่นี่ / Put your tokens here LINE_TOKEN = 'YOUR_CHANNEL_ACCESS_TOKEN' LINE_SECRET = 'YOUR_CHANNEL_SECRET' line_bot_api = LineBotApi(LINE_TOKEN) handler = WebhookHandler(LINE_SECRET) # รับข้อมูลจาก LINE / Receive data from LINE @app.route('/callback', methods=['POST']) def callback(): signature = request.headers['X-Line-Signature'] body = request.get_data(as_text=True) try: handler.handle(body, signature) except InvalidSignatureError: abort(400) return 'OK' # เมื่อรับข้อความ → ส่งกลับเหมือนเดิม # When a message is received → echo it back @handler.add(MessageEvent, message=TextMessage) def handle_message(event): user_message = event.message.text # ข้อความจากผู้ใช้ line_bot_api.reply_message( event.reply_token, # reply token จาก event TextSendMessage(text=user_message) # ส่งกลับ! ) if __name__ == '__main__': app.run(port=5000)

1รันโปรแกรม — Run the Program

บันทึกไฟล์เป็น echo_bot.py จากนั้นรันใน terminal
English: Save the file as echo_bot.py then run it in the terminal.
python echo_bot.py

2ทดสอบ Bot ของคุณ — Test Your Bot

เปิด LINE และค้นหา bot ของคุณ (ใน LINE Developers Console จะมี QR code) เพิ่มเป็นเพื่อน แล้วส่งข้อความ!
English: Open LINE and find your bot (there is a QR code in the LINE Developers Console). Add it as a friend and send a message!

💡 สิ่งที่เกิดขึ้น / What is happening?

ไทย: เมื่อคุณส่งข้อความ → LINE ส่ง event ไปที่ webhook → Flask รับ request → Python อ่านข้อความ → ส่งข้อความเดิมกลับผ่าน LINE API

English: When you send a message → LINE sends an event to the webhook → Flask receives the request → Python reads the message → sends it back through the LINE API

🎉 ยินดีด้วย! / Congratulations!

คุณสร้าง LINE Chatbot ตัวแรกของคุณแล้ว!

You just built your first LINE Chatbot! Your bot is now listening for messages and replying automatically. 🤖

🚀 ก้าวต่อไป
Next Steps

🇹🇭 คุณได้เรียนรู้

  • LINE Chatbot คืออะไรและทำงานอย่างไร
  • คำศัพท์สำคัญ: webhook, token, event, payload
  • สร้าง channel ใน LINE Developers Console
  • เขียน Echo Bot ด้วย Python + Flask
  • เชื่อม ngrok เข้ากับ LINE

🇬🇧 You Have Learned

  • What a LINE Chatbot is and how it works
  • Key vocabulary: webhook, token, event, payload
  • Creating a channel in LINE Developers Console
  • Writing an Echo Bot with Python + Flask
  • Connecting ngrok to LINE

🚀 In the Activities Page You Will Build — ในหน้า Activities คุณจะสร้าง:

  • 🔑 Keyword Response Bot — ตอบตามคีย์เวิร์ด (if "สวัสดี" → "สวัสดีครับ!")
  • 👋 Welcome Message Bot — ส่งข้อความต้อนรับเมื่อมีคนติดตาม
  • 🎨 Flex Message Bot — ส่งการ์ดสวยงามพร้อมรูปและปุ่ม
🚀 Go to Activities / ไปหน้า Activities → 📝 Take the Exam / ทำข้อสอบ