หน้าเว็บ

วันพฤหัสบดีที่ 22 กุมภาพันธ์ พ.ศ. 2561

การ Ping Server แจ้งผ่านทาง Line Notify


Website uptime Event Emitter


Ping-monitor (Node-Monitor) is a tool for monitoring your website's uptime. If the website is up or down an event is emitted. This module is extracted from Node Ping, an uptime app I wrote recently.

Installation

npm install ping-monitor

How to use

var Monitor = require('ping-monitor');
 
var myWebsite = new Monitor(options);
 
myWebsite.on(event, callback(response) {
    // Do something with the response 
});

Options

  • website (* required) - The url of the website to be monitored.
  • interval (defaults to 15) - time interval(in minutes) for checking website availability.

Emitted Events

  • up - All is good website is up.
  • down - Not good, website is down.
  • error - Bad, http request module cannot load website.
  • stop - Fired when the monitor has stopped.

response object

  • object.website - website being monitored.
  • object.time - time in milliseconds.
  • object.responseCode - http response code.
  • object.responseMessage - http response code message.

Example


var Monitor = require('ping-monitor');
var request = require('request'); 
const token = 'Line Token';

var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
}
// Configure the request
var options = {
    'url': 'https://notify-api.line.me/api/notify',
    'method': 'POST',
    'headers': headers,
'auth': {
       'bearer': token
},
'form': {
       'message': ''
}
}

var myWebsite = new Monitor({
    website: 'http://www.tapee.ac.th',
    interval: 1
});


myWebsite.on('error', function (msg) {
    console.log(msg);
    options.form.message = 'http://www.tapee.ac.th  is error! ';
    request(options);
});


myWebsite.on('up', function (res) {
    console.log('Yay!! ' + res.website + ' is up.');
});


myWebsite.on('down', function (res) {
    options.form.message = 'http://www.tapee.ac.th  is down! ';
    request(options);
    console.log( res.website + ' is down! ' + res.statusMessage);
});

ทำระบบแจ้งเตือนบน LINE ด้วย Python

ทำระบบแจ้งเตือนบน LINE ด้วย Python


ที่มาจาก : http://python3.wannaphong.com/2016/10/line-python.html
ปัจจุบันนี้ไลน์เป็นที่นิยมกันมาก เราสามารถนำ LINE มาประยุกต์ใช้งานให้เหมาะสมกับงาน เช่น การนำไปใช้งานทำระบบแจ้งเตือนบน LINE ด้วย Python


ไลน์สามารถทำระบบแจ้งเตือนได้โดยผ่าน LINE Notify และมี API ให้เรียกใช้งาน

ก่อนอื่นให้ทำการเพิ่มเพื่อนของ LINE Notify ก่อน

LINE Notify 
LINE Notify

เข้าไปที่ https://notify-bot.line.me/my/ คลิก "ออก Token"

แล้วทำการตั้งชื่อแล้วเลือกห้องแชทที่ต้องการส่งข้อความแจ้งเตือน แล้วกด "ออก Token"


จะได้ Token ทำการคัดลอกเก็บเอาไว้



ลงมือเขียนโค้ด Python
import requests,json
import urllib.parse

LINE_ACCESS_TOKEN="ใส่ Token"
url = "https://notify-api.line.me/api/notify" 


message ="ทดสอบ" # ข้อความที่ต้องการส่ง
msg = urllib.parse.urlencode({"message":message})
LINE_HEADERS = {'Content-Type':'application/x-www-form-urlencoded',"Authorization":"Bearer "+LINE_ACCESS_TOKEN}
session = requests.Session()
a=session.post(url, headers=LINE_HEADERS, data=msg)
print(a.text)



นอกจากนั้นยังสามารถส่งรูปภาพได้ด้วย สามารถใช้งานโดยการกำหนดค่าข้อความในโค้ด Python ด้านบน
สามารถอ่านเอกสาร API ได้ที่ https://notify-bot.line.me/static/pdf/line-notify-api.pdf?v=1.0