Compare v1 and v2 Webhook handling
masterWebhook handling has changed significantly between versions:
- v1: Used
Line::Bot::Client#validate_signatureandLine::Bot::Client#parse_events_fromto handle incoming requests. - v2: Uses a dedicated
Line::Bot::V2::WebhookParser. The parser handles signature validation and returns structured event objects. You should rescueLine::Bot::V2::WebhookParser::InvalidSignatureErrorto handle invalid requests.
# v2 Webhook Implementation Example
require 'line-bot-api'
def client
@client ||= Line::Bot::V2::MessagingApi::ApiClient.new(
channel_access_token: ENV.fetch("LINE_CHANNEL_ACCESS_TOKEN")
)
end
def parser
@parser ||= Line::Bot::V2::WebhookParser.new(channel_secret: ENV.fetch("LINE_CHANNEL_SECRET"))
end
post '/callback' do
body = request.body.read
signature = request.env['HTTP_X_LINE_SIGNATURE']
begin
events = parser.parse(body: body, signature: signature)
rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
end
events.each do |event|
# Handle events (e.g., Line::Bot::V2::Webhook::MessageEvent)
end
"OK"
end