LoginSignup
9
10

More than 1 year has passed since last update.

【GAS】Slackの特定のキーワードを拾ってNotionに保存する方法

Last updated at Posted at 2021-06-16

社内でこんな要望があったのでやってみました
Slack___shoppl-contents___fastbeat.png

手順

  1. Notionの公式サイトからIntegration+トークンを作成する
  2. GASで後述の関数を作成+ウェブアプリとして公開(デプロイ時に設定するアクセス権限は「全て」を選択すること)
  3. SlackのOutGoing Webhookを設定し、手順2で公開したアプリのURLを設定Outgoing_Webhook___Slack_App_ディレクトリ.png

GASの実装

function doPost(e) {
  // Slackのトリガーに設定した単語を正規表現で消す
  var text = e.parameter.text.replace(/`Idea`/g, '');
  var arr = text.split(/\r\n|\n/);

  var data = {
    "parent": { "database_id": {*** NotionのページURLにあるパラメータを入力 ***} },
    "properties": {
      "Title": {
        "title": [
          {
            "text": {
              "content": arr[0]
            }
          }
        ]
      },
      "Description": {
        "rich_text": [
          {
            "text": {
              "content": arr[1]
            }
          }
        ]
      }
    }
  }

  var headers = {
    "Authorization": "Bearer {*** Notionで作成したIntegrationのトークン ***}",
    "Content-Type": "application/json",
    "Notion-Version": "2021-05-13"
  };

  var options = {
    "headers": headers,
    "method": "post",
    "payload": JSON.stringify(data)
  };

  UrlFetchApp.fetch("https://api.notion.com/v1/pages", options);
}

※ String.splitで改行をキー配列に変換してますが、割と適当です。あしからず

結果

Slack___shoppl-contents___fastbeat.png

Idea_lists.png

参考

9
10
0

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
9
10