我一直在使用我的Telegram机器人使用python的telebot库从我的桌面计算机向我发送不同的通知。一切都运作了很长时间,但有一天它停止了工作。
这是代码(Python 2.7):
global.h
当我尝试在解释器中执行此操作时,我得到了这个:
import telebot
import socket
TELEBOT_TOKEN = '<token>'
CHAT_ID = <chat id>
bot = telebot.TeleBot(TELEBOT_TOKEN)
def notify(message):
bot.send_message(CHAT_ID, 'Notification from ' + socket.gethostname() + ':\n' + message)
notify('Hello world!')
似乎我会在任何请求中收到此错误
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import telebot
>>> TELEBOT_TOKEN = '<token>'
>>> CHAT_ID = <chat id>
>>> bot = telebot.TeleBot(TELEBOT_TOKEN)
>>> bot.send_message(CHAT_ID, 'Hello world!')
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}
我也尝试在浏览器中使用直接HTTPS Telegram bot API - 键入此
>>> bot.get_me()
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}
在地址栏中它做了那件事!
它也适用于Python的请求库
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test
最后,它可以与我的两台服务器(VDS)上的完全相同的代码一起工作,没有任何麻烦。
我最近安装了scapy,如果它与此有任何关系(可能会导致问题?)
编辑:卸载scapy无济于事。
我尝试重新启动计算机和路由器,但没有任何改变。
我的电脑出了什么问题?
修改
在查看dir(bot)时,查看了bot对象的config属性。
>>> import requests
>>> res = requests.get('https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test')
设置api_key可以解决问题
>>> bot.config
{'requests_kwargs': {'timeout': 60}, 'api_key': None}
bot.send_message也开始正常工作。
希望这篇文章能帮助别人。
答案 0 :(得分:1)
您的回答对我有所帮助,但对于那些不会读问题的人而言:
bot = telebot.TeleBot(TOKEN)
bot.config['api_key'] = TOKEN