Skip to content

Instantly share code, notes, and snippets.

@erral
Forked from juananpe/tg.py
Last active July 6, 2021 21:56
Show Gist options
  • Save erral/1470c70af04afc8e06ff5a7ed30d9622 to your computer and use it in GitHub Desktop.
Save erral/1470c70af04afc8e06ff5a7ed30d9622 to your computer and use it in GitHub Desktop.
Telegram notifier
# Twutter: https://twitter.com/juanan/status/1363860121676353537
# pip install python-telegram-bot
# Haria desagertzen bada ere, hemen azalpena:
# 1) Si no lo has creado aún, crea tu bot usando @BotFather en Telegram y copia tu APITOKEN
# 2) Obtén un webhook de usar y tirar aquí: https://webhook.site Te asignará una URL aleatoria, guárdala
# 3) Asigna el webhook a tu bot (la URL de http://webhook.site) https://api.telegram.org/botAPITOKEN/setWebhook?url=https://webhook.site...
# 4) Copia este mini script python a tu home directory https://gist.github.com/juananpe/edee47d41ed16c0a00eb820e0135bed3
# Donde pone CHAT_ID pon el ID de tu usuario Telegram
# Si no sabes cuál es, envía un mensaje al bot desde un cliente Telegram y verás tu ID en http://webhook.site
# 5) Crea el fichero ~/telegram.token con el APITOKEN de tu bot
# 6) Instala el módulo python-telegram-bot; $ pip3 install python-telegram-bot --user
# 7) Listo! $ python3 http://tg.py kaixo mundua
import telegram
import sys
CHAT_ID="XXXXXXX"
def get_credentials(file_path):
f = open(file_path)
return f.readline().strip()
def main():
cred = get_credentials("/ruta/a/telegram.token")
message = " ".join(sys.argv[1:])
bot = telegram.Bot(token=cred)
bot.send_message(chat_id=CHAT_ID, text=message)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment