Created
August 7, 2023 11:01
-
-
Save Miyu-dev/74bc5c29bc5eaa407bedf4a453d08956 to your computer and use it in GitHub Desktop.
Slack Appsにメンションすると反応するだけのGAS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function doPost(e) { | |
| var postData = JSON.parse(e.postData.getDataAsString()); | |
| if(postData.type == 'url_verification') { | |
| return ContentService.createTextOutput(postData.challenge); | |
| } | |
| // Slackからのメッセージを取得 | |
| // app_mention | |
| // https://api.slack.com/events/app_mention | |
| var payload = JSON.parse(e.postData.contents); | |
| var user = payload.event.user; | |
| var text = payload.event.text; | |
| var ts = payload.event.ts; | |
| var channel = payload.event.channel; | |
| var event_ts = payload.event.event_ts; | |
| var botUserId = "XXXX" | |
| if (text.includes(`<@${botUserId}>`)) { | |
| postMessage("こんにちは", channel,ts); | |
| } | |
| } | |
| function postMessage(message, channelId, ts) { | |
| // Slackにメッセージを投稿 | |
| var slackToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
| var slackApiUrl = "https://slack.com/api/chat.postMessage"; | |
| var payload = { | |
| method: "post", | |
| headers: { "Authorization": "Bearer " + slackToken }, | |
| contentType: "application/json", | |
| payload: JSON.stringify({ | |
| text: message, | |
| channel: channelId, | |
| thread_ts: ts | |
| }) | |
| }; | |
| UrlFetchApp.fetch(slackApiUrl, payload); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment