VBOX Communication with Line Notify

Last updated: 11 ก.ย. 2565  |  1843 จำนวนผู้เข้าชม  | 

VBOX Communication with Line Notify

สำหรับตัวอย่างข้างต้น ก่อนทำการทดสอบต้องทำการสมัคร Line Notify ก่อนนะครับ แล้วจึงนำ Token ของไลน์มาใช้งาน

 

This example shows how to use the LINE Notify to send message into LINE group. When monitoring bit "@test" changes, it will trigger and send the message. Please replace with your own Token.

สร้าง bit ใน vbox ชื่อ "Test" สำหรับเทส

ส่วนของ Lua Scrips.


local tempBit = 0
local tempWord = 0

local LineToken = "เปลี่ยน Line Token ตรงนี้"

local https = require("https")
local json = require("json")
local ltn12 = require("ltn12")

-- Send http.get request and return response result
function getHttpsUrl(url,header,reqbody)
   local body = {}
   local bodyJson = json.encode(body)
   local result_table, code, headers, status = https.request{
       method = "POST",
       url = url,
       source = ltn12.source.string(reqbody),
       headers = header,
       sink = ltn12.sink.table(body)
   }
   print("code:"..code)
   if code~= 200 then
       return
   else
       return body
   end
end

function getMessageUrl(lineMessage)
   local url = "https://notify-api.line.me/api/notify"
   local reqMess = "message="..lineMessage
   local headers =
                {
                        ["Authorization"] = "Bearer "..LineToken,
                        ["Content-Type"] = "application/x-www-form-urlencoded",
                        ["Content-Length"] = #reqMess
                }
   
   print("Get the link:"..url)
   getHttpsUrl(url, headers, reqMess)
end


function linenotify.main()
   local bitValue = addr_getbit("@test");
   local message = ''
   print("b=="..bitValue)
   if bitValue == 1 and bitValue ~= tempBit then
       message = 'Alarm V-Box triggered, the output is '.. bitValue
       getMessageUrl(message)
       print("Notification pushed of triggering alarm,"..bitValue)
   elseif bitValue == 0 and bitValue ~= tempBit then
       message = 'Alarm V-Box dismissed, the output is '.. bitValue
       getMessageUrl(message)
       print("Notification pushed of dismissing alarm,"..bitValue)
   end
   tempBit = bitValue----Prevent monitoring values from continuous being sent to the platform
   
   local wordValue = addr_getword("@t2")
   print("w=="..wordValue)
   --dosomething
   if wordValue >= 100 and wordValue ~= tempWord and tempWord <= 100 then
       message = 'Alarm V-Box triggered, the temperature is '.. wordValue
       getMessageUrl(message)
       print("Notification pushed of triggering alarm,"..wordValue)
   elseif wordValue < 100 and wordValue ~= tempWord and tempWord >= 100 then
       message = 'Alarm V-Box dismissed, the temperature is '.. wordValue
       getMessageUrl(message)
       print("Notification pushed of dismissing alarm,"..wordValue)
   end
   tempWord = wordValue----Prevent monitoring values from continuous being sent to the platform
end

เนื้อหาที่เกี่ยวข้อง

Powered by MakeWebEasy.com
เว็บไซต์นี้มีการใช้งานคุกกี้ เพื่อเพิ่มประสิทธิภาพและประสบการณ์ที่ดีในการใช้งานเว็บไซต์ของท่าน ท่านสามารถอ่านรายละเอียดเพิ่มเติมได้ที่ นโยบายความเป็นส่วนตัว  และ  นโยบายคุกกี้