This is my first time trying to make a telegram bot.
Code:
import os
import telebot
API_TOKEN = os.getenv('API_KEY')
bot = telebot.TeleBot(API_TOKEN)
@bot.message_handler(commands=['hello'])
def send_welcome(message):
bot.reply_to(message, "HI!")
bot.polling()
Error:
Traceback (most recent call last):
File "/Users/anshtyagi/Documents/telegram bot/main.py", line 23, in <module>
bot.polling()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 621, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 695, in __threaded_polling
raise e
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 650, in __threaded_polling
polling_thread.raise_exceptions()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/util.py", line 111, in raise_exceptions
raise self.exception_info
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/util.py", line 93, in run
task(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 360, in __retrieve_updates
updates = self.get_updates(offset=(self.last_update_id + 1),
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 338, in get_updates
json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates, long_polling_timeout)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/apihelper.py", line 324, in get_updates
return _make_request(token, method_url, params=payload)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/apihelper.py", line 80, in _make_request
raise Exception('Bot token is not defined')
Exception: Bot token is not defined
I am getting this error while running my telegram bot this is my first time. I have seen some tutorials how to make a bot but mine is not working. I have posted my token in .env file and imported it using os.getenv. I am just simply trying to make a simple bot just for my knowledge as I have tried making discord bots and it was great experience and I learned many new languages so I thought why not try this also.
2
Answers
try this:
import telebot
bot = telebot.TeleBot("API_KEY")
instead of:
import os
import telebot
API_TOKEN = os.getenv(‘API_KEY’)
bot = telebot.TeleBot(API_TOKEN)
I’m also new to python and telegram bots, but my first bot is running ok and I see the problem in your code. Your exception tells us
Exception: Bot token is not defined
. So your script doesn’t get a token from the system var.You say that you have your token in .env file. To get os variable from that file first of all we must import a module:
from dotenv import load_dotenv
.Next we must import variables from that file using the function
load_dotenv()
.After that we can read our variables with
os.getenv
. So that line of your code seems to be correct. If there is no variable in the fileos.getenv
should get it from the os.So your code may look like: