Calling a Telegram Token from an external file

纵饮孤独 提交于 2019-12-13 05:06:47

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/51222123/calling-a-telegram-token-from-an-external-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!