Skip to content

Instantly share code, notes, and snippets.

@sergiocasero
Last active September 29, 2022 11:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiocasero/442d9b1e7746058026f824f8069368ad to your computer and use it in GitHub Desktop.
Save sergiocasero/442d9b1e7746058026f824f8069368ad to your computer and use it in GitHub Desktop.
A simple, lightweight but powerfull telegram bot that sends your public IP through telegram in case it changes, really useful if you have dynamic IPs but also have a private cloud
# Every minute, perfect for IP_INFO free tier
* * * * * /usr/bin/python3 /PATH_TO_THE_SCRIPT/ipbot.py
import telebot
from telebot import types
import os
import requests
import json
TOKEN = 'BOT_TOKEN'
ip = "YOUR_INITIAL_IP"
def get():
endpoint = 'https://ipinfo.io/json?token=IPINFO_TOKEN'
response = requests.get(endpoint, verify = True)
if response.status_code != 200:
return '' # 'Status:', response.status_code, 'Problem with the request. Exiting.'
exit()
data = response.json()
return data['ip']
bot = telebot.TeleBot(TOKEN)
cid = YOUR_CHAT_TELEGRAM_ID
newIp = get()
previous = open("ROUTE_TO_THE_FILE/ip", "r").read()
if(newIp != previous and newIp != ""):
f = open('ROUTE_TO_THE_FILE/ip','w')
print(newIp)
f.write(newIp)
f.close()
bot.send_message(cid, newIp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment