개발
쉘스크립트로 슬랙 메시지 보내는 방법
AimB
2021. 11. 18. 18:09
1. 먼저 앱을 만든다.
https://api.slack.com/start/overview#creating
An introduction to the Slack platform
Slack apps and workflows extend, expand, and automate your Slack workspace. Cultivate conversation, inspire action, and integrate services by building an app or workflow.
api.slack.com
2. Settings-Basic informations-하단의 App level token을 눌러 토큰을 생성한다.
3. 쉘스크립트를 작성한다.
채널 ID는 채널 이름을 클릭 후 하단부를 보면 알 수 있다.
TOKEN="xoxb-~~~"
message=""
curl -H "Content-type: application/json; charset=utf-8" \
--data '{"username": "bot", "channel":"[채널 ID]", "text":"sample message", "attachments": [{"text": '"\"${message}\""'}]}' \
-H "Authorization: Bearer $TOKEN" \
-X POST https://slack.com/api/chat.postMessage
Webhook url로 보내려면 아랫부분의 코드만 갈아끼우면 된다.
webhook_url="https://hooks.slack.com/services/~~~~"
channel=""
message="sample message"
curl -H "Content-type: application/json; charset=utf-8" \
--data '{"username": "bot", "channel":'"\"${channel}\""', "text":"<!channel>", "attachments": [{"text": '"\"${message}\""'}]}' \
-X POST ${webhook_url}