ส่ง SMS แจ้งเตือนผ่าน Google Calendar โดยใช้ command line interface API v.3 ใน Centos 5


ส่ง SMS แจ้งเตือนผ่าน Google Calendar โดยใช้ command line interface API v.3 ใน Centos 5 

ขั้นแรกติดตั้ง zlib กันก่อนครับ

#yum install zlib-devel -y

จากนั้นทำการติดตั้ง python 2.5 จาก source โดยต้อง compile ให้รองรับ SSL 

#wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tgz
#tar fxz Python-2.5.2.tgz

หลังจากได้ source มาแล้วให้แก้ไขไฟล์ /Modules/Setup.dist เพื่อให้รองรับ SSL

#cd Python-2.5.2/Modules
#vi Setup.dist

โดยจะต้องแก้ไขโดยการเอา Comment ออก ในบรรทัดที่ประมาณ 201 และ 207-209

Uncomment line 201

socket socketmodule.c

Uncomment line 207-209   

    ssl _ssl.c <br> 
        
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl <br>         

    -L$(SSL)/lib -lssl -lcrypto

จากนั้นทำการติดตั้ง python ต่อโดยการใช้คำสั่งต่อไปนี้

#./configure
#make
#make altinstall

ทดสอบการติดตั้ง python โดยการันคำสั่ง 

#python2.5 -V

python จะแสดงรุ่นให้ทราบ เป็นอันเรียบร้อย จากนั้น ทำการติดตั้ง package อื่นๆ ต่อดังนี้ โดยไปที่ /usr/bin

ติดตั้ง elementtree-1.2.6-20050316.tar.gz

#cd /usr/bin
#wget http://effbot.org/media/downloads/elementtree-1.2.6-20050316.tar.gz
#tar -xzf elementtree-1.2.6-20050316.tar.gz
#cd elementtree-1.2.6-20050316
#python2.5 setup.py install

ติดตั้ง setuptools-0.6c7-py2.5.egg

#cd /usr/bin
#wget https://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c7-py2.5.egg#md5=45d2ad28f9750e7434111fde831e8372
#./setuptools-0.6c7-py2.5.egg

ติดตั้ง python-dateutil-1.5.tar.gz

#cd /usr/bin
#wget https://pypi.python.org/packages/source/p/python-dateutil/python-dateutil-1.5.tar.gz#md5=0dcb1de5e5cad69490a3b6ab63f0cfa5
#tar -xzf python-dateutil-1.5.tar.gz
#cd python-dateutil-1.5
#python2.5 setup.py install

ติดตั้ง oauth2client-1.1.tar.gz

#cd /usr/bin
#wget https://google-api-python-client.googlecode.com/files/oauth2client-1.1.tar.gz
#tar -xzf oauth2client-1.1.tar.gz
#cd oauth2client-1.1
#python2.5 setup.py install

ติดตั้ง google-api-python-client-1.1.tar.gz

#cd /usr/bin
#wget https://google-api-python-client.googlecode.com/files/google-api-python-client-1.1.tar.gz
#tar -xzf google-api-python-client-1.1.tar.gz
#cd google-api-python-client-1.1
#python2.5 setup.py install

ทำการติดตั้ง Google-Python-Client gdata โดยการเข้าไปที่ /usr/bin จากนั้น

#wget https://gdata-python-client.googlecode.com/files/gdata-2.0.17.zip
#unzip gdata-2.0.17.zip
#cd gdata-2.0.17
#python2.5 setup.py install

หลังจากนั้น ทำการติดตั้ง simplejson โดยเปลี่ยนไปอยู่ที่ /usr/bin และดาวน์โหลด simplejson-3.1.3.tar.gz ดังนี้

#wget https://pypi.python.org/packages/source/s/simplejson/simplejson-3.1.3.tar.gz#md5=ffb9fdd8ff3225a61b455bb1bcb98e41
#tar -xzf simplejson-3.1.3.tar.gz
#cd simplejson-3.1.3
# python2.5 setup.py install

