转换URL外观并通过电报机器人发送

2024-04-18 05:11:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用电报机器人从python脚本发送信息

我向电报机器人发送信息的功能是:

def telegram_bot_sendtext(bot_message):

    bot_token = 'token'
    bot_chatID = 'id'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
    response = requests.get(send_text)

    return response.json()

其中一个信息是一个名为“URL”的变量,它包含URL,例如,这一个:https://www.sezane.com/fr/product/collection-printemps-all-0804/robe-will?cou_Id=859

我有两个问题:

  1. 当我使用其他变量时,一切正常。但是,发送URL变量时,不会发生任何事情,也不会收到任何消息
  2. 如何才能将链接接收为“短名称链接”,而不是整个链接?例如,接收“Google”而不是“www.Google.com”

Tags: texthttpstokensendidurlmessage链接
1条回答
网友
1楼 · 发布于 2024-04-18 05:11:41

1)markdownparseMode需要有效的标记语法,我猜示例url中的_会启动一个从未关闭的Markdown italic text,电报返回一个错误,消息将不会发送。使用^{}禁用此选项。(html解析模式中没有_元素)

2)markDownHTML解析模式都提供了向消息添加超链接的选项

HTML

<a href="https://www.google.com/">Google</a>

降价

[Google](https://www.google.com/)
telegram_bot_sendtext("Please press this [link](https://www.sezane.com/fr/product/collection-printemps-all-0804/robe-will?cou_Id=859)")

有关更多信息,请查看那里的documentation

相关问题 更多 >