为什么机器人不发送消息? 我的代码:
import telegram
import requests
bot_token = 'token***'
my_bot = telegram.Bot(token=bot_token)
def get_chat_id(bot_token):
request = requests.post(f'https://api.telegram.org/bot{bot_token}/getUpdates')
result = request.json()['result'][0]['message']['chat']['id']
return result
chat_id = get_chat_id(bot_token)
msg = 'My PC'
my_bot.sendMessage(chat_id=chat_id, text=msg)
Python 3.11.5、电报 20.7
在我的电报机器人中,我使用下一个配置
#Token of the telegram bot
TOKEN = "yours"
#Id for the chat
chat_id = "the_chat_id"
msj_err = "Bot is alive"
requests.post('https://api.telegram.org/bot'+TOKEN+'/sendMessage?chat_id='+chat_id+'&text='+msj_err)
chat_id 是我需要手动设置的,因为有时它不接受它。现在这个机器人已经工作了 3 个月了。
我认为问题是聊天ID不正确。尝试提取聊天 ID 并比较您请求中的内容。