Showing posts with label line Bot. Show all posts
Showing posts with label line Bot. Show all posts

10/27/2020

Pi : Pi moition camera notification Line Nofity API

 Pi : Pi moition camera notification Line Nofity API
ติดตั้ง motion
https://intranet.sci.com/blog.php?u=281&b=1667

เพิ่มเติม แก้ Motion ให้ไปเรียกใช้งาน ไฟล์ python ที่สร้างขึ้นเมื่อมีการสร้าง Video

  1. nana /etc/motion/motion.conf

แก้ตรง
on_movie_start python3 /home/pi/line.py


Code pyton ส่ง Line
http://intranet.sci.com/blog.php?u=281&b=1809

Code สำเร็จ
  1. import requests, json
  2. import urllib.parse
  3. import sys
  4.  
  5. import glob
  6. import os
  7. import time
  8.  
  9. LINE_ACCESS_TOKEN = "XXXXXXX"
  10.  
  11. URL_LINE = "https://notify-api.line.me/api/notify"
  12.  
  13. def line_text(message):
  14.     msg = urllib.parse.urlencode({"message":message})
  15.     LINE_HEADERS = {'Content-Type':'application/x-www-form-urlencoded',"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
  16.     session = requests.Session()
  17.     session_post = session.post(URL_LINE, headers=LINE_HEADERS, data=msg)
  18.     print(session_post.text)
  19.  
  20. def line_pic(message, path_file):
  21.     file_img = {'imageFile': open(path_file, 'rb')}
  22.     msg = ({'message': message})
  23.     LINE_HEADERS = {"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
  24.     session = requests.Session()
  25.     session_post = session.post(URL_LINE, headers=LINE_HEADERS, files=file_img, data=msg)
  26.     print(session_post.text)
  27.  
  28. list_of_files = glob.glob('/home/pi/Monitor/*.jpg')
  29. latest_file = max(list_of_files, key=os.path.getctime)
  30. #print(latest_file)
  31. text_send = "Motion Detect OD."
  32. line_pic(text_send, latest_file)
  33.  
  34. #//// Delete All File In Folder Monitor ////#
  35. parth = "/home/pi/Monitor/"
  36. for i in os.listdir ( parth ):
  37.     os.remove(parth+i)

ส่งข้อความเข้า Line Bot ด้วย Sell Script ส่งหา UID ของแต่ละ User ทีละหลาย ๆ User

 ส่งข้อความเข้า Line Bot ด้วย Sell Script ส่งหา UID ของแต่ละ User ทีละหลาย ๆ User
XXX : Token Line Bot
UID = User ID Line
Code

  1. #!/bin/sh
  2. curl -v -X POST https://api.line.me/v2/bot/message/multicast \
  3. -H 'Content-Type: application/json' \
  4. -H 'Authorization: Bearer {XXX}' \
  5. -d '{
  6.     "to": ["UID","UID2","UID2"],
  7.     "messages":[
  8.         {
  9.             "type":"text",
  10.             "text":"Hello, world1"
  11.         },
  12.         {
  13.             "type":"text",
  14.             "text":"Hello, world2"
  15.         }
  16.     ]
  17. }'

Line : Python ส่งรูปเข้า Line API

 Line : Python ส่งรูปเข้า Line API
โปรแกรมที่ต้องใช้ ในเครื่อง rasberry pi
- python 3 ขึ้นไป
- ติดตั้ง

  1. pip install requests

- ติดตั้ง pip
  1. wget "https://bootstrap.pypa.io/get-pip.py"
  2. sudo python get-pip.py


1. เปิดใช้งาน Token ที่ https://notify-bot.line.me/
สามารถทำเป็น User หรือ ทำเป็น Group ก็ได้

*** สำคัญต้องเอา LINE Notify เข้าไปใน Group ด้วย ถึงจะส่งข้อความได้ ***

2. สร้าง Code python line.py
XXXX คือ Token ที่ได้จากข้อ 1.
Code
  1. import requests, json
  2. import urllib.parse
  3. import sys
  4.  
  5. LINE_ACCESS_TOKEN = "XXXX" #Use Token Or Group Token https://notify-bot.line.me/
  6.  
  7. URL_LINE = "https://notify-api.line.me/api/notify"
  8.  
  9. def line_text(message):
  10.     msg = urllib.parse.urlencode({"message":message})
  11.     LINE_HEADERS = {'Content-Type':'application/x-www-form-urlencoded',"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
  12.     session = requests.Session()
  13.     session_post = session.post(URL_LINE, headers=LINE_HEADERS, data=msg)
  14.     print(session_post.text)
  15.  
  16. def line_pic(message, path_file):
  17.     file_img = {'imageFile': open(path_file, 'rb')}
  18.     msg = ({'message': message})
  19.     LINE_HEADERS = {"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
  20.     session = requests.Session()
  21.     session_post = session.post(URL_LINE, headers=LINE_HEADERS, files=file_img, data=msg)
  22.     print(session_post.text)
  23.  
  24. if __name__ == "__main__":
  25.     if len(sys.argv) < 3:
  26.         # <Linux>
  27.         # python line.py "Test"
  28.         line_text(sys.argv[1])
  29.     else:
  30.         # <Linux>
  31.         # python line.py "Test" "/home/pi/test.jpg"
  32.         line_pic(sys.argv[1], sys.argv[2])


3. ทดลองส่งข้อความ
  1. python3 line_group.py "พบการเคลื่อนไหว"

จะมีข้อความแจ้ง
{"status":200,"message":"ok"}

และมีข้อความส่งไป Line

4. ส่งรูปใช้คำสั่ง
  1. python3 line.py "พบการเคลื่อนไหว" "/home/pi/3923-7.jpg"


ส่วนตัว Line Bot จะส่งได้เฉพาะรูปที่ผ่าน Link และต้องเป็น https เมื่อรูปถูกลบหรือเปลี่ยนชื่อ Line จะไม่สามารถแสดงผลรูป
ข้อจำกับ Line notify ส่งได้เดือนละ 1000 ครั้ง
https://notify-bot.line.me/doc/en/

ตัวอย่าง
https://maker.goisgo.net/raspberry-pi-w ... ne-notice/
https://medium.com/@dome.soda125/%E0%B8 ... 8ce98f0bd6
https://medium.com/dolab/blog-7-line-no ... 9724796428
https://engineering.linecorp.com/en/blo ... ad-images/

9/29/2020

Line : Python ส่งรูปเข้า Line API Notify

 Line : Python ส่งรูปเข้า Line API Notify
โปรแกรมที่ต้องใช้ ในเครื่อง rasberry pi
- python 3 ขึ้นไป
- ติดตั้ง

  1. pip install requests

- ติดตั้ง pip
  1. wget "https://bootstrap.pypa.io/get-pip.py"
  2. sudo python get-pip.py


1. เปิดใช้งาน Token ที่ https://notify-bot.line.me/
สามารถทำเป็น User หรือ ทำเป็น Group ก็ได้

2. สร้าง Code python line.py
XXXX คือ Token ที่ได้จากข้อ 1.
Code
  1. import requests, json
  2. import urllib.parse
  3. import sys
  4.  
  5. LINE_ACCESS_TOKEN = "XXXX" #Use Token Or Group Token https://notify-bot.line.me/
  6.  
  7. URL_LINE = "https://notify-api.line.me/api/notify"
  8.  
  9. def line_text(message):
  10.     msg = urllib.parse.urlencode({"message":message})
  11.     LINE_HEADERS = {'Content-Type':'application/x-www-form-urlencoded',"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
  12.     session = requests.Session()
  13.     session_post = session.post(URL_LINE, headers=LINE_HEADERS, data=msg)
  14.     print(session_post.text)
  15.  
  16. def line_pic(message, path_file):
  17.     file_img = {'imageFile': open(path_file, 'rb')}
  18.     msg = ({'message': message})
  19.     LINE_HEADERS = {"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
  20.     session = requests.Session()
  21.     session_post = session.post(URL_LINE, headers=LINE_HEADERS, files=file_img, data=msg)
  22.     print(session_post.text)
  23.  
  24. if __name__ == "__main__":
  25.     if len(sys.argv) < 3:
  26.         # <Linux>
  27.         # python line.py "Test"
  28.         line_text(sys.argv[1])
  29.     else:
  30.         # <Linux>
  31.         # python line.py "Test" "/home/pi/test.jpg"
  32.         line_pic(sys.argv[1], sys.argv[2])


3. ทดลองส่งข้อความ
  1. python3 line_group.py "พบการเคลื่อนไหว"

จะมีข้อความแจ้ง
{"status":200,"message":"ok"}

และมีข้อความส่งไป Line

4. ส่งรูปใช้คำสั่ง
  1. python3 line.py "พบการเคลื่อนไหว" "/home/pi/3923-7.jpg"


ส่วนตัว Line Bot จะส่งได้เฉพาะรูปที่ผ่าน Link และต้องเป็น https เมื่อรูปถูกลบหรือเปลี่ยนชื่อ Line จะไม่สามารถแสดงผลรูป
ข้อจำกับ Line notify ส่งได้เดือนละ 1000 ครั้ง
https://notify-bot.line.me/doc/en/

ตัวอย่าง
https://maker.goisgo.net/raspberry-pi-webcam-cctv-line-notice/
https://medium.com/@dome.soda125/%E0%B8%A1%E0%B8%B2%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-line-notification-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-python-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-ab8ce98f0bd6
https://medium.com/dolab/blog-7-line-notify-with-python-8c9724796428
https://engineering.linecorp.com/en/blog/using-line-notify-to-send-stickers-and-upload-images/

 

10/29/2019

Line Bot : Line แบบ Push สามารถทำเขียน php แล้วเรียกใช้ผ่าน Crontab ก็ได้ Run จาก หน้า php

Line Bot : Line แบบ Push สามารถทำเขียน php แล้วเรียกใช้ผ่าน Crontab ก็ได้ Run จาก หน้า php
ไม่ต้องส่ง reply จาก Line
Test Run ผ่านหน้า Web
หรือ ผ่าน Terminal command php
  1. <?php
  2.  
  3. $ACCESS_TOKEN = "XXX";
  4.  
  5.    $content = file_get_contents('php://input');
  6.    $arrayJson = json_decode($content, true);
  7.    $arrayHeader = array();
  8.    $arrayHeader[] = "Content-Type: application/json";
  9.    $arrayHeader[] = "Authorization: Bearer {$ACCESS_TOKEN}";
  10.    //รับข้อความจากผู้ใช้
  11. //   $message = $arrayJson['events'][0]['message']['text'];
  12.    //รับ id ของผู้ใช้
  13. //   $id = $arrayJson['events'][0]['source']['userId'];
  14. //   #ตัวอย่าง Message Type "Text + Sticker"
  15. //   if($message == "สวัสดี"){
  16.  
  17.       $arrayPostData['to'] = 'UID XXXXX'; //Test Suwit
  18.  
  19.       $arrayPostData['messages'][0]['type'] = "text";
  20.       $arrayPostData['messages'][0]['text'] = "สวัสดีจ้าาา";
  21.  
  22.       $arrayPostData['messages'][1]['type'] = "sticker";
  23.       $arrayPostData['messages'][1]['packageId'] = "2";
  24.       $arrayPostData['messages'][1]['stickerId'] = "34";
  25.       pushMsg($arrayHeader, $arrayPostData);
  26. //   }
  27.  
  28.    function pushMsg($arrayHeader,$arrayPostData){
  29.       $strUrl = "https://api.line.me/v2/bot/message/push";
  30.       $ch = curl_init();
  31.       curl_setopt($ch, CURLOPT_URL,$strUrl);
  32.       curl_setopt($ch, CURLOPT_HEADER, false);
  33.       curl_setopt($ch, CURLOPT_POST, true);
  34.       curl_setopt($ch, CURLOPT_HTTPHEADER, $arrayHeader);
  35.       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrayPostData));
  36.       curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  37.       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  38.       $result = curl_exec($ch);
  39.       curl_close ($ch);
  40.    }
  41. ?>

Line Bot : หา User ID ของ Line เพื่อระบุตัวตนว่า ID Line นี้เป็นของใคร

Line Bot : หา User ID ของ Line เพื่อระบุตัวตนว่า ID Line นี้เป็นของใคร
echo ดูในคำสั่ง bot line หรือ Save To logs แล้วค่อยไปบันทึกเอาก็ได้ จะได้รู้ว่า Line ที่พิมพ์มานี้เป็นของใคร หรือ Group นี้ ID อะไร เป็นต้น Add Bot เข้ากลุ่ม แล้วให้คนอื่นลองพิมพ์ ก็จะได้ ID ของ คนนั้นมาเก็บ
  1. $API_URL = 'https://api.line.me/v2/bot/message';
  2. //###### Token Line Bot Register ######//
  3. $accessToken = "XX";//copy Channel access token ตอนที่ตั้งค่ามาใส่
  4.  
  5. $request = file_get_contents('php://input');   // Get request content
  6. $request_array = json_decode($request, true);   // Decode JSON to Array
  7.  
  8. $arrayHeader = array();
  9. $arrayHeader[] = "Content-Type: application/json";
  10. $arrayHeader[] = "Authorization: Bearer {$accessToken}";
  11.  
  12. //########### Find IDUser IDGroup IDRoom From Source ###############//
  13. if ( sizeof($request_array['events']) > 0 ) {
  14.     foreach ($request_array['events'] as $event) {
  15.         $reply_message = '';
  16.         $reply_token = $event['replyToken'];
  17.  
  18.     if(isset($event['source']['userId'])){
  19.         $id = $event['source']['userId'];
  20.     } else if(isset($event['source']['groupId'])){
  21.         $id = $event['source']['groupId'];
  22.     } else if(isset($event['source']['room'])){
  23.         $id = $event['source']['room'];
  24.     }
  25.  
  26.     $text = $event['message']['text'];
  27.         list($flag, $cmd, $parm1) = explode(' ', $text);
  28.         //###Call Function Save Log ###//
  29.         //saveBotLog($text, $id);
  30.         if($flag == "bot:"){        
  31.             //saveBotLog($text, $id);
  32.             if($cmd == "id"){
  33.                 //$output = getFX();
  34.                 $arrayPostData['replyToken'] = $request_array['events'][0]['replyToken'];
  35.                 $arrayPostData['messages'][0]['type'] = "text";
  36.                 $arrayPostData['messages'][0]['text'] = $id;
  37.                 replyMsg($arrayHeader,$arrayPostData);
  38.            }
  39.            // ###### Not Word bot Not reply ######## //
  40.             if(!empty($output)){    
  41.                 $data = [
  42.                 'replyToken' => $reply_token,
  43.                 'messages' => [['type' => 'text', 'text' => $output ]]
  44.                     ];
  45.                     $post_body = json_encode($data, JSON_UNESCAPED_UNICODE);
  46.                     $send_result = send_reply_message($API_URL.'/reply', $POST_HEADER, $post_body);
  47.                         echo "Result: ".$send_result."\r\n";
  48.             }//Close If empty.
  49.         }//Close If bot.
  50.     }//Close For.
  51. }//Close If.

10/28/2019

Line Bot Auto reply message

Line Bot Auto reply message
https://developers.line.me/en/
1. Login Account Line
2. ใช้ Massaging API ตั้งชื่อ App และข้อมูลอื่น ๆ ที่มีให้กรอก
3. เปิดใช้ Bot , Channel access token, Use webhooks
4. Webhook URL SSL เช่น extranet.scivalve.com/file.php
5. Add Line เป็นเพื่อน ใช้ Basic ID หรือ QR Code
6. นำ QR Code ไปใส่ในไฟล์ PHP

$accessToken = "";


สามารถเขียน Code ส่งค่าให้ ตอบ Auto ได้ หรือดึงค่าจาก Web เช่น Rate หรือค่าทอง ส่งรูป ส่งข้อความ และอื่น ๆ ได้

https://medium.com/@nattaponsirikamonnet/%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-bot-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-line-messaging-api-d7de644ac892


PHP Code Test
  1. <?php
  2.     $accessToken = "";//copy Channel access token ตอนที่ตั้งค่ามาใส่
  3.    
  4.     $content = file_get_contents('php://input');
  5.     $arrayJson = json_decode($content, true);
  6.    
  7.     $arrayHeader = array();
  8.     $arrayHeader[] = "Content-Type: application/json";
  9.     $arrayHeader[] = "Authorization: Bearer {$accessToken}";
  10.    
  11.     //รับข้อความจากผู้ใช้
  12.     $message = $arrayJson['events'][0]['message']['text'];#ตัวอย่าง Message Type "Text"
  13.     if($message == "สวัสดี"){
  14.         $arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
  15.         $arrayPostData['messages'][0]['type'] = "text";
  16.         $arrayPostData['messages'][0]['text'] = "สวัสดีจ้าาา";
  17.         replyMsg($arrayHeader,$arrayPostData);
  18.     }
  19.     #ตัวอย่าง Message Type "Sticker"
  20.     else if($message == "ฝันดี"){
  21.         $arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
  22.         $arrayPostData['messages'][0]['type'] = "sticker";
  23.         $arrayPostData['messages'][0]['packageId'] = "2";
  24.         $arrayPostData['messages'][0]['stickerId'] = "46";
  25.         replyMsg($arrayHeader,$arrayPostData);
  26.     }
  27.     #ตัวอย่าง Message Type "Image"
  28.     else if($message == "รูปน้องแมว"){
  29.         $image_url = "https://i.pinimg.com/originals/cc/22/d1/cc22d10d9096e70fe3dbe3be2630182b.jpg";
  30.         $arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
  31.         $arrayPostData['messages'][0]['type'] = "image";
  32.         $arrayPostData['messages'][0]['originalContentUrl'] = $image_url;
  33.         $arrayPostData['messages'][0]['previewImageUrl'] = $image_url;
  34.         replyMsg($arrayHeader,$arrayPostData);
  35.     }
  36.     #ตัวอย่าง Message Type "Location"
  37.     else if($message == "พิกัดสยามพารากอน"){
  38.         $arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
  39.         $arrayPostData['messages'][0]['type'] = "location";
  40.         $arrayPostData['messages'][0]['title'] = "สยามพารากอน";
  41.         $arrayPostData['messages'][0]['address'] =   "13.7465354,100.532752";
  42.         $arrayPostData['messages'][0]['latitude'] = "13.7465354";
  43.         $arrayPostData['messages'][0]['longitude'] = "100.532752";
  44.         replyMsg($arrayHeader,$arrayPostData);
  45.     }
  46.     #ตัวอย่าง Message Type "Text + Sticker ใน 1 ครั้ง"
  47.     else if($message == "ลาก่อน"){
  48.         $arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
  49.         $arrayPostData['messages'][0]['type'] = "text";
  50.         $arrayPostData['messages'][0]['text'] = "อย่าทิ้งกันไป";
  51.         $arrayPostData['messages'][1]['type'] = "sticker";
  52.         $arrayPostData['messages'][1]['packageId'] = "1";
  53.         $arrayPostData['messages'][1]['stickerId'] = "131";
  54.         replyMsg($arrayHeader,$arrayPostData);
  55.     }function replyMsg($arrayHeader,$arrayPostData){
  56.         $strUrl = "https://api.line.me/v2/bot/message/reply";
  57.         $ch = curl_init();
  58.         curl_setopt($ch, CURLOPT_URL,$strUrl);
  59.         curl_setopt($ch, CURLOPT_HEADER, false);
  60.         curl_setopt($ch, CURLOPT_POST, true);
  61.         curl_setopt($ch, CURLOPT_HTTPHEADER, $arrayHeader);    
  62.         curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($arrayPostData));
  63.         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  64.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  65.         $result = curl_exec($ch);
  66.         curl_close ($ch);
  67.     }   exit;
  68. ?>