Last active
September 10, 2018 07:21
-
-
Save hrsano645/599914b6c1e0d798a913a3aee33d0682 to your computer and use it in GitHub Desktop.
This file contains 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
function createWebhook(){ | |
// プロパティを設定してから必要な情報を取り出す | |
// 参考:https://qiita.com/0Delta/items/7d8303eebbff4062069e | |
var scriptProp = PropertiesService.getScriptProperties().getProperties(); | |
var trelloKey = scriptProp.TRELLO_API_KEY; | |
var trelloToken = scriptProp.TRELLO_TOKEN; | |
// Trello WebhookへCallback URLを登録する手順 | |
// 1. 登録用のURL生成のために、GASのWEBアプリケーションの公開を以下のように変更してURLを生成する | |
// 次のユーザーとしてアプリケーションを実行:自分, アプリケーションにアクセスできるユーザー:全員(匿名ユーザーを含む) | |
// URLの構造がhttps://script.google.com/macros/s/[一意の文字列]/exec ならOK | |
// options内のpayload.callbackURLに取得したURLを書き込む | |
// 2. createWebhook関数を実行する前に、一時的にGASのWEBアプリケーションの公開を以下に変更する | |
// 次のユーザーとしてアプリケーションを実行:自分, アプリケーションにアクセスできるユーザー:自分だけ | |
// 3. createWebhook関数を実行し、結果がログで確認できれば成功。 | |
// 初回に実行すると(確か)スクリプト実行時のGoogleの認証を行うダイアログが出てくるので許可すること | |
// 4. GASのWEBアプリケーションの公開を 1 と同じ設定に戻す | |
// [TrelloのボードID] はAPIのDocumentから取得方法を確認すること | |
// https://developers.trello.com/docs/api-introduction#section--a-name-auth-your-first-api-call-a- | |
var url = 'https://api.trello.com/1/tokens/' + trelloToken + '/webhooks/?key=' + trelloKey; | |
var options = { | |
'method' : 'post', | |
'payload' : { | |
'description': 'iotlt_trello_to_mirrorball', | |
'callbackURL': '[Callback先のGASの公開URL]', | |
'idModel': '[TrelloのボードID]' | |
} | |
} | |
console.log(UrlFetchApp.fetch(url, options)); | |
} | |
function doPost(e){ | |
console.log("run dopost function!"); | |
// トリガーにしたい移動先のリスト名をプロパティから取得する | |
var scriptProp = PropertiesService.getScriptProperties().getProperties(); | |
var targetMovedListName = scriptProp.TARGET_MOVED_LIST_NAME; | |
// コールバックで受け取ったJSONデータをパースする | |
var contents = JSON.parse(e.postData.contents); | |
// フィルター1:actionTypeが'updateCard'以外の場合は終了。 | |
// actionTypeはこちらで確認できます:https://developers.trello.com/reference/#action-types | |
var actionType = contents.action.type; | |
if(actionType !== 'updateCard'){ return } | |
// フィルター2:移した先のリスト名で絞る | |
if("listAfter" in contents.action.data) { | |
var movedListName = contents.action.data.listAfter.name; | |
} else { | |
return | |
} | |
if(movedListName !== targetMovedListName){return} | |
// Stackdriver Loggerへ生成したメッセージを流す | |
var cardName = contents.action.data.card.name; | |
var message = 'cardtitle:' + cardName + ', actiontype:' + actionType; | |
console.log(message) | |
// beebotte のhook用urlを叩いてデータを渡す | |
// [channnel], [resource], [token] はbeebotte側で生成させたもので変更すること | |
// (ここの情報もプロパティにすればよかった...) | |
var url = 'http://api.beebotte.com/v1/data/publish/[channnel]/[resource]?token=[token]'; | |
var options = { | |
'method' : 'post', | |
'contentType' : 'application/json', | |
"payload" : '{ "data": "exchange trello to gas" }' | |
} | |
var beebotte_response = UrlFetchApp.fetch(url, options); | |
console.log("end"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment