簡體   English   中英

如何使用 Native API 發送 Telegram Bold 消息?

[英]How to send Telegram Bold message using the Native API?

我想使用本機 Python 請求發送電報消息,以便在使用BOLD時收到“名字”。 我試過 HTML 和 Markdown 語法,但沒有顯示。

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
    }

Telegram 支持 HTML 或 Markdown 格式。 對於格式化,您必須將parse_mode參數設置為HTMLMarkdown (舊版)或MarkdownV2

例如:要將 output 設為“粗體文本”,您可以使用,
text=<b>bold text</b>&parse_mode=HTML
text=*bold text*&parse_mode=Markdown
text=*bold text*&parse_mode=MarkdownV2

您可以簡單地在代碼片段中進行更改;

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
    ...

在此處閱讀有關格式化選項的更多信息: https://core.telegram.org/bots/api#formatting-options

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM