はじめに
TrelloにはButlerと呼ばれる自動化ツールが備わっています。この記事ではButlerに頼らず、自由度の高いNode-REDとTrello APIを使いTrelloから情報を取得します
目次
- ユーザー名からBOARD情報(BOARD ID、BOARD名)を出力する (①)
- BOARD IDからLIST情報(LIST ID、LIST名)を出力する (②)
- LIST IDからCARD情報(CARD ID、CARD名)を出力する (③)
- LIST IDからCARD情報を出力し、1番上のCARD IDとCARD名を取得する
事前準備
- Trello API key, token情報が必要です
参考(取得方法) : https://qiita.com/HiguchiTakahiro/items/94f52eee07d257f8f995 - この記事ではTrello API key, Tokenを仮にセットして記載しています。ご自身で取得されたKey , Tokenへ書き換えてください
API key : samplekeya1samplekeyb2samplekeyc
Token : sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest - Trelloユーザー名を控えてください。この記事では
takahirohiguchi5
を使用します
1. ユーザー名からBOARD情報を出力する (①)
BOARD名「食べ物」のIDを取得します。BOARD IDはBOARDに所属しているユーザー名を使い取得します
Trello REST API : Get a Member
https://developer.atlassian.com/cloud/trello/rest/api-group-members/#api-members-id-get
GET /members/{id}
ブラウザを使い結果を出力する
ブラウザを使い以下のような表記でアクセスすると結果が表示されます
https://api.trello.com/1/members/takahirohiguchi5/boards?key=samplekeya1samplekeyb2samplekeyc&token=sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest&fields=name
Member IDを me に置き換えることも可能です
https://api.trello.com/1/members/me/boards?key=samplekeya1samplekeyb2samplekeyc&token=sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest&fields=name
結果 : BOARD名「食べ物」のIDが出力されます
[{"id":"64f3ef0dee20f23e30b0e580","name":"食べ物"}]
BOARD IDは頻繁に取得する必要がないと思われるため、ブラウザから取得したほうが早いと思います
Node-REDを使い結果を出力する
① injectノード : タイムスタンプ
injectノードはフローを始動させるために配置しているため、ノード中身の変更はありません
② functionノード : Trello API key/Token
API keyおよびTokenは再利用することが多いため、functionノードを設けています。
msg.Trello = {};
msg.Trello.Key = "samplekeya1samplekeyb2samplekeyc";
msg.Trello.Token = "sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest";
return msg;
③ functionノード : Get a Member
6行目 msg.url += "takahirohiguchi5";
は msg.url += "me";
に置き換えることができます
8行目 msg.url += &fields=name;
は省略できますが、結果からBOARD名とBOARD IDだけを取得するために記載しています
// Get a Member
// https://developer.atlassian.com/cloud/trello/rest/api-group-members/#api-members-id-get
// GET /members/{ id }
msg.url = "https://api.trello.com/1/members/";
msg.url += "takahirohiguchi5"; // Member ID
msg.url += "/boards?key=" + msg.Trello.Key + "&token=" + msg.Trello.Token;
msg.url += "&fields=name";
msg.method = "GET";
return msg;
④ http requestノード
①メソッドは前項目でmsg.method = "GET";
と記載しているため、- msg.methodに定義 -
をセットします
⑤ debugノード : debug 1
結果をNode-REDのデバッグメッセージ欄に表示させるために配置します
⑥ 結果
injectノード左側をクリックすることで、Node-REDデバッグメッセージ欄に実行結果が表示されます
①がBOARD名「食べ物」のBOARD IDです
Node-REDフロー : Get a Member
[{"id":"6e0eb204e54f5d76","type":"function","z":"40aa92b5925058af","name":"Get a Member","func":"// Get a Member\n// https://developer.atlassian.com/cloud/trello/rest/api-group-members/#api-members-id-get\n// GET /members/{ id }\n\nmsg.url = \"https://api.trello.com/1/members/\";\nmsg.url += \"takahirohiguchi5\"; // ユーザー名\nmsg.url += \"/boards?key=\" + msg.Trello.Key + \"&token=\" + msg.Trello.Token;\nmsg.url += \"&fields=name\";\n\nmsg.method = \"GET\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":80,"wires":[["d4612af2b8ddf1aa"]]},{"id":"c709be531932e462","type":"inject","z":"40aa92b5925058af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":80,"wires":[["1a9706603104a55b"]]},{"id":"d4612af2b8ddf1aa","type":"http request","z":"40aa92b5925058af","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{},"x":710,"y":80,"wires":[["c9a5a5bffc4dabf7"]]},{"id":"c9a5a5bffc4dabf7","type":"debug","z":"40aa92b5925058af","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":80,"wires":[]},{"id":"1a9706603104a55b","type":"function","z":"40aa92b5925058af","name":"Trello Key / Token","func":"msg.Trello = {};\nmsg.Trello.Key = \"samplekeya1samplekeyb2samplekeyc\";\nmsg.Trello.Token = \"sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":80,"wires":[["6e0eb204e54f5d76"]]},{"id":"9e8fa44f59fe3d4d","type":"comment","z":"40aa92b5925058af","name":"Get a Member","info":"","x":110,"y":40,"wires":[]}]
2. BOARD IDからLIST情報を出力する (②)
BOARD内のLIST情報を取得します。BOARD IDを使いBOARD内のLIST名およびLIST IDを取得します。
Trello REST API : Get Lists on a Board
https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-lists-get
GET /1/boards/{id}/lists
Node-REDを使い結果を出力する
① injectノード : タイムスタンプ
injectノードはフローを始動させるために配置しているため、ノード中身の変更はありません
② functionノード : Trello API key/Token
API keyおよびTokenは再利用するため、専用でfunctionノードを設けています。
msg.Trello = {};
msg.Trello.Key = "samplekeya1samplekeyb2samplekeyc";
msg.Trello.Token = "sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest";
return msg;
③ functionノード : Get Lists on a Board
6行目はBOARD IDを記載します
8行目 msg.url += "&fields=name";
は出力結果をLIST名とLIST IDに絞るため記載しています。省略が可能です
// Get Lists on a Board
// https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-lists-get
// GET /1/boards/{id}/lists
msg.url = "https://api.trello.com/1/boards/";
msg.url += "64f3ef0dee20f23e30b0e580"; // BOARD ID
msg.url += "/lists?key=" + msg.Trello.Key + "&token=" + msg.Trello.Token;
msg.url += "&fields=name";
msg.method = "GET";
return msg;
④ http requestノード
①メソッドは前項目でmsg.method = "GET";
と記載しているため、- msg.methodに定義 -
をセットします
⑤ debugノード : debug 2
結果をNode-REDのデバッグメッセージ欄に表示させるために配置します
⑥ 結果
injectノード左側をクリックすることで、Node-REDデバッグメッセージ欄に実行結果が表示されます
Node-REDフロー : Get Lists on a Board
[{"id":"91a3c9b283ceb643","type":"function","z":"40aa92b5925058af","name":"Trello Key / Token","func":"msg.Trello = {};\nmsg.Trello.Key = \"samplekeya1samplekeyb2samplekeyc\";\nmsg.Trello.Token = \"sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":80,"wires":[["971e30ce4d736f0c"]]},{"id":"971e30ce4d736f0c","type":"function","z":"40aa92b5925058af","name":"Get Lists on a Board","func":"// Get Lists on a Board\n// https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-lists-get\n// GET /1/boards/{id}/lists\n\nmsg.url = \"https://api.trello.com/1/boards/\";\nmsg.url += \"64f3ef0dee20f23e30b0e580\"; // BOARD ID\nmsg.url += \"/lists?key=\" + msg.Trello.Key + \"&token=\" + msg.Trello.Token;\nmsg.url += \"&fields=name\";\n\nmsg.method = \"GET\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":80,"wires":[["e2bb9b6760c941ed"]]},{"id":"c3c964ef93161c5a","type":"inject","z":"40aa92b5925058af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":80,"wires":[["91a3c9b283ceb643"]]},{"id":"e2bb9b6760c941ed","type":"http request","z":"40aa92b5925058af","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":750,"y":80,"wires":[["36a85bae9bef8034"]]},{"id":"36a85bae9bef8034","type":"debug","z":"40aa92b5925058af","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":80,"wires":[]},{"id":"a9cebf31350710a8","type":"comment","z":"40aa92b5925058af","name":"Get Lists on a Board","info":"","x":130,"y":40,"wires":[]}]
3. LIST IDからCARD情報を出力する (③)
LIST内のCARD情報を取得します。LIST IDを使いLIST内のCARD名およびCARD IDを取得します
ここでは、LIST名「果物」(LIST ID: 64f3ef0dee20f23e30b0e587
) 内のCARD情報を取得します
Trello REST API : Get Cards in a List
https://developer.atlassian.com/cloud/trello/rest/api-group-lists/#api-lists-id-cards-get
GET /1/lists/{id}/cards
Node-REDを使い結果を出力する
① injectノード : タイムスタンプ
injectノードはフローを始動させるために配置しているため、ノード中身の変更はありません
② functionノード : Trello API key/Token
API keyおよびTokenは再利用するため、専用でfunctionノードを設けています。
msg.Trello = {};
msg.Trello.Key = "samplekeya1samplekeyb2samplekeyc";
msg.Trello.Token = "sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest";
return msg;
③ functionノード : Get Cards in a List
6行目は取得したいLISTのLIST IDを記載します
8行目 msg.url += "&fields=name";
は出力結果をCARD名とCARD IDに絞るため記載しています。省略が可能です
// Get Cards in a List
// https://developer.atlassian.com/cloud/trello/rest/api-group-lists/#api-lists-id-cards-get
// GET /1/lists/{id}/cards
msg.url = "https://api.trello.com/1/lists/";
msg.url += "64f3ef0dee20f23e30b0e587"; // LIST ID
msg.url += "/cards?key=" + msg.Trello.Key + "&token=" + msg.Trello.Token;
msg.url += "&fields=name";
msg.method = "GET";
return msg;
④ http requestノード
①メソッドは前項目でmsg.method = "GET";
と記載しているため、- msg.methodに定義 -
をセットします
⑤ debugノード : debug 3
結果をNode-REDのデバッグメッセージ欄に表示させるために配置します
⑥ 結果
injectノード左側をクリックすることで、Node-REDデバッグメッセージ欄に実行結果が表示されます
Node-REDフロー : Get Cards in a List
[{"id":"ebed5c29631b4a43","type":"function","z":"40aa92b5925058af","name":"Get Cards in a List","func":"// Get Cards in a List\n// https://developer.atlassian.com/cloud/trello/rest/api-group-lists/#api-lists-id-cards-get\n// GET /1/lists/{id}/cards\n\nmsg.url = \"https://api.trello.com/1/lists/\";\nmsg.url += \"64f3ef0dee20f23e30b0e587\"; // LIST ID\nmsg.url += \"/cards?key=\" + msg.Trello.Key + \"&token=\" + msg.Trello.Token;\nmsg.url += \"&fields=name\";\n\nmsg.method = \"GET\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":80,"wires":[["88593a3d1492dbda"]]},{"id":"213a0d4cb08129cd","type":"inject","z":"40aa92b5925058af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":80,"wires":[["0a5d2211c9d28d57"]]},{"id":"88593a3d1492dbda","type":"http request","z":"40aa92b5925058af","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":730,"y":80,"wires":[["57f1313bbddd21ae"]]},{"id":"57f1313bbddd21ae","type":"debug","z":"40aa92b5925058af","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":80,"wires":[]},{"id":"d05c84fd3986f415","type":"comment","z":"40aa92b5925058af","name":"Get Cards in a List","info":"","x":130,"y":40,"wires":[]},{"id":"0a5d2211c9d28d57","type":"function","z":"40aa92b5925058af","name":"Trello Key / Token","func":"msg.Trello = {};\nmsg.Trello.Key = \"samplekeya1samplekeyb2samplekeyc\";\nmsg.Trello.Token = \"sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":80,"wires":[["ebed5c29631b4a43"]]}]
4. LIST IDから1番目のCARD IDとCARD名を取得する
ここでは、LIST「果物」内のCARD情報出力結果を使い、1番目のCARD IDとCARD名を取得します
Node-REDを使い結果を出力する
① debugノード : debug 3
② jsonノード
③ debugノード : debug 4
④ debugノード : 1番目のCARD ID
配列形式での出力結果を参照し、1番目のCARD IDを取得します
取得結果
⑤ debugノード : 1番目のCARD名
配列形式での出力結果を参照し、1番目のCARD名を取得します
取得結果
Node-REDフロー : LIST IDから1番目のCARD IDとCARD名を取得する
[{"id":"ebed5c29631b4a43","type":"function","z":"40aa92b5925058af","name":"Get Cards in a List","func":"// Get Cards in a List\n// https://developer.atlassian.com/cloud/trello/rest/api-group-lists/#api-lists-id-cards-get\n// GET /1/lists/{id}/cards\n\nmsg.url = \"https://api.trello.com/1/lists/\";\nmsg.url += \"64f3ef0dee20f23e30b0e587\"; // LIST ID\nmsg.url += \"/cards?key=\" + msg.Trello.Key + \"&token=\" + msg.Trello.Token;\nmsg.url += \"&fields=name\";\n\nmsg.method = \"GET\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":80,"wires":[["88593a3d1492dbda"]]},{"id":"213a0d4cb08129cd","type":"inject","z":"40aa92b5925058af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":80,"wires":[["0a5d2211c9d28d57"]]},{"id":"88593a3d1492dbda","type":"http request","z":"40aa92b5925058af","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":730,"y":80,"wires":[["57f1313bbddd21ae","632521e92f21fb1c"]]},{"id":"57f1313bbddd21ae","type":"debug","z":"40aa92b5925058af","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":80,"wires":[]},{"id":"d05c84fd3986f415","type":"comment","z":"40aa92b5925058af","name":"Get Cards in a List","info":"","x":130,"y":40,"wires":[]},{"id":"0a5d2211c9d28d57","type":"function","z":"40aa92b5925058af","name":"Trello Key / Token","func":"msg.Trello = {};\nmsg.Trello.Key = \"samplekeya1samplekeyb2samplekeyc\";\nmsg.Trello.Token = \"sampletokenasampletokenbsampletokencsampletokendsampletokenesampletokenftest\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":80,"wires":[["ebed5c29631b4a43"]]},{"id":"632521e92f21fb1c","type":"json","z":"40aa92b5925058af","name":"","property":"payload","action":"","pretty":false,"x":890,"y":120,"wires":[["b3cb7fcf5345eb4f","74f99294250209ab","518f82dc68f70301"]]},{"id":"b3cb7fcf5345eb4f","type":"debug","z":"40aa92b5925058af","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":120,"wires":[]},{"id":"74f99294250209ab","type":"debug","z":"40aa92b5925058af","name":"1番目のCARD ID","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0].id","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":180,"wires":[]},{"id":"518f82dc68f70301","type":"debug","z":"40aa92b5925058af","name":"1番目のCARD名","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0].name","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":220,"wires":[]}]
参考
BOARD ID取得
Trello REST API : Get a Member
https://developer.atlassian.com/cloud/trello/rest/api-group-members/#api-members-id-get
LIST ID取得
Trello REST API : Get Lists on a Board
https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-lists-get
CARD ID取得
Trello REST API : Get Cards in a List :
https://developer.atlassian.com/cloud/trello/rest/api-group-lists/#api-lists-id-cards-get