Created
October 22, 2020 04:52
-
-
Save feliperyan/a7b69c2d1818b8cec34dc5d4a2bf2fe4 to your computer and use it in GitHub Desktop.
Notify me on Slack when my external IP changes
This file contains 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
from urllib.request import urlopen | |
from urllib import request | |
resp = urlopen("https://api.ipify.org") | |
ip = resp.read().decode('utf-8').strip() | |
f = open('last_ip.txt', 'r+') | |
last_ip = f.read().strip() | |
if (ip != last_ip): | |
f.seek(0) | |
f.write(ip) | |
f.truncate() | |
print(f'Old ip: {last_ip} new ip: {ip}') | |
# call webhook | |
data_utf8 = f'{{"text": "New IP: *{ip}*"}}' | |
data_utf8 = data_utf8.encode('utf-8') | |
req = request.Request('https://hooks.slack.com/services/your_long_string_for_webhooks_here', data=data_utf8) | |
resp = request.urlopen(req) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment