Last active
November 24, 2018 05:42
-
-
Save honux77/59c2efc28afc220a1c674e244971c940 to your computer and use it in GitHub Desktop.
slack + AWS example
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
#!/bin/bash | |
# put this in ec2 ubuntu user-data | |
mkdir /home/ubuntu/script/ | |
cat <<'EOF' >> /home/ubuntu/script/slack.sh | |
#!/bin/bash | |
ip=`/usr/bin/curl -s -w '\n' http://169.254.169.254/latest/meta-data/public-ipv4` | |
instance=`/usr/bin/curl -s -w '\n' http://169.254.169.254/latest/meta-data/instance-id` | |
url="WEBHOOK URL HERE" | |
name="YOUR NAME HERE" | |
payload="{\"text\": \"`date`: $name $instance ($ip) started\"}" | |
/usr/bin/curl -X POST -H 'Content-type: application/json' \ | |
--data "$payload" $url | |
EOF | |
chmod +x /home/ubuntu/script/slack.sh | |
cat <<'EOF' >> /etc/systemd/system/slack.service | |
[Unit] | |
Description=Slack IP Notification | |
Wants=network-online.target | |
After=network-online.target | |
[Service] | |
Type=oneshot | |
WorkingDirectory=/home/ubuntu/script | |
ExecStart=/home/ubuntu/script/slack.sh | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
systemctl enable slack.service | |
systemctl status slack | |
/home/ubuntu/script/slack.sh |
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
echo <<'EOF' > send-msg.sh | |
url="YOUR SLACK incoming-webhook URL HERE" | |
if [ $# -ge 1 ]; then | |
msg=$@ | |
else | |
msg="Hello! I am aws study bot" | |
fi | |
payload='{"text": "'$msg'"}' | |
echo Try send $payload to Slack channel | |
curl -X POST -H 'Content-type: application/json' \ | |
--data "$payload" $url | |
EOF | |
chmod +x ./send-msg.sh | |
./send-msg.sh |
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
import simplejson as json | |
import requests | |
webhook_url = 'hookhook" | |
msg = { | |
"attachments": [ | |
{ | |
"pretext": "message from honux-bot", | |
"title": "우아한 형제들", | |
"title_link": "http://www.woowahan.com/", | |
"text": "치킨은 살찌지 않아요.", | |
"color": "#7CD197" | |
} | |
] | |
} | |
response = requests.post( | |
webhook_url, data=json.dumps(msg), | |
headers={'Content-Type': 'application/json'}) | |
if response.status_code != 200: | |
raise ValueError( | |
'Request to slack returned an error %s, the response is:\n%s' | |
% (response.status_code, response.text) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment