Skip to content

Instantly share code, notes, and snippets.

@yuzutas0
Created April 29, 2019 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuzutas0/fb0f2dfd318e08ea8b6c331606948761 to your computer and use it in GitHub Desktop.
Save yuzutas0/fb0f2dfd318e08ea8b6c331606948761 to your computer and use it in GitHub Desktop.
GoogleFrom to Slack by GAS

1. set incoming-webhook at slack

  • click channel name
  • Add an app
  • incomming-webhook
  • add configration
  • set target channel and bot name
  • get url

2. add gas

function sendToSlack(body) {
  var url = "https://hooks.slack.com/services/xxxxxxxxxx";
  var data = {
    "icon_emoji": ":xxxxxx:",
    "attachments": [{
      "color": "good",
      "title": "Google Form",
      "text" : body,
    }],
  };
  var payload = JSON.stringify(data);
  var options = {
    "method" : "POST",
    "contentType" : "application/json",
    "payload" : payload
  };
  var response = UrlFetchApp.fetch(url, options);
}

function onFormSubmit(e){
  var body = "```\n"; 
  var itemResponse = e.response.getItemResponses();
  for (var j = 0; j < itemResponse.length; j++){    
    var formData = itemResponse[j];
    body += formData.getItem().getTitle() + ": ";
    body += formData.getResponse() + "\n";
  }
  body += "```"
  sendToSlack(body);
}

function test(){
  sendToSlack("test notification");
}

3. test

  • do test method

4. deploy

  • choose onFormSubmit method
  • click timer icon
  • set trigger on submit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment