因為發送訊息,是以python程式寫成。運用Google API Calendar等工具將信息傳到Google日曆,再利用Google日曆提供手機簡訊提醒工具來達成SMS的目的。
安裝python必要套件。 (easy_install )
#yum install python-setuptools
#easy_install python-gflags #easy_install httplib2 #easy_install argparse #easy_install rfc3339 #easy_install google-api-python-client
因為是使用Google API,需要到 https://code.google.com/apis/console/ (使用google帳號登入)申請API金鑰。
#!/usr/bin/python # -*- coding: utf8 -*- import gflags import httplib2 import sys import time from apiclient.discovery import build from oauth2client.file import Storage from oauth2client.client import OAuth2WebServerFlow from oauth2client.tools import run from rfc3339 import rfc3339 FLAGS = gflags.FLAGS FLOW = OAuth2WebServerFlow( client_id='Your Client ID', client_secret='Your Client secret', scope='https://www.googleapis.com/auth/calendar', user_agent='APIProject/0.1') # To disable the local server feature, uncomment the following line: gflags.FLAGS.auth_local_webserver = False # If the Credentials don't exist or are invalid, run through the native client # flow. The Storage object will ensure that if successful the good # Credentials will get written back to a file. storage = Storage('/var/lib/zabbixsrv/alertscripts/calendar.dat') ==>(網址進行授權完成後,會在此路徑下產生calendar.dat) credentials = storage.get() if credentials is None or credentials.invalid == True: credentials = run(FLOW, storage) # Create an httplib2.Http object to handle our HTTP requests and authorize it # with our good Credentials. http = httplib2.Http() http = credentials.authorize(http) # Build a service object for interacting with the API. Visit # the Google APIs Console # to get a developerKey for your own application. service = build(serviceName='calendar', version='v3', http=http, developerKey='Your API key') #zb_sendto = 日曆ID ,zb_title = 主旨 , zb_body= 說明 zb_sendto= sys.argv[1] zb_title= sys.argv[2] zb_body= sys.argv[3] sttime = time.time() endtime = time.time()+60*60 event = { 'summary': zb_title, 'location': 'QY_office', 'description': zb_body, 'start': { 'dateTime': rfc3339(sttime), 'timeZone': 'Asia/Taipei' }, 'end': { 'dateTime': rfc3339(endtime), 'timeZone': 'Asia/Taipei' }, "reminders": { "useDefault": False, "overrides": [ { "method": "email", "minutes": 0 }, { "method": "popup", "minutes": 0 }, { "method": "sms", "minutes": 0 }, ] }, } created_event = service.events().insert(calendarId=zb_sendto, body=event).execute() print created_event['id']
#python gapi_calendar.py [email protected] TilteTest88 contextTest88