python telebot得到了意外的响应 [英] python telebot got unexpected response
问题描述
我一直在使用我的 Telegram bot 从我的台式计算机使用 python 的 Telebot 库向我发送不同的通知.一切正常运行了很长时间,但有一天它停止了工作.
这是代码(Python 2.7):
导入远程机器人进口插座TELEBOT_TOKEN = '<令牌>'CHAT_ID = <聊天ID>bot = telebot.TeleBot(TELEBOT_TOKEN)定义通知(消息):bot.send_message(CHAT_ID, '通知来自' + socket.gethostname() + ':\n' + message)通知('你好世界!')
当我尝试在解释器中执行此操作时,我得到了:
Python 2.7.12(默认,2016 年 11 月 19 日,06:48:10)[GCC 5.4.0 20160609] 在 linux2输入帮助"、版权"、信用"或许可"以获取更多信息.>>>进口远程机器人>>>TELEBOT_TOKEN = '<令牌>'>>>CHAT_ID = <聊天ID>>>>bot = telebot.TeleBot(TELEBOT_TOKEN)>>>bot.send_message(CHAT_ID, 'Hello world!'){'ok': False, 'error': '得到意外响应.(404) - {"ok":false,"error_code":404,"description":"Not Found"}'}
似乎任何请求都会出现此错误
<预><代码>>>>bot.get_me(){'ok': False, 'error': '得到意外响应.(404) - {"ok":false,"error_code":404,"description":"Not Found"}'}我也尝试在浏览器中使用直接的 HTTPS Telegram bot API - 输入这个
https://api.telegram.org/bot/sendMessage?chat_id=&text=Test
在地址行中,它做到了!
它也适用于 Python 的请求库
<预><代码>>>>进口请求>>>res = requests.get('https://api.telegram.org/bot最后,它可以在我的两台服务器 (VDS) 上使用 非常相同的代码,没有任何问题.
我最近安装了 scapy,如果它与此有关(可能是它导致了问题?)
卸载 scapy 没有帮助.
我尝试重新启动计算机和路由器,但没有任何改变.
我的电脑有什么问题?
在查看 dir(bot) 时发现了 bot 对象的 config 属性.
<预><代码>>>>bot.config{'requests_kwargs': {'timeout': 60}, 'api_key': None}设置 api_key 解决问题
<预><代码>>>>bot.get_me(){'ok': False, 'error': '得到意外响应.(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>}}bot.send_message 也开始正常工作.
希望这篇文章对某人有所帮助.
你的回答帮助了我,但对于其他不会阅读问题的人:
bot = telebot.TeleBot(TOKEN)bot.config['api_key'] = 令牌
I have been using my Telegram bot for sending me different notifications from my desktop computer using python's telebot library. Everything was working properly for quite a long time, but one day it stopped working.
Here's the code (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!')
When I try doing this in the interpreter, I get this:
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"}'}
Seems that I will get this error on any request
>>> bot.get_me()
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}
I also tried using direct HTTPS Telegram bot API in the browser - typed this
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test
in the address line and it did the thing!
It also works with Python's requests library
>>> import requests
>>> res = requests.get('https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test')
And finally, it works with the very same code on my two servers (VDS) without any troubles.
I recently installed scapy, if it has anything to do with this (maybe it caused the problem?)
EDIT: uninstalling scapy didn't help.
I tried rebooting the computer, and the router, but nothing changed.
What can be wrong with my computer?
EDIT:
Came over the config attribute of the bot object while looking at dir(bot).
>>> bot.config
{'requests_kwargs': {'timeout': 60}, 'api_key': None}
Setting the api_key fixes the problem
>>> 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>}}
bot.send_message starts working normally as well.
Hope this post will help someone.
Your answer helped me, but for others who will not read question:
bot = telebot.TeleBot(TOKEN)
bot.config['api_key'] = TOKEN
这篇关于python telebot得到了意外的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!