skip to Main Content

i started learning new stuff about programming a bot in Telegram so i wrote my first lines, but when it comes to giving it a try i keep getting some errors so here is my code and the erros i keep getting …

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()

after i run it i get this :

Traceback (most recent call last):
  File "C:UsersraccoonDesktopCoding roomPython 3.9TelegramBotmain.py", line 12, in <module>
    bot.polling()
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebot__init__.py", line 496, in polling
    self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout)
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebot__init__.py", line 555, in __threaded_polling
    raise e
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebot__init__.py", line 517, in __threaded_polling
    polling_thread.raise_exceptions()
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebotutil.py", line 87, in raise_exceptions
    raise self.exception_info
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebotutil.py", line 69, in run
    task(*args, **kwargs)
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebot__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:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebot__init__.py", line 292, in get_updates
    json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates, long_polling_timeout)
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebotapihelper.py", line 281, in get_updates
    return _make_request(token, method_url, params=payload)
  File "C:UsersraccoonAppDataLocalProgramsPythonPython39libsite-packagestelebotapihelper.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]

i literally have no idea how to get through this, please if someone could help!

2

Answers


  1. From what I can see from the error seems like you API_TOKEN is not on the environment of your computer.

    You have two (?) options:

    1. Add the API_TOKEN in yor environment, in the case of windows this can be done using set API_TOKEN your_api_key or export API_TOKEN=your_api_key on Linux

    2. Change your code harcoding the API_KEY directly

    API_KEY = your_api_key
    bot = telebot.TeleBot(API_KEY)
    
    Login or Signup to reply.
  2. you need to load your environment variables first from .env file in the root directory of your project, use python-dotenv.

    import os
    from dotenv import load_dotenv
    
    load_dotenv()
    API_KEY = os.getenv("API_KEY")
    

    this should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search