簡體   English   中英

如何在 Python 中的電報發送消息機器人中使用解析模式

[英]How to use parse mode in telegram send message bot in Python

我對制作機器人很陌生,所以我有幾個問題想問:

  1. 我正在使用機器人發送消息

     def send_message(chat_id, msg): url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}" requests.get(url) send_message(chat_id,msg)

通過這個,我可以發送消息,我想通過使用 parse_mode 美化結果,但我不知道在 url 中的哪個位置使其工作。

  1. 我替代使用 Telethon。 我可以使用它通過用戶名和組邀請鏈接向個人和組發送消息,但是當我嘗試通過以下方式將其發送到頻道時:

    client.send_message(entity='我的頻道名稱', message=message)

當我嘗試運行它時,它返回找不到與“測試通道”相對應的任何實體。

我也試試

destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")

但它需要我的頻道access_hash,如何獲取它,或者有沒有其他方法可以向頻道發送消息。

  1. 我知道 Telegram bot API 有 function 像 sendMessage 或 bot.sendMessage 也可以完成這項工作,但不知何故我不能調用它,我應該安裝和/或導入哪些包。

非常感謝!

  1. 這應該可以解決問題:
def send_message(chat_id, msg, parse_mode):
    url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}&parse_mode={parse_mode}"
    requests.get(url)
  1. TBH 我對 Telethon 不太熟悉,但該庫主要用於所謂的用戶機器人。 這意味着它使用了 Telegram 應用程序也使用的 API,因此,它通常比普通的機器人 api 更復雜。

  2. To use the Bot API in Python, I can recommend the package python-telegram-bot , see https://python-telegram-bot.org . 免責聲明:我目前是該 package 的維護者。 機器人 api 還有其他 python 包,參見例如https://core.telegram.org/bots/samples#python

暫無
暫無

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

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