======利用google api calendar 發送SMS====== 因為發送訊息,是以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 {{:linux:monitor:google_calendar.png?300|}} =====申請使用Google API金鑰===== 因為是使用Google API,需要到 https://code.google.com/apis/console/ (使用google帳號登入)申請API金鑰。 - 先到 service 開啟 calendar api (舊版控制台){{:linux:monitor:api_services.png?700|}} - 再到 API Access, 就可以找到程式所需的資訊(舊版控制台)。分別是Client ID: Client secret: API key:等項目{{:linux:monitor:api_apiaccess.png?300|}} - 用python撰寫 發送訊息到API的程式,利用上面查到Client ID: Client secret: API key分別填入以下程式內 gapi_calendar.py#!/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'] - 執行此程式 參數1:日曆ID(google mail ) 2.標題 3.說明)#python gapi_calendar.py example@gmail.com TilteTest88 contextTest88 * 使用此程式產生的**網址進行授權** 要求是否允許 使用google API 發送訊息到日曆ID{{:linux:monitor:api_cmd.png?700|}}{{:linux:monitor:api_htmlauth.png?600|}}{{:linux:monitor:api_htmlauthcode.png?300|}} * 貼上授權碼後完成授權 , 會在目錄下產 calendar.dat{{:linux:monitor:api_cmd2.png?700|}} * 送出events 後會print event id ,就完成了。{{:linux:monitor:api_cmd3.png?700|}}{{:linux:monitor:google_calendar.png?700|}} ======參考資料====== * [[http://jerry2yang.wordpress.com/2012/02/06/ubuntu-install-google-calendar-api-with-python/|Ubuntu install google calendar api with python]] * [[https://www.evernote.com/shard/s112/sh/0db8e94a-8c19-4272-b1b8-392c4a623bb6/063809b3590dd7ea095cc0fd87a95a27?noteKey=063809b3590dd7ea095cc0fd87a95a27¬eGuid=0db8e94a-8c19-4272-b1b8-392c4a623bb6|JasonW同事-使用GOOGLE 日曆功能 發送SMS]] * [[http://www.openfoundry.org/tw/tech-column/8536-introduction-of-python-extension-management-tools|Python 套件管理程式簡介]]