Google Forms to Slack: Auto mention via Webhook

Introduction

Integrating Google Forms with Slack can turn a simple questionnaire into a real‑time notification hub for your team. By routing form responses through Google Sheets and an Apps Script, you can automatically post messages to a Slack channel the moment a submission arrives. The real power lies in being able to mention a specific teammate—using Slack’s @username syntax—so the right person gets alerted instantly. This article walks you through every step: creating a Slack incoming webhook, linking a Google Form to a Sheet, writing the script that sends the payload, embedding the proper user ID for a mention, and finally testing the whole workflow. Follow the guide and turn any form into a proactive communication tool.

Creating a Slack Incoming Webhook

Before any data can travel from Google to Slack, you need a secure endpoint that accepts JSON payloads.

  • Open Slack: Go to Settings & administration > Manage apps and search for “Incoming Webhooks”.
  • Activate the app: Click “Add to Slack”, choose the channel where you want the messages to appear, and press “Add Incoming Webhooks”.
  • Copy the webhook URL: This unique URL is the bridge between Google and Slack; keep it handy but never share it publicly.
  • Note the format: Slack expects a POST request with a JSON body containing at least a text field. To mention a user you’ll need the user’s Slack ID, not the display name.

Linking Google Forms to a Google Sheet

Google Forms stores responses automatically in a linked spreadsheet, which becomes the data source for your script.

  • Create the form: Design your questionnaire, ensuring you have a field that will hold the email or identifier of the person to be mentioned.
  • Connect the sheet: In the Form editor, click Responses > Create spreadsheet. Choose “Create a new spreadsheet” for a clean start.
  • Identify columns: Open the sheet and label the header row clearly (e.g., Responder Email, Issue Description). The script will reference these column names.
  • Set triggers: In the Sheet, go to Extensions > Apps Script. Later you’ll add an “on form submit” trigger so the script runs automatically.

Writing the Apps Script to Push Data to Slack

The core of the integration lives in a short Apps Script that formats the payload and fires the webhook.

  • Open the script editor: Extensions > Apps Script creates a default Code.gs file.
  • Sample function:
function sendToSlack(e) {
  var webhookUrl = 'YOUR_WEBHOOK_URL';
  var sheet = e.range.getSheet();
  var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
  var row = e.range.getRow() - 1;
  var data = sheet.getRange(row,1,1,sheet.getLastColumn()).getValues()[0];
  var payload = {};
  headers.forEach(function(col,i){ payload[col] = data[i]; });

  var userId = getSlackUserId(payload['Responder Email']);
  var message = '*New Form Submission*\n' +
                'Issue: ' + payload['Issue Description'] + '\n' +
                '<@' + userId + '> please review.';

  var options = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({text: message})
  };
  UrlFetchApp.fetch(webhookUrl, options);
}
  • Explanation: The function extracts the submitted row, builds a key‑value object, looks up the Slack user ID (see next chapter), composes a message with a mention, and sends it via UrlFetchApp.fetch.
  • Save and authorize: Run the function once to trigger the OAuth permission dialog; grant access to “Connect to an external service”.
  • Add the trigger: In the Apps Script UI, choose Triggers > Add Trigger, select sendToSlack, event type “On form submit”.

Tagging a Slack User – Using the Correct ID

Slack does not recognize plain email addresses or display names in mentions; it requires the internal user ID (e.g., U01ABCD2EFG). There are two reliable ways to obtain this ID.

  • Manual lookup: In Slack, open the user’s profile, click “More” > “Copy member ID”. Paste that ID into a hidden column in the Google Sheet, or store it in a lookup table that the script can query.
  • Programmatic lookup: Use Slack’s users.lookupByEmail API endpoint. You’ll need a Slack Bot token with the users:read.email scope. Add a helper function:
    function getSlackUserId(email) {
      var token = 'xoxb-YOUR_BOT_TOKEN';
      var url = 'https://slack.com/api/users.lookupByEmail?email=' + encodeURIComponent(email);
      var resp = UrlFetchApp.fetch(url, {
        headers: {Authorization: 'Bearer ' + token}
      });
      var json = JSON.parse(resp.getContentText());
      return json.ok ? json.user.id : '';
    }
    
  • Embedding the mention: In the final message string, wrap the ID with <@USERID>. Slack will render it as a clickable @‑mention and send a direct notification to that user.

Testing, Debugging, and Best Practices

After wiring everything together, a systematic test ensures reliability.

  • Submit a test entry: Fill out the form with a known email address. Verify the message appears in the chosen Slack channel and that the correct user is highlighted.
  • Check execution logs: In Apps Script, open View > Executions. Look for errors such as “Invalid webhook URL” or “User not found”.
  • Rate‑limit awareness: Slack limits incoming webhook calls to 1 per second per workspace. If your form receives bursts of submissions, consider batching or adding a short Utilities.sleep(500) between calls.
  • Security tip: Store the webhook URL and Bot token in the script’s Properties Service instead of hard‑coding them. This keeps credentials out of version control.
  • Future enhancements: Expand the payload to include attachments, emojis, or interactive buttons, and use conditional logic to mention different users based on form answers.

Conclusion

By creating a Slack incoming webhook, linking a Google Form to a Sheet, and crafting a concise Apps Script, you can automatically push form submissions into Slack and directly @‑mention the right teammate. The key is retrieving the correct Slack user ID—either manually or via the users.lookupByEmail API—and embedding it in the message payload using the <@USERID> syntax. Once the trigger is set, every new response becomes an instant, actionable alert for your team, reducing lag and improving collaboration. With the testing steps and best‑practice tips outlined, you now have a robust, scalable solution that turns ordinary forms into a dynamic communication channel. Happy automating!

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Digital Malayali