Python Telegram 机器人错误

Python Telegram Bot errors

我开始学习有关在 Telegram 中编写机器人程序的新知识,所以我写了第一行,但是在尝试时,我不断遇到一些错误,所以这是我的代码和我不断遇到的错误 .. .

import telebot , config , os

API_KEY = os.getenv("API_KEY")
bot = telebot.TeleBot(API_KEY)

@bot.message_handler(commands=['Greet'])

def greet(message):
    bot.reply_to(message,"Hey, how's it going?")


bot.polling()

在我 运行 之后我明白了 :

Traceback (most recent call last):
  File "C:\Users\raccoon\Desktop\Coding room\Python 3.9\TelegramBot\main.py", line 12, in <module>
    bot.polling()
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 496, in polling
    self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout)
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 555, in __threaded_polling
    raise e
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 517, in __threaded_polling
    polling_thread.raise_exceptions()
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 87, in raise_exceptions
    raise self.exception_info
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 69, in run
    task(*args, **kwargs)
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 322, in __retrieve_updates
    updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout, long_polling_timeout = long_polling_timeout)
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 292, in get_updates
    json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates, long_polling_timeout)
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\apihelper.py", line 281, in get_updates
    return _make_request(token, method_url, params=payload)
  File "C:\Users\raccoon\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\apihelper.py", line 76, in _make_request
    logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files).replace(token, token.split(':')[0] + ":{TOKEN}"))
AttributeError: 'NoneType' object has no attribute 'split'
[Finished in 1.3s]

我真的不知道怎么解决这个问题,如果有人能帮忙的话请帮忙!

从错误中我可以看出,您 API_TOKEN 不在您计算机的环境中。

您有两个(?)选项:

  1. 在您的环境中添加 API_TOKEN,在 windows 的情况下,可以使用 set API_TOKEN your_api_keyexport API_TOKEN=your_api_key on [=31] 来完成=]

  2. 直接更改 API_KEY 的代码

API_KEY = your_api_key
bot = telebot.TeleBot(API_KEY)

你需要先从项目根目录下的.env文件加载你的环境变量,使用python-dotenv.

import os
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.getenv("API_KEY")

这应该有效。