LoginSignup
7

More than 5 years have passed since last update.

はじめてのGAS - Slackにファイルを送る

Last updated at Posted at 2018-06-11

Google Apps Script(以後、GAS)から、Slackのチャンネルにファイルを送る方法です。

ここでは以下の説明は省略します。(別記事を書く予定)
- GASプロジェクトの作成方法
- GAS環境変数の設定方法
- Slackの操作方法
- Slack Appの登録方法

ソースコード

/**
 * 呼び出しテスト
*/
function testFile() {
  const channel = "xxxxxxxx";
  const content = "ファイルの中身だよー\nほげほげー";

  postFileToSlackChannel(channel, content);
}

/* 
 * FileをSlackチャンネルにポストする関数
 */
function postFileToSlackChannel(channel, content){
  var payload = {
    "token" : PropertiesService.getScriptProperties().getProperty('SlackOAuthAccessToken'),
    "channels" : channel,
    'content' : content,
    'filename':"sample.txt",
    'initial_comment': "ファイルにつけるコメント",
    'title': "Slack上でのファイルのタイトル"
  };
  var options = {
    "method" : "post",
    'contentType': 'application/x-www-form-urlencoded',
    "payload" : payload
  };

  var response = UrlFetchApp.fetch("https://slack.com/api/files.upload", options);
  Logger.log(response);
}

SlackOAuthAccessToken はココに表示されている値です。
ã__ã_£ã__ã__ã_£.png

設定

OAuth & Permissions > Scopes から、以下の権限追加が必要
files:write:user

参考リンク

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7