Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trello List to Slack
// --------------EDIT REQUIRED START---------------------
// Trello
var TRELLO_USERNAME = "<USERNAME>";// enter your trello username
var TRELLO_KEY = "<TRELLO KEY>";// enter your trello key
var TRELLO_TOKEN = "<TRELLO TOKEN>";// enter your trello token
var TRELLO_BOARD_NAME = "<TRELLO BOARD NAME>"; // enter your trello board name
var TRELLO_LIST_NAME = "<TRELLO LIST NAME>"; // enter your trello list name
// Slack
var SLACK_WEBHOOK_URL = '<SLACK WEBHOOK URL>'; // enter your slack webhook url
// --------------EDIT REQUIRED END---------------------
var TRELLO_CREDENTIALS = "key=" + TRELLO_KEY + "&token=" + TRELLO_TOKEN;
function main() {
var cards = findTrelloCardsFromList(TRELLO_BOARD_NAME, TRELLO_LIST_NAME);
if (cards.length === 0) {
// don't notify if cards are empty
return;
}
notifySlack(cards);
}
function findTrelloCardsFromList(boardName, listName) {
var boardId = findTrelloBoardId(boardName);
var listId = findTrelloListId(boardId, listName);
var url = "https://trello.com/1/lists/" + listId + "/cards?&fields=name,shortUrl&" + TRELLO_CREDENTIALS;
var res = UrlFetchApp.fetch(url, {'method':'get'});
var cards = JSON.parse(res);
return cards;
///////////// function implementations
function findTrelloBoardId(name) {
var url = "https://trello.com/1/members/" + TRELLO_USERNAME + "/boards?fields=name&" + TRELLO_CREDENTIALS;
var res = UrlFetchApp.fetch(url, {'method':'get'});
var boards = JSON.parse(res);
for (var board of boards) {
if (board.name === name) {
return board.id;
}
}
throw new Error("board not found: " + name);
}
function findTrelloListId(boardId, listName) {
var url = "https://trello.com/1/boards/" + boardId + "/lists?fields=name&" + TRELLO_CREDENTIALS;
var res = UrlFetchApp.fetch(url, {'method':'get'});
var lists = JSON.parse(res);
for (var list of lists) {
if (list.name === listName) {
return list.id;
}
}
throw new Error("list not found: " + listName);
}
}
function notifySlack(cards) {
var item = [];
for (var card of cards) {
item.push("- <" + card.shortUrl + "|" + card.name + ">");
}
var text = item.join("\n");
// FYI: Message formatting
// https://api.slack.com/messaging/webhooks#advanced_message_formatting
var jsonData = {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "*本日の残タスク*"
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: text
}
}
]
};
var payload = JSON.stringify(jsonData);
var options =
{
"method" : "post",
"contentType" : "application/json",
"payload" : payload
};
UrlFetchApp.fetch(SLACK_WEBHOOK_URL, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment