[英]How to send message to unsaved number using Telegram API(Telethon)
[英]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
參數設置為HTML
、 Markdown
(舊版)或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.