chitanok/line-api-laravel
Laravel package for building simple LINE Messaging API bots. Provides a service provider and basic setup to send and receive LINE messages and create a lightweight bot for notifications or customer interactions.
Installation
composer require chitanok/line-api-laravel
Publish the config file:
php artisan vendor:publish --provider="Chitanok\LineApiLaravel\LineServiceProvider" --tag="config"
Configuration
Edit .env with your LINE Developer credentials:
LINE_CHANNEL_SECRET=your_channel_secret
LINE_CHANNEL_ACCESS_TOKEN=your_channel_access_token
LINE_BOT_ID=your_bot_id
LINE_BOT_SECRET=your_bot_secret
First Use Case: Webhook Setup
Add the middleware to routes/web.php:
Route::post('/line-webhook', [LineWebhookController::class, 'handle']);
Register the webhook URL in the LINE Developers Console.
Sending a Simple Message
use Chitanok\LineApiLaravel\Facades\Line;
Line::sendMessage('User ID', 'Hello from Laravel!');
Handling Incoming Events
Extend LineWebhookController to process events:
public function handle(Request $request)
{
$events = Line::parseEvents($request->getContent());
foreach ($events as $event) {
if ($event->type === 'message') {
$this->handleMessage($event);
}
}
}
Rich Message Responses Use built-in message types:
Line::sendMessage('user123', [
'type' => 'flex',
'altText' => 'Flex Message',
'contents' => ['type' => 'bubble', ...],
]);
User Management Fetch user profiles:
$user = Line::getProfile('user123');
Broadcasting to Groups
Line::sendMessage('group123', 'Hello group!');
LineAuthMiddleware to validate requests in protected routes.line.event for global event handling:
event(new LineEvent($event));
LineMessageJob for async processing:
dispatch(new LineMessageJob('user123', 'Delayed message'));
Webhook Verification
X-Line-Signature in production:
if (!Line::validateSignature($request->header('X-Line-Signature'), $request->getContent())) {
abort(401);
}
LINE_DEBUG=true in .env.Rate Limits
Message Failures
Line::sendMessage()’s options:
Line::sendMessage('user123', 'Message', ['retry' => 3]);
Flex Message Bubble Validation
.env:
LINE_DEBUG=true
\Log::debug('LINE Event', ['event' => $event->toArray()]);
Custom Event Handlers
Override LineEvent to add custom logic:
class CustomLineEvent extends LineEvent {
public function handle()
{
if ($this->type === 'sticker') {
// Custom sticker logic
}
}
}
Provider Extensions
Bind custom services in AppServiceProvider:
$this->app->bind('line.custom', function () {
return new CustomLineService();
});
Template Messages
Use Line::sendTemplateMessage() for pre-designed templates (e.g., confirmations):
Line::sendTemplateMessage('user123', 'template_id', ['key' => 'value']);
How can I help you explore Laravel packages today?