0

我可以在浏览器中使用以下 url 向包含我自己的机器人的电报频道发送消息:

https://api.telegram.org/bot*****/sendMessage?chat_id=-********&text=Hello%20World! #there's a - before the chat id value

但是,以下python代码:

import telebot

API_TOKEN = '*********'
bot = telebot.TeleBot(API_TOKEN)
chat_id='-*******'

try:
    text_telegram='testing'
    print('line1')  
    print(bot.send_message(chat_id, text_telegram))
    print('line2')  
except Exception as e:
    print(e)

回报:

{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}

更新:

所以运行以下命令解决了我的问题。有人可以解释为什么我必须运行它以及最后一行的原因是什么?当然第三行安装的是最新版本?

pip3 uninstall telebot
pip3 uninstall PyTelegramBotAPI
pip3 install pyTelegramBotAPI
pip3 install --upgrade pyTelegramBotAPI
4

1 回答 1

1

我的猜测是你使用了 tele_bot 包,它不是官方的包,因此可能被窃听并且无法正常工作。所以你的修复删除了坏包(pip3 uninstall...),以下几行安装了适当的(官方)包。

假设您卸载了 PyTelegramBotAPI,重新安装应该就足够了。如果您没有卸载它,更新(第 4 行)是个好主意。

旁注:您为 tele_bot 包提供了您的私有 API 密钥,我尚未检查该包,但它可能是恶意的并窃取了您的 API 令牌。所以我建议使旧的 API 令牌无效并生成一个新的。

于 2022-02-05T17:17:41.227 回答