Tom*_*Tom 7 python telegram python-telegram-bot telegram-bot
我创建了一个电报机器人并将其添加到我的电报频道中。现在,当我的 python 程序中发生某些情况时,我想使用它向我的频道发送消息。例如,我有一个 python 程序,每 15 秒检查一次天气,当天气发生变化时,我希望我的机器人将新的天气信息发送到我的电报频道。
所以我的问题是,我该怎么做?我陷入困境,因为 python-telegram-bot 需要用户发送消息才能触发,或者需要预定订单,而我无法安排它,因为我不知道天气何时会发生变化。
Que*_*beh 11
最简单的方法是使用 request 方法。Telegram 提供了一个很酷的 API 来使用您的机器人发送消息,您需要使用链接来执行此操作,例如:
https://api.telegram.org/bot<yourbottoken>/sendMessage?chat_id=<yourchatid>&text=Hello World!
Run Code Online (Sandbox Code Playgroud)
它的作用是将 Hello World 消息发送到某个聊天 ID。如果您不知道如何获取聊天 ID,则需要向您的机器人发送 DM,并且可以使用以下链接:
https://api.telegram.org/bot<yourbottoken>/getUpdates
Run Code Online (Sandbox Code Playgroud)
在页面中,会有相当多的JSON数据,你需要使用Control + F搜索你的telegram用户名(不带@)并搜索聊天id
如果你想在 python 代码中执行此操作,则需要使用该requests
模块。
import requests
requests.post('https://api.telegram.org/bot<yourbottoken>/sendMessage?chat_id=<yourchatid>&text=Hello World!')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16326 次 |
最近记录: |