为什么电报机器人不发送消息?

问题描述 投票:0回答:1

为什么机器人不发送消息? 我的代码:

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

python telegram telegram-bot python-telegram-bot
1个回答
0
投票

在我的电报机器人中,我使用下一个配置

    #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 并比较您请求中的内容。

© www.soinside.com 2019 - 2024. All rights reserved.