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