def telegram(channel, bot, text, img, topic_message_id=""):
base_url = f'https://api.telegram.org/bot{bot}/sendMessage'
chat_id = f'@{channel}' # Used for regular channel messages
if not text:
text = "Refer - "
# Function to send message
def send_message(image_link):
message_text = f"[]({image_link}){text}" if image_link else text
params = {
'chat_id': chat_id,
'parse_mode': 'MarkdownV2',
'text': message_text,
}
# If a topic message ID is provided, add it to the parameters to reply within a topic
if topic_message_id:
params['reply_to_message_id'] = topic_message_id
# For topics, the chat_id needs to be numeric. Extract numeric ID from the update if available.
# This assumes the caller has the correct chat ID format for topics.
response = requests.get(base_url, params=params)
return response.text
responses = []
if isinstance(img, list):
# Send a message for each image link in the list
for image_link in img:
response = send_message(image_link)
responses.append(response)
else:
# Handle single image link or empty img
response = send_message(img)
responses.append(response)
return responses if isinstance(img, list) else responses[0]
Type "" if you are sending to group or channel. Add the message_id if you want to reply to a topic.
telegram('unofficedtrades','your_token_here',"Amit","",32576)
will work fine.
message_thread_id
through an update containing Messge object. Note that there is currently no way to "get"message_thread_id
explicitly – Capernaum