![見出し画像](https://assets.st-note.com/production/uploads/images/80082030/rectangle_large_type_2_2993f8f1417530a7716403883a1ed819.jpeg?width=800)
[GAS][Slack]定型的な投稿にBotを用いる(その3)
前2回、「GASで『定型的な文章』をSlack botに投稿」という記事を書きました。
別のパターンを構築してみます。
各チャンネルごとにメッセージを設定し一斉に投稿する
チャンネルごとに少しだけメッセージ内容を変更し投稿したいケースはよくあります。
以下のスプレッドシートを用意します。
![](https://assets.st-note.com/img/1653726060409-ItLDefvfWl.png?width=800)
送信したいチャンネルにチェックを入れ、列Cに投稿メッセージを入力します。
スクリプトはこちら。
function slackBot_03() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const lastRow = sheet.getLastRow();
const channelArray = ss.getSheetByName('channelリスト').getDataRange().getValues();
for (let i = 2; i <= lastRow; i++) {
const isSend = sheet.getRange(i, 1).getValue();
if (isSend) {
const channelName = sheet.getRange(i, 2).getValue();
const channel = findValue(channelArray, channelName);
const message = sheet.getRange(i, 3).getValue();
postMessage(channel, message);
}
}
}
function postMessage(channel, message) {
const token = PropertiesService.getScriptProperties().getProperty('SlackBot_token');
const options = {
"method": "post",
"contentType": "application/x-www-form-urlencoded",
"payload": {
"token": token,
"channel": channel,
"text": message
}
};
const url = 'https://slack.com/api/chat.postMessage';
UrlFetchApp.fetch(url, options);
}
function findValue(values, key) {
for (const i in values) {
if (values[i][0] === key) {
return values[i][1];
}
}
return;
}
※列Aのチェックボックスをオン/オフにするのが煩わしい場合は以下のようなパターンでも良いかと思います。
この場合、「列Bが空白でないと投稿する」という設計にしているのでその点注意しましょう。
![](https://assets.st-note.com/img/1653726447989-rwkkKHjL8V.png?width=800)
スクリプトはこちら。(function postMessage以下は共通)
function slackBot_04_2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const lastRow = sheet.getLastRow();
const channelArray = ss.getSheetByName('channelリスト').getDataRange().getValues();
for (let i = 2; i <= lastRow; i++) {
const message = sheet.getRange(i, 2).getValue();
if (message !== "") {
const channelName = sheet.getRange(i, 1).getValue();
const channel = findValue(channelArray, channelName);
postMessage(channel, message);
}
}
}
function postMessage(channel, message) {
const token = PropertiesService.getScriptProperties().getProperty('SlackBot_token');
const options = {
"method": "post",
"contentType": "application/x-www-form-urlencoded",
"payload": {
"token": token,
"channel": channel,
"text": message
}
};
const url = 'https://slack.com/api/chat.postMessage';
UrlFetchApp.fetch(url, options);
}
function findValue(values, key) {
for (const i in values) {
if (values[i][0] === key) {
return values[i][1];
}
}
return;
}
チャンネル数が多い場合はプルダウンで
チャンネル数が多い場合はプルダウンで実装するのも良いかと思います。
![](https://assets.st-note.com/img/1653727140251-lPF2GSyRWe.png?width=800)
スクリプトはこちら。(function postMessage以下は共通)
function slackBot_04_3() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const lastRow = sheet.getLastRow();
const channelArray = ss.getSheetByName('channelリスト').getDataRange().getValues();
for (let i = 2; i <= lastRow; i++) {
const channelName = sheet.getRange(i, 1).getValue();
const channel = findValue(channelArray, channelName);
const message = sheet.getRange(i, 2).getValue();
postMessage(channel, message);
}
}
function postMessage(channel, message) {
const token = PropertiesService.getScriptProperties().getProperty('SlackBot_token');
const options = {
"method": "post",
"contentType": "application/x-www-form-urlencoded",
"payload": {
"token": token,
"channel": channel,
"text": message
}
};
const url = 'https://slack.com/api/chat.postMessage';
UrlFetchApp.fetch(url, options);
}
function findValue(values, key) {
for (const i in values) {
if (values[i][0] === key) {
return values[i][1];
}
}
return;
}
この記事が気に入ったらサポートをしてみませんか?