0

I have a Google Apps Script script from a spreadsheet to send an automatic message to slack (and an auto-reply email) when a Google Form answer is submitted. How do I send 2 slack messages in 1 script, one as a new message in a specific channel and another as a reply (thread) to the message I just sent? The current script runs as a single slack message like below and I want to split it into 2; sending the --REQUEST CONTENT-- part as a thread.

function autoReply(e) {
  var values = e.values;

  var name    = values[1];
  var email   = values[2];
  var phone   = values[3];
  var office  = values[4];
  var comment = values[5];
  
  var title = "EMAIL TITLE";
  var body = `
Dear ${name},

TEXT

-------------------------------------------------------

SIGNATURE

-------------------------------------------------------
  `;
  GmailApp.sendEmail(email, title, body);
  notifySlack(`
<!channel>

You received a Google Form request:

-----REQUEST CONTENT-----
・NAME: ${name}
・EMAIL: ${email}
・PHONE: ${phone}
・OFFICE: ${office}
・COMMENT: ${comment}
-------------------------

`);
}

function notifySlack(message) {
  
  var postUrl = 
"https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX";
  var userName = "[alert] FORM REQUEST";
  
  var payloadObj = {
    username:userName,
    text:message,
    icon_emoji : ":mega:"
  }
  
  var payloadJson = JSON.stringify(payloadObj);
  var options = {
    method:"post",
    contentType:"application/json",
    payload:payloadJson
  }
  UrlFetchApp.fetch(postUrl, options);
}

oki_oki
  • 89
  • 9
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand `How do I send 2 slack messages in 1 script, one as a new message in a specific channel and another as a reply (thread) to the message I just sent? The current script runs as a single slack message like below and I want to split it into 2; sending the --REQUEST CONTENT-- part as a thread.`. Can I ask you about the detail of your current issue and your goal? – Tanaike Jan 19 '22 at 01:24
  • @Tanaike Thank you so much. My issue and goal is: ISSSUE: - I can only send 1 slack message per 1 google forms answer - The message is sent as a new message in a specific channel - When the REQUEST CONTENT becomes too long for some answers, it is difficult to see all contents in a single slack message – oki_oki Jan 19 '22 at 02:34
  • GOAL: - Split the current slack message into 2 slack message - (1) First, only send the minimum message as a new message in slack. Specifically, the first 3 rows in the current in my `notifySlack` message - (2) Then, I would like to send a second message right after that. The content will be the REQUEST CONTENT part and I want it to be a reply/thread of (1) message – oki_oki Jan 19 '22 at 02:35
  • Thank you for replying. I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I would be grateful if you can forgive my poor English skill. – Tanaike Jan 19 '22 at 03:16
  • @Tanaike ありがとうございます。日本にお住まいの方のようなので、念の為日本語でもお伝えさせていただきます。Google Form の回答があったときに Slack 通知が 1 通送られる GAS スクリプトを運用しているのですが、これを 1 回の Form 回答で 2 通の Slack 通知が送られるように分けたいという課題です。REQUEST CONTENT の部分のテキストが長くなることがあるため、全てをまとめた 1 通の Slack メッセージだととても見づらいという現状の課題があります。これを、最初の 3 行(channel メンションと簡易テキスト)だけをチャンネルへの新規メッセージとして今まで通り投稿して、REQUEST CONTENT の部分をその新規メッセージのスレッドとして投稿できないかと考えている次第です。ご検討のほど、よろしくお願いいたします。 – oki_oki Jan 19 '22 at 08:14
  • Thank you for replying. In your goal, you want to send 2 texts to the same channel as 2 individual messages when Google Form is submitted. Is my understanding correct? – Tanaike Jan 19 '22 at 09:04
  • @Tanaike Yes, that is 100% correct. I want to send 2 texts to the same channel, but one as a simple new message in a channel and the second one in the thread of the first message. Is that possible? – oki_oki Jan 19 '22 at 10:20
  • Thank you for replying. When I saw your script, it seems that you are using the incoming webhook. In this case, about `one as a simple new message in a channel and the second one in the thread of the first message.`, this thread might be the answer for your question. https://stackoverflow.com/a/56291606/7108653 – Tanaike Jan 19 '22 at 12:02
  • @Tanaike Thank you very much for sharing. Will my goal be achievable if I don't use a webhook? Are there other options? – oki_oki Jan 19 '22 at 12:39
  • 1
    Thank you for replying. I had thought that the thread of https://stackoverflow.com/q/56291169/7108653 was the answer to your question. And the workaround mentioned in the thread is as follows. `In summary: webhooks do not support threads. If you want to do threading you need to post your messages via the API (e.g. chat.postMessage) and not use webhooks. Webhooks are just meant to offer an easy and quick way for posting messages, but they dont't offer the full functionality.`. – Tanaike Jan 19 '22 at 12:49
  • Noted, thank you very much for your support. – oki_oki Jan 20 '22 at 04:49
  • Thank you for replying. From your reply, I flagged your question as the duplicated question of https://stackoverflow.com/a/56291606 – Tanaike Jan 20 '22 at 05:08

0 Answers0