And*_*Che 5 python python-2.7 python-telegram-bot telegram-bot
我一直在使用Telegram机器人使用python的telebot库从台式计算机向我发送不同的通知。一切工作了很长时间,但是有一天它停止了工作。
这是代码(Python 2.7):
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!')
Run Code Online (Sandbox Code Playgroud)
当我尝试在解释器中执行此操作时,得到以下信息:
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"}'}
Run Code Online (Sandbox Code Playgroud)
似乎我将在任何请求上收到此错误
>>> bot.get_me()
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}
Run Code Online (Sandbox Code Playgroud)
我也尝试在浏览器中使用直接HTTPS Telegram机器人API-输入
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test
Run Code Online (Sandbox Code Playgroud)
在地址栏中,它就完成了!
它还可以与Python的请求库一起使用
>>> import requests
>>> res = requests.get('https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test')
Run Code Online (Sandbox Code Playgroud)
最后,它可以在两个服务器(VDS)上使用完全相同的代码,而不会出现任何麻烦。
我最近安装了scapy,如果与此有关(可能是引起问题了?)
编辑:卸载Scapy没有帮助。
我尝试重新启动计算机和路由器,但没有任何改变。
我的电脑怎么了?
编辑:
在查看dir(bot)时,超过了bot对象的config属性。
>>> bot.config
{'requests_kwargs': {'timeout': 60}, 'api_key': None}
Run Code Online (Sandbox Code Playgroud)
设置api_key可解决问题
>>> bot.get_me()
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}
>>> bot.config['api_key'] = TELEBOT_TOKEN
>>> bot.get_me()
{u'ok': True, u'result': {u'username': u'Andys96NotificationsBot', u'first_name': u'NotificationsBot', u'id': <hidden>}}
Run Code Online (Sandbox Code Playgroud)
bot.send_message也开始正常工作。
希望这篇文章能对某人有所帮助。
小智 7
bot = telebot.TeleBot(TOKEN)
bot.config['api_key'] = TOKEN
Run Code Online (Sandbox Code Playgroud)