从外部文件调用电报令牌 [英] Calling a Telegram Token from an external file
问题描述
我似乎找不到与此相关的任何信息,而且我知道几个月前我有一个解决方案.我想用 python-telegram-bot 编写一个 Telegram Bot,从任何(合法)网站下载视频并将其发送给用户.
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.
机器人应该询问用户他想要视频、音频(mp3)还是 GIF(无音频的 mp4).这应该通过内联键盘发生.我正在摆脱我最初的问题...
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...
我不想在脚本中包含令牌,因为我可能会与其他人共享它以获得更具体的帮助.那么如何从外部token.txt"调用令牌以在我的 python 脚本中使用?
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?
推荐答案
你的意思是这样的?
import os
with open(os.path.dirname(os.path.realpath(__file__)) + '/token.txt') as file:
TOKEN = file.readline().strip()
updater = Updater(TOKEN)
我假设这是在runner"脚本中,该脚本应与令牌文本文件位于同一目录中.dirname/realpath
的东西是让它工作,即使你从另一个目录调用运行脚本.
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.
我对我的机器人做了同样的事情,我将 token.txt
添加到 .gitignore
;-)
I do the same thing with my bot and I added the token.txt
to .gitignore
;-)
这篇关于从外部文件调用电报令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!