Search code examples
tokenpython-telegram-bot

Calling a Telegram Token from an external file


I couldn't seem to find anything regarding this and I know I had a solution months ago. I want to write a Telegram Bot with python-telegram-bot to download videos from any (legal) website and send it to the user.

The Bot should ask the User whether he wants a Video, Audio(mp3) or a GIF(mp4 w/o audio). This should happen via Inline Keyboard. I'm getting away from my initial question...

I don't want to have the Token inside the script, as I might share it with others for more specific help. So how do I call the token from an external "token.txt" to be used within my python script?


Solution

  • You mean something like this?

    import os
    with open(os.path.dirname(os.path.realpath(__file__)) + '/token.txt') as file:
        TOKEN = file.readline().strip()
    updater = Updater(TOKEN)
    

    I'm assuming that this is inside the "runner" script which shall be in the same directory as the token text file. The dirname/realpath stuff is to make it work even if you call the runner script from another directory.

    I do the same thing with my bot and I added the token.txt to .gitignore ;-)