可以通过 Telegram API 发送消息。浏览器但不是 python 脚本

Can send a message using the Telegram API via. the browser but not a python script

我可以在浏览器中使用以下 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)

Returns:

{'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

我的猜测是您使用了 tele_bot 软件包,这不是官方软件包,因此可能有问题并且无法正常工作。 所以你的修复删除了坏包(pip3 uninstall...),下面几行安装了合适的(官方)包。

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

旁注:你提供了 tele_bot 包和你的 API 私钥,我没有检查那个包,但它可能是恶意的并偷走了你的 API 令牌。所以我建议使旧的 API 令牌失效并生成一个新令牌。