skip to Main Content

I’m working on a Telegram bot using the python-telegram-bot library, and I’m encountering an error that says "telegram.error.InvalidToken: You must pass the token you received from https://t.me/Botfather!". I’ve double-checked my token from BotFather and it’s correct, so I’m not sure why I’m getting this error.

Here’s the relevant portion of my code:

import os
import openai
from telegram import Update, Bot
from telegram.ext import Updater, CallbackContext, MessageHandler, filters
from dotenv import load_dotenv

load_dotenv()

TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

I’ve also ensured that the .env file contains the correct token. The error occurs when I try to create the Bot and Updater instances. I’m using the correct version of the library (latest version) and I’m following the official documentation.

Am I missing something? Could someone please help me identify the root cause of this issue and provide suggestions for resolving it? Thank you in advance for your assistance!

2

Answers


  1. Chosen as BEST ANSWER

    My .env file was called token.env, my problem was gone just calling the file just .env


  2. My guess is that the token is just not loaded correctly. Try logging/printing the value of TELEGRAM_TOKEN to check that.

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