Skip to content

Instantly share code, notes, and snippets.

@sulincix
Created April 3, 2024 11:04
Show Gist options
  • Save sulincix/40d1f921b93ad38d779cfa26c04ad33c to your computer and use it in GitHub Desktop.
Save sulincix/40d1f921b93ad38d779cfa26c04ad33c to your computer and use it in GitHub Desktop.
telegram-notify.py
#!/usr/bin/env python3
import sys
import gi
from gi.repository import GLib
import dbus
from dbus.mainloop.glib import DBusGMainLoop
token = ""
id = ""
def notifications(bus, message):
title = message.get_args_list()[3]
msg = message.get_args_list()[4]
tg_send(f"{title}\n{msg}")
import subprocess
import urllib.parse
def tg_send(msg):
print(msg)
msg = urllib.parse.quote_plus(msg)
arg = ["wget", "-O", "/dev/null", f"https://api.telegram.org/bot{token}/sendMessage?chat_id={id}&text={msg}"]
print(arg)
subprocess.run(arg)
if len(sys.argv) >= 3:
token = sys.argv[1]
id = sys.argv[2]
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'")
bus.add_message_filter(notifications)
mainloop = GLib.MainLoop()
mainloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment