Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active March 26, 2023 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/11624f89739dedce9a78d9d27a049fa8 to your computer and use it in GitHub Desktop.
Save juanpabloaj/11624f89739dedce9a78d9d27a049fa8 to your computer and use it in GitHub Desktop.
python3 send telegram message
import os
import urllib.request
import urllib.parse
TELEGRAM_TOKEN = os.environ["TELEGRAM_TOKEN"]
TELEGRAM_CHANNEL_ID = os.environ["TELEGRAM_CHANNEL_ID"]
telegram_url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
telegram_url += f"?chat_id={TELEGRAM_CHANNEL_ID}"
def send_message(message):
if not TELEGRAM_TOKEN or not TELEGRAM_CHANNEL_ID:
return
message = urllib.parse.quote(message)
url = f"{telegram_url}&text={message}"
urllib.request.urlopen(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment