[Python] How to solve telegram invalid token due to additional newline character?

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a project with python-telegram-bot where I'm encountering some error. My develop environment is centos7 and python3.6.8

    Below contents is code where the errors occure:

    import telegram
    from telegram.ext import Updater
    from telegram.ext import CommandHandler
    from telegram.ext import MessageHandler, Filters
    from main import *

    with open(r"/home/inseo/바탕화면/weather_report/telegram_key.txt","r") as file:
    token = file.readline()


    wp = Weather_Report()

    updater = Updater(token=token, use_context=True)
    dispatcher = updater.dispatcher


    The file telegram_api.txt contains the api key that I was given by telegram_chatbot. This code is executing normally in windows10, but when I'm going to run this code in linux(centos7) errors occure like below:

    [root@localhost weather_report]# python3 message.py
    Traceback (most recent call last):
    File "message.py", line 13, in <module>
    updater = Updater(token=token, use_context=True)
    File "/root/.local/lib/python3.6/site-packages/telegram/ext/updater.py", line 290, in __init__
    else arbitrary_callback_data
    File "/root/.local/lib/python3.6/site-packages/telegram/ext/extbot.py", line 103, in __init__
    private_key_password=private_key_password,
    File "/root/.local/lib/python3.6/site-packages/telegram/bot.py", line 191, in __init__
    self.token = self._validate_token(token)
    File "/root/.local/lib/python3.6/site-packages/telegram/bot.py", line 347, in _validate_token
    raise InvalidToken()
    telegram.error.InvalidToken: Invalid token


    I checked that the api key is right, but it doesn't work well. How to solve this problem?

    Continue reading...

Compartilhe esta Página