LoginSignup
3

More than 3 years have passed since last update.

GASでesaのwebhookを拾ってslackに飛ばそう!

Last updated at Posted at 2018-07-05

概要

esaでslack連携をした時にメンションがactiveにならなくて困ったのでサクッとGASで作りました。

本編

GASの設定

var slack = {
  webhook: '', // Slackのwebhook
}

//esaからのpost
function doPost(e) {
  var params = JSON.parse(e.postData.getDataAsString());

  // mention分岐
  if(params.kind == "comment_mention") {
    var slack_post_data = {
      "text": params.user.name + "がメンションコメントしました。\n" + params.comment.body_md + "\n" + params.post.url,
      "link_names" : 1
    };
  }else{
     var slack_post_data = {
      "text": "esaでなにかが起こった、詳細は@dasisyouyuにきいて。"
    };
  }

  var payload = JSON.stringify(slack_post_data);

  var options = {
    "method" : "post",
    "contentType" : "application/json",
    "payload" : payload
  };

  //slack webhookへpost
  UrlFetchApp.fetch(slack["webhook"], options);
}

esaの設定

team設定のwebhookからGenericを選択し先程作ったGASのwebhookを設定しよう!

slackの設定

Incoming Webhooksを作成してできたwebhookURLをGASに貼っておく

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
What you can do with signing up
3