2

I want to send a Telegram message using the native Python requests so that the "First Name" is received in using in BOLD. I have tried HTML and Markdown syntax but it's not showing.

import json
import requests

# Telegram bot configurations
TELE_TOKEN='TOKEN'
URL = "https://api.telegram.org/bot{}/".format(TELE_TOKEN)

def send_message(text, chat_id):
    url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
    requests.get(url)

# AWS Lambda handler    
def lambda_handler(event, context):
    
    message = json.loads(event['body'])
    chat_id = message['message']['chat']['id']
    first_name = message['message']['chat']['first_name']
    text = message['message']['text']

    # How can the first name be in BOLD?
    reply = "Hello " + first_name + "! You have said:" + text
    
    send_message(reply, chat_id)
    

    return {
        'statusCode': 200
    }
Andre
  • 598
  • 1
  • 7
  • 18

1 Answers1

4

Telegram supports formatting in either HTML or Markdown. For formatting, you have to set the parse_mode parameter to either HTML,Markdown (legacy), orMarkdownV2.

E.g.: To get the output as "bold text", you could use,
text=<b>bold text</b>&parse_mode=HTML or
text=*bold text*&parse_mode=Markdown or
text=*bold text*&parse_mode=MarkdownV2.

You could simply do that change in your code snippet;

def send_message(text, chat_id):
    url = URL + "sendMessage?text={}&chat_id={}&parse_mode=MarkdownV2".format(text, chat_id)
    requests.get(url)

# AWS Lambda handler    
def lambda_handler(event, context):
    ...
    # How can the first name be in BOLD?
    reply = "Hello *" + first_name + "*! You have said:" + text
    ...

Read more about the formatting options here: https://core.telegram.org/bots/api#formatting-options

Tharindu Sathischandra
  • 1,654
  • 1
  • 15
  • 37