I’m trying to send notifications to multiple Telegram chats using a Python script. The Firebase notifications part works fine, but the Telegram notifications do not get sent. Here’s the relevant part of my code:
import requests
TG_BOT_TOKEN = ""
TG_CHAT_IDS = [chatid1,chatid2 ]
def send_telegram_message(chat_id, title, body):
url = f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage"
payload = {
'chat_id': chat_id,
'text': f"{title}n{body}"
}
response = requests.post(url, data=payload)
response.raise_for_status() # Raises an HTTPError for bad responses
return response.json()
title = "Test Title"
body = "Test Body"
for cht in TG_CHAT_IDS:
try:
telegram_response = send_telegram_message(chat_id=cht, title=title, body=body)
print(f"Message sent to {cht} - RESPONSE: {telegram_response}")
except requests.RequestException as e:
print(f"Failed to send message to {cht}: {e}")
-
Problem:
-
The script prints “Message sent to {chat_id} – RESPONSE: {response}” but the messages do not appear in the specified Telegram chats.
-
No exceptions are raised, so the requests appear to be successful, but no messages are received.
What I’ve Checked: -
The TG_BOT_TOKEN is correct and the bot has been added to the specified chats.
-
The TG_CHAT_IDS contain valid chat IDs (one is a group chat, the other is a direct user chat).
-
The requests library is installed and working properly.
Additional Info:
-
Running on Python 3.x.
-
The Firebase notifications part of the script works correctly.
-
No HTTP errors are being raised by response.raise_for_status().
-
The main purpose of this is receiving on telegram the same notifications that are from our app whcih are using firebase, firebase notifications are working, telegram isn’t
Everything is running on a websocket server that we use to handle push notifications for firebase
Desmond R oliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.