Googleスプレッドシートやフォームって、ちょっとした情報を共有したり何か申請してもらう時に何気に便利ですよね。
ただ、お問い合わせが発生したときなんかの通知がメールに集まる傾向があって、最近のようなチャットを主に使っている環境ではすぐに通知に反応できなかったり、必要以上にメールがたまってしまう傾向があります。
以下のようなGoogleAppScriptをスクリプトエディタから作成、保存して契機を設定しておくと、任意のサービスに通知を出すことができるようになります。
以下では、 messageToHipchat() をフォーム送信を契機に設定しておくと、フォームが送信されたことを契機に、フォームの中身含めてHipChatの特定のルームに通知を投げます。
UrlFetchApp がGoogle App ScriptでHTTPリクエストを発行したりするところです。 FormApp がフォームに対する操作をするところです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * @param authToken [String] HipChat token you are created. | |
| * @param roomId [String] Room id you would like to send notification. | |
| * @param message [String] Message string | |
| */ | |
| function hipchat(authToken, roomId, message) { | |
| var url = 'https://api.hipchat.com/v2/room/' + roomId + '/notification?auth_token=' + authToken; | |
| var payload = | |
| { | |
| color : 'green', | |
| message : message, | |
| notify : true, | |
| message_format : 'text' | |
| }; | |
| var params = | |
| { | |
| method : 'post', | |
| contentType : 'application/json; charset=utf-8', | |
| payload : JSON.stringify(payload) | |
| }; | |
| var res = UrlFetchApp.fetch(url, params); | |
| } | |
| /* | |
| * @param formId [String] Form ID you would like to access. | |
| */ | |
| function submittedMessage(formId) { | |
| var existingForm = FormApp.openById(formId); | |
| var responses = existingForm.getResponses(); | |
| var result = 'Submitted:'; | |
| var latestResponse = responses[responses.length – 1]; | |
| result = result + Utilities.formatDate(latestResponse.getTimestamp(), 'Asia/Tokyo', 'yyyy年MM月dd日(EEE) HH:mm:ss'); | |
| var itemResponses = latestResponse.getItemResponses(); | |
| for (var i = 0; i < itemResponses.length; i++) { | |
| var itemResponse = itemResponses[i]; | |
| result = result + '\n' + itemResponse.getItem().getTitle() + itemResponse.getResponse(); | |
| } | |
| return result | |
| } | |
| function messageToHipchat() { | |
| hipchat(authToken, roomId, submittedMessage(formId)); | |
| } |
何気に便利。