Last active
March 26, 2023 13:26
-
-
Save juanpabloaj/11624f89739dedce9a78d9d27a049fa8 to your computer and use it in GitHub Desktop.
python3 send telegram message
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
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