การใช้งาน Line Notify ร่วมกับ PHP & C#

การสร้าง Token เพื่อใช้ในการส่งข้อมูล

  1. เข้าสู่ระบบที่ https://notify-bot.line.me  ด้วย Account Line
  2. คลิกที่ My Page และเลือก Generate access token (For developers)
  3. ตั้งชื่อ Token และ เลือกกลุ่มที่ต้องการสร้าง Token
  4. เมื่อได้ Token แล้ว ให้ทำการ Copy ไว้ เนื่องจากระบบ จะไม่แสดง Token นี้ให้เห็นอีกครั้ง จากนั้นในไลน์ จะเห็น line notify เพิ่มขึ้นมาเป็นเพื่อนใหม่ ให้ทำการ invite เข้า ไลน์กลุ่มที่ต้องการ

การเขียนโปรแกรม เพื่อส่งข้อมูลให้กับ Line notification

ตัวอย่างการเขียนโปรแกรมภาษา PHP เพื่อส่งข้อความ

<?php

define('LINE_API',"https://notify-api.line.me/api/notify");

$token = "Un2LudBajEKlJs8I7qbWIMkl3f2WjGH56YuDJ9xxxxx"; // ใส่Token ที่copy เอาไว้

$str = "สวัสดี"; //ข้อความที่ต้องการส่ง สูงสุด 1000 ตัวอักษร

 

$res = notify_message($str,$token);

print_r($res);

 

function notify_message($message,$token){

$queryData = array('message' => $message);

$queryData = http_build_query($queryData,'','&');

$headerOptions = array(

'http'=>array(

'method'=>'POST',

'header'=> "Content-Type: application/x-www-form-urlencoded\r\n"

."Authorization: Bearer ".$token."\r\n"

."Content-Length: ".strlen($queryData)."\r\n",

'content' => $queryData

),

);

$context = stream_context_create($headerOptions);

$result = file_get_contents(LINE_API,FALSE,$context);

$res = json_decode($result);

return $res;

}

?>

ผลลัพธ์

 

 

 

ตัวอย่างการเขียนโปรแกรมภาษา PHP เพื่อส่งข้อความ และ สติกเกอร์

<?php

define('LINE_API',"https://notify-api.line.me/api/notify");

$token = "Un2LudBajEKlJs8I7qbWIMkl3f2WjGH56YuDJ9xxxxx"; // ใส่Token ที่copy เอาไว้

$str = "สวัสดี"; //ข้อความที่ต้องการส่ง สูงสุด 1000 ตัวอักษร

$stickerPkg = 2; //stickerPackageId

$stickerId = 34; //stickerId

 

$res = notify_messageAndSticker($str,$stickerPkg,$stickerId,$token);

print_r($res);

 

function notify_messageAndSticker($message,$stickerPkg,$stickerId,$token){

$queryData = array(

'message' => $message,

'stickerPackageId'=>$stickerPkg,

'stickerId'=>$stickerId

);

$queryData = http_build_query($queryData,'','&');

$headerOptions = array(

'http'=>array(

'method'=>'POST',

'header'=> "Content-Type: application/x-www-form-urlencoded\r\n"

."Authorization: Bearer ".$token."\r\n"

."Content-Length: ".strlen($queryData)."\r\n",

'content' => $queryData

),

);

$context = stream_context_create($headerOptions);

$result = file_get_contents(LINE_API,FALSE,$context);

$res = json_decode($result);

return $res;

}?>

ผลลัพธ์

 

 

 

 

 

 

ตัวอย่างการเขียนโปรแกรมภาษา PHP เพื่อส่งข้อความ และ รูปภาพ

<?php

define('LINE_API',"https://notify-api.line.me/api/notify");

$token = "Un2LudBajEKlJs8I7qbWIMkl3f2WjGH56YuDJ9xxxxx"; // ใส่Token ที่copy เอาไว้

$str = "สวัสดี"; //ข้อความที่ต้องการส่ง สูงสุด 1000 ตัวอักษร

$imageThumbnail = "https://f.ptcdn.info/345/051/000/oqd15uejqXy8Zr3LXWj-o.jpg"; //ภาพขนาดย่อ

$imageFullsize = "https://f.ptcdn.info/345/051/000/oqd15uejqXy8Zr3LXWj-o.jpg"; // ภาพขนาดเต็ม

 

$res = notify_image($str,$imageThumbnail,$imageFullsize,$token);

print_r($res);

 

function notify_image($message,$imageThumbnail,$imageFullsize,$token){

$queryData = array(

'message' => $message,

'imageThumbnail'=>$imageThumbnail,

'imageFullsize'=>$imageFullsize

);

$queryData = http_build_query($queryData,'','&');

$headerOptions = array(

'http'=>array(

'method'=>'POST',

'header'=> "Content-Type: application/x-www-form-urlencoded\r\n"

."Authorization: Bearer ".$token."\r\n"

."Content-Length: ".strlen($queryData)."\r\n",

'content' => $queryData

),

);

$context = stream_context_create($headerOptions);

$result = file_get_contents(LINE_API,FALSE,$context);

$res = json_decode($result);

return $res;

}?>

ผลลัพธ์

 

 

 

 

 

 

 

 

ตัวอย่างการเขียนโปรแกรมภาษา C# 

var request = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
var postData = string.Format("message={0}","สวัสดี");
var data = Encoding.UTF8.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

request.Headers.Add("Authorization", "Bearer Un2LudBajEKlJs8I7qbWIMkl3f2WjGH56YuDJ9xxxxx");

using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

 

Parameters ที่ใช้ หากต้องการส่งรูปภาพ และ Sticker ใน Function

จากรูปจะเห็นได้ว่า นอกจากมี parameter ที่เป็น message แล้วยังมี parameter ให้เลือกใช้ทั้ง image และ sticker ซึ่ง message มีสถานะเป็น Required และที่เหลือเป็น Optional จึงเป็นการบังคับว่า LINE Notify ต้องมี message ไปด้วยเสมอ

 

Reference

LINE Notify API Document

https://notify-bot.line.me/doc/en/

 

ส่วนประกอบของ sticker ประกอบด้วย stickerPackageId และ stickerId

ดูได้จาก Sticker List Document

https://devdocs.line.me/files/sticker_list.pdf