ส่ง Notification ผ่าน Line Notify ด้วย PHP
วันนี้จะมาแนะนำการส่ง Notification ผ่าน Line Notify ด้วยภาษา PHP กันครับ
โดย Line Notify นั้นมีความสามารถดังนี้
- ส่ง Notification มายังตัวเราเอง
- ส่ง Notification ไปยัง Line Group ที่เราอยู่ (เราต้องเพิ่ม Line Notify เข้ามาใน Line Group นั้นด้วย)
- ส่ง ข้อความ Text, รูปภาพ และ Sticker ได้
Add Line Notify เป็นเพื่อนเราก่อน
ก่อนอื่นเราต้องแอด Line Notify มาเป็นเพื่อนเราก่อน โดยค้นหาคำว่า Line Notify แล้วแอดเป็นเพื่อนดังภาพ
สร้าง Token สำหรับการ Notify
- ให้เรา Login เข้าในเวป https://notify-bot.line.me
- เมื่อ Login เสร็จเรียบร้อย ให้เข้า https://notify-bot.line.me/my/
- เลื่อนลงมาด้านล่างคลิกที่ Generate Token
![]() |
คลิก Generate token |
- ให้เราตั้งชื่อ Notify (ชื่อนี้จะไปแสดงตอนส่งข้อความ)
![]() |
เลือกตัวเราเองหรือ Line Group ที่ต้องการ |
1-on-1 chat with LINE Notify คือจะส่งข้อความมาหาตัวเราเอง
นอกนั้นจะเป็น List ของ Line Group ที่เราอยู่ หากต้องการให้ส่งเข้า Line Group เราต้อง Invite Line Notify เข้าไปใน Line Group นั้นด้วย
- คลิก Generate token เราก็จะได้ Token เอาไว้ในการส่ง Notify ให้เราเซฟไว้ให้ดี เพราะเราจะเห็น Token อันนี้เพียงครั้งเดียว
![]() |
Token จะแสดงใน Popup นี้เพียงครั้งเดียวเท่านั้น |
ส่ง Notification ด้วย PHP
ในที่นี้เราจะใช้ PHP ในการส่ง Notification กันนะครับ หากต้องการส่งด้วยภาษาอื่นสามารถดู API ได้ที่เอกสารนี้ https://notify-bot.line.me/doc/en/
โดยผมได้สร้าง PHP Library ง่ายๆ สำหรับที่ใช้ในการส่ง Line Notify ไว้ที่ https://github.com/kittinan/php-line-notify โดยมี Requirement ดังนี้
- PHP 5.5+
เริ่มกันเลย
- โหลด Library ผ่าน composer
composer require kittinan/php-line-notify
- ทดสอบส่ง Text Message ตาม Code ด้านล่าง
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
require_once './vendor/autoload.php';
$token = 'LINE_NOTIFY_TOKEN';
$ln = new KS\Line\LineNotify($token);
$text = 'สวัสดี Line Notify';
$ln->send($text);
- ข้อความก็จะถูกส่งไปยังตัวเราหรือ Line Group ที่เรา Generate Token มา
- ทดลองส่งรูปภาพ ตาม Code ด้านล่าง
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
require_once './vendor/autoload.php';
$token = 'LINE_NOTIFY_TOKEN';
$ln = new KS\Line\LineNotify($token);
$text = ' '; // Line Notify บังคับให้ใส่ข้อความ แต่อยากส่งแแต่รูปภาพเลยใส่ space ไว้
$image_path = '/tmp/test_line.jpg'; //Line notify allow only jpeg and png file
$ln->send($text, $image_path);
- รูปภาพก็จะถูกส่งไปยังตัวเราหรือ Line Group ที่เรา Generate Token มา
จบแล้วค้าบบ
ปล. ไว้มีเวลาจะอัพเดท Library ให้สามารถส่ง Sticker ได้นะครับ
ปล. API Line Notify มี Rate Limit อยู่นะครับ โดย Rate Limit จะอยู่ที่ 1,000 ต่อชั่วโมง
32 comments: