Hey there, fellow developer! Ready to dive into the world of Line API integration? You're in for a treat. Line's messaging platform is a powerhouse, and with the linecorp/line-bot-sdk
package, we'll have your PHP app chatting away in no time.
Before we jump in, make sure you've got:
Got all that? Great! Let's roll.
First things first, let's get that SDK installed:
composer require linecorp/line-bot-sdk
Easy peasy, right?
Now, let's set up our bot client. Grab your channel access token and secret from the Line Developer Console and let's get configuring:
use LINE\LINEBot; use LINE\LINEBot\HTTPClient\CurlHTTPClient; $httpClient = new CurlHTTPClient('YOUR_CHANNEL_ACCESS_TOKEN'); $bot = new LINEBot($httpClient, ['channelSecret' => 'YOUR_CHANNEL_SECRET']);
Time to create our webhook endpoint. This is where Line will send all those juicy incoming messages:
$signature = $_SERVER['HTTP_X_LINE_SIGNATURE']; $body = file_get_contents('php://input'); try { $events = $bot->parseEventRequest($body, $signature); } catch(\Exception $e) { error_log('Parse event error: ' . $e->getMessage()); http_response_code(500); exit(); }
Don't forget to set this URL in your Line Developer Console!
Let's handle those incoming messages like a pro:
foreach ($events as $event) { if ($event instanceof \LINE\LINEBot\Event\MessageEvent\TextMessage) { $replyToken = $event->getReplyToken(); $text = $event->getText(); // Your logic here } }
Alright, time for the fun part - talking back!
// Text message $bot->replyText($replyToken, 'Hello, human!'); // Image message $bot->replyImage($replyToken, 'https://example.com/image.jpg', 'https://example.com/preview.jpg'); // Button template $buttonTemplateBuilder = new \LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder( 'My Button Template', 'Please select', 'https://example.com/thumbnail.jpg', [ new \LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder('Yes', 'yes'), new \LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder('No', 'no'), ] ); $templateMessage = new \LINE\LINEBot\MessageBuilder\TemplateMessageBuilder('Button Template', $buttonTemplateBuilder); $bot->replyMessage($replyToken, $templateMessage);
Want to kick it up a notch? Try these:
// Get user profile $userId = $event->getUserId(); $profile = $bot->getProfile($userId)->getJSONDecodedBody(); // Create a rich menu $richMenuBuilder = new \LINE\LINEBot\RichMenuBuilder(...); $richMenuId = $bot->createRichMenu($richMenuBuilder)->getJSONDecodedBody()['richMenuId']; // Add quick replies $quickReply = new \LINE\LINEBot\MessageBuilder\QuickReplyBuilder([ new \LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder('Yes', 'yes'), new \LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder('No', 'no'), ]); $textMessageBuilder = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder('Choose:', $quickReply);
Always be prepared:
try { $response = $bot->replyText($replyToken, 'Hello!'); } catch(\Exception $e) { error_log('Failed to send message: ' . $e->getMessage()); return false; } if (!$response->isSucceeded()) { error_log('Failed to send message: ' . $response->getRawBody()); }
Use the Line Official Account Manager to test your bot. If things go sideways, check your error logs and make sure your webhook URL is correct and accessible.
Remember, Line requires HTTPS for webhooks. Make sure your server is configured correctly and your SSL certificate is valid.
And there you have it! You're now armed and ready to create some awesome Line integrations. The world of chatbots awaits you, my friend. Go forth and code!
For more in-depth info, check out the Line Messaging API documentation and the linecorp/line-bot-sdk GitHub repo.
Happy coding!