When to use this skill
Use this skill when you need to:
-
Develop or modify LINE Bot webhook handlers
-
Implement new bot commands and message responses
-
Handle LINE events (messages, join/leave groups)
-
Integrate LINE Messaging API features
-
Debug LINE Bot webhook issues
-
Test bot functionality in LINE groups
How to use it
Core Components
- LINE Bot SDK Setup
Use the LINE Bot SDK v3 for Python:
from linebot.v3.messaging import MessagingApi, Configuration, ApiClient from linebot.v3.webhook import WebhookHandler, MessageEvent from linebot.v3.messaging.models import PushMessageRequest, TextMessage from linebot.v3.webhooks import TextMessageContent, JoinEvent, LeaveEvent
- Environment Configuration
Required environment variables:
-
LINE_CHANNEL_ACCESS_TOKEN
-
Bot access token from LINE Developers Console
-
LINE_CHANNEL_SECRET
-
Channel secret for webhook verification
-
LINE_GROUP_ID (optional) - Comma-separated group IDs for broadcasting
- Webhook Handler Pattern
@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 Exception as e:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessageContent) def handle_message(event): # Process user messages pass
@handler.add(JoinEvent) def handle_join(event): # Handle bot joining a group pass
@handler.add(LeaveEvent) def handle_leave(event): # Handle bot leaving a group pass
- Message Sending
Reply to user message
messaging_api.reply_message( ReplyMessageRequest( reply_token=event.reply_token, messages=[TextMessage(text="Response text")] ) )
Push message to group
messaging_api.push_message( PushMessageRequest( to=group_id, messages=[TextMessage(text="Broadcast message")] ) )
- Command Pattern
Implement commands with @ prefix:
def handle_message(event): text = event.message.text.strip()
if text.startswith('@'):
parts = text[1:].split()
command = parts[0].lower()
args = parts[1:] if len(parts) > 1 else []
if command == 'help':
# Show help
elif command == 'schedule':
# Show schedule
# ... more commands
Best Practices
Group Context Awareness
-
Always extract group_id from events
-
Store per-group configurations separately
-
Validate group permissions before operations
Error Handling
-
Wrap webhook handlers in try-except blocks
-
Log errors for debugging
-
Send user-friendly error messages back to LINE
Message Formatting
-
Use emojis to enhance UX (🗑️, 📅, 👥, etc.)
-
Keep messages concise and scannable
-
Use line breaks for readability
Testing
-
Test commands in a dedicated LINE group
-
Verify webhook signature validation
-
Check response times (should be quick)
Common Patterns
Multi-Group Support
Get group ID from event
group_id = event.source.group_id if hasattr(event.source, 'group_id') else None
Load group-specific data
group_data = groups.get(group_id, {})
Scheduled Broadcasting
from apscheduler.schedulers.background import BackgroundScheduler
def send_reminder(group_id): # Get group-specific schedule # Send reminder message pass
scheduler = BackgroundScheduler() scheduler.start()
Reference Links
-
LINE Developers Console
-
LINE Messaging API Documentation
-
LINE Bot SDK for Python