Creating a LINE Chatbot Using Laravel

Creating a LINE Chatbot involves several steps, including setting up a LINE Developer account, creating a Messaging API bot, and developing the bot using a backend framework like Laravel, Node.js, or Python. Here’s how you can do it step by step:


1. Create a LINE Developer Account

  1. Go to LINE Developers Console
  2. Sign in with your LINE account.
  3. Create a Provider (a group for your chatbots and services).

2. Create a LINE Messaging API Bot

  1. In the LINE Developers Console, click “Create a new channel” under the Messaging API.
  2. Fill in details such as:
    • App name
    • Profile image
    • Description
  3. Enable “Use Webhook” (important for receiving messages).
  4. Copy these credentials from the “Basic settings” tab:
    • Channel Secret
    • Channel Access Token (Click “Issue” to generate it)

3. Setup Webhook & Backend (Laravel Example)

You need a backend server to process messages. You can use Laravel:

Install Laravel & LINE SDK

composer create-project --prefer-dist laravel/laravel line-bot
cd line-bot
composer require linecorp/line-bot-sdk

Configure .env

Add your LINE credentials:

LINE_ACCESS_TOKEN=your_channel_access_token
LINE_SECRET=your_channel_secret

Create a Controller

Run:

php artisan make:controller LineBotController

Edit app/Http/Controllers/LineBotController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use LINE\LINEBot;
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot\MessageBuilder\TextMessageBuilder;

class LineBotController extends Controller
{
    private $bot;

    public function __construct()
    {
        $httpClient = new CurlHTTPClient(env('LINE_ACCESS_TOKEN'));
        $this->bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_SECRET')]);
    }

    public function webhook(Request $request)
    {
        $events = $request->all();

        foreach ($events['events'] as $event) {
            $replyToken = $event['replyToken'];
            $message = new TextMessageBuilder("Hello! You said: " . $event['message']['text']);
            $this->bot->replyMessage($replyToken, $message);
        }

        return response()->json(['status' => 'ok']);
    }
}

Setup Routes

Edit routes/web.php:

use App\Http\Controllers\LineBotController;

Route::post('/webhook', [LineBotController::class, 'webhook']);

4. Deploy Your Laravel App

  • Deploy on VPS, AWS, or Cloud hosting (Make sure your app is accessible online).
  • Use Ngrok if testing locally: ngrok http 8000 Copy the public URL and set it as your Webhook URL.

5. Set Webhook URL in LINE Console

  1. Go to Messaging API settings in LINE Developer Console.
  2. Set Webhook URL to: https://your-domain.com/webhook
  3. Click “Verify” (It should say “Success”).

6. Test Your Bot

  1. Add your bot as a friend in LINE.
  2. Send a message like “Hello”.
  3. Your bot should reply with “Hello! You said: Hello”.

Next Steps

  • Improve your bot with rich messages, carousels, and buttons.
  • Integrate AI/NLP for smart replies.
  • Store chat logs in a database.

Leave a Comment