gpt4 book ai didi

python telebot得到了意外的响应

转载 作者:行者123 更新时间:2023-12-03 14:50:17 27 4
gpt4 key购买 nike

我一直在使用我的 Telegram bot 使用 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!')

当我尝试在解释器中执行此操作时,我得到以下信息:
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"}'}

似乎我会根据任何请求收到此错误
>>> bot.get_me()
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}

我还尝试在浏览器中使用直接 HTTPS Telegram bot API - 输入了这个
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test

在地址行中,它做到了!

它也适用于 Python 的请求库
>>> import requests
>>> res = requests.get('https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test')

最后,它可以在我的两台服务器 (VDS) 上使用完全相同的代码,没有任何问题。

我最近安装了 scapy,如果它与此有关(也许是它导致了问题?)

编辑:卸载 scapy 没有帮助。

我尝试重新启动计算机和路由器,但没有任何改变。

我的电脑有什么问题?

编辑:

查看 dir(bot) 时发现了 bot 对象的 config 属性。
>>> bot.config
{'requests_kwargs': {'timeout': 60}, 'api_key': None}

设置 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>}}

bot.send_message 也开始正常工作。

希望这篇文章对某人有所帮助。

最佳答案

您的回答对我有所帮助,但对于不会阅读问题的其他人:

bot = telebot.TeleBot(TOKEN)
bot.config['api_key'] = TOKEN

关于python telebot得到了意外的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42752391/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com