จากนั้นทำการ ติดตั้ง gcalcli 

#wget https://raw.github.com/insanum/gcalcli/master/gcalcli

แก้ไข ไฟล์ gcalcli ที่ได้ download มา (version 3.1)โดย

#vi gcalcli 

เติม เข้าไปในบรรทัดที่ 2 คำสั่งดังนี้

from future import with_statement

แก้ไขบรรทัดที่ 195-198 ให้เป็นดังนี้

try:   
       from simplejson import json
except ImportError:
       import simplejson

บันทึกการแก้ไขให้เรียบร้อยครับ


จากนั้นมาทดสอบการทำงานกันครับ โดยใช้คำสั่ง
#python2.5 gcalcli --cal 'main' quick 'test SMS9 23:10' 

อธิบายคำสั่ง

'main' คือชื่อปฏิทินหลักใน Google Calendar ของท่าน
'test SMS9 23:10' คือข้อความที่ต้องการให้ขึ้นการนัดหมายข้อความ test SMS9 โดยให้เป็นการนัดหมายที่เวลา 23:10 นะครับ เวลาในที่นี้ควรเป็นเวลาในอนาคตโดยที่ จะต้องเป็นเวลาก่อนที่เราต้องการให้ sms แจ้งเตือนเรา สัก 6-7 นาที ทั้งนี้ขึ้นอยู่กับว่าท่านได้ตั้งค่าให้แจ้งเตือน SMS ก่อนเวลานัดใน Google Calendar กี่นาที

เมื่อรันคำสั่ง

[root@noomnet bin]# python2.5 gcalcli --cal 'main' quick 'test SMS9 23:10' 
Failed to start a local webserver listening on either port 8080
or port 9090. Please check your firewall settings and locally
running programs that may be blocking or using those ports.
Falling back to --noauth_local_webserver and continuing with authorization.
Go to the following link in your browser:
   https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Furlshortener&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=232867676714.apps.googleusercontent.com&access_type=offline
Enter verification code: 4/wfhJrWzAJa7_H433Tt0YQ-Uwvq_Q.ovUR39Su3bEZgrKXntQAax2kMuYcfAI
Authentication successful.
จะต้องทำการเปิด link ที่ได้มาใน browser เพื่อยืนยันอนุญาติให้เข้าใช้ Google Calendar Service โดยต้อง copy เอา Authentication code มาใส่ (ทำครั้งแรกครั้งเดียว หลังจากนั้นไม่ต้องทำอีก) 
จากคำสั่งดังกล่าว เราจะได้ SMS จาก Google ก่อนเวลานัดหมายประมาณ 5 นาทีครับ
SMS
เราสามารถประยุกต์ใช้ Shell script เพื่อให้ตรวจสอบว่า server เราทำงานปกติหรือไม่ หากมีปัญหาให้ sms แจ้ง โดยใช้คำสั่งเหล่านี้ บันทึกลงในไฟล์เช่น servchk.sh จากนั้นสั่งให้ cron รันทุก 15 นาที 

#!/bin/bash
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
# -------------------------------------------------------------------------
# Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Modify by Yudthaphon
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Setup SMS User below
# See URL for more info:
# http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
# -------------------------------------------------------------------------
# add ip / hostname separated by blank space
HOSTS="192.168.2.100"
# no ping request
COUNT=5
DATE=date +"%H:%M%p" --date '6 mins'

for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed
myText="Hostdown $myHost $DATE"
python2.5 gcalcli --cal 'main' quick "$myText"
  fi
done

คำสำคัญ (Tags): #google calendar#sms#Centos 5#gcalcli
หมายเลขบันทึก: 533249เขียนเมื่อ 18 เมษายน 2013 17:03 น. ()แก้ไขเมื่อ 20 เมษายน 2013 14:41 น. ()สัญญาอนุญาต: ครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า-ไม่ดัดแปลงจำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

ไม่อนุญาตให้แสดงความเห็น
พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท