feat: add token in config.ini
Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
6ec797a387
commit
1178c80831
4 changed files with 24 additions and 12 deletions
|
@ -26,7 +26,7 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
from main import config
|
||||
reply_markup = None
|
||||
source_url = config.get('Source', 'Url')
|
||||
source_url = config.get('General', 'SourceUrl')
|
||||
if source_url.startswith('http://') or source_url.startswith('https://'):
|
||||
keyboard = [
|
||||
[
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
[General]
|
||||
Name = Danbooru
|
||||
Domain = danbooru.donmai.us
|
||||
Token =
|
||||
SourceUrl =
|
||||
|
||||
[Source]
|
||||
Url =
|
||||
[Service]
|
||||
Name = Danbooru
|
||||
Domain = danbooru.donmai.us
|
|
@ -20,7 +20,7 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
return
|
||||
|
||||
from main import config
|
||||
response = requests.get(f"https://{config.get('General', 'Domain')}/posts/{query}.json")
|
||||
response = requests.get(f"https://{config.get('Service', 'Domain')}/posts/{query}.json")
|
||||
|
||||
if response.status_code != 200:
|
||||
await invalid_query(update, query)
|
||||
|
@ -37,8 +37,8 @@ async def answer_query(update, query, config, data):
|
|||
artists = humanize_tags_from_json(data['tag_string_artist'], "unknown artist")
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton(f"Open in {config.get('General', 'Name')}",
|
||||
url=f"https://{config.get('General', 'Domain')}/posts/{query}"),
|
||||
InlineKeyboardButton(f"Open in {config.get('Service', 'Name')}",
|
||||
url=f"https://{config.get('Service', 'Domain')}/posts/{query}"),
|
||||
InlineKeyboardButton("View original", url=data['file_url'])
|
||||
]
|
||||
]
|
||||
|
|
19
main.py
19
main.py
|
@ -17,12 +17,12 @@ logging.getLogger("httpx").setLevel(logging.WARNING)
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
config.read("config.ini")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
validate_config()
|
||||
application = Application.builder().token(os.getenv("BOT_TOKEN")).build()
|
||||
application = Application.builder().token(get_token()).build()
|
||||
|
||||
application.add_handler(CommandHandler("start", start_command))
|
||||
application.add_handler(CommandHandler("help", help_command))
|
||||
|
@ -36,13 +36,24 @@ def main() -> None:
|
|||
def validate_config():
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
response = requests.get(f"https://{config.get('General', 'Domain')}/profile.json")
|
||||
response = requests.get(f"https://{config.get('Service', 'Domain')}/profile.json")
|
||||
if response.status_code != 200:
|
||||
raise
|
||||
except:
|
||||
print('Unable validate Domain in config.ini.')
|
||||
print("Unable validate Domain in config.ini.")
|
||||
exit(1)
|
||||
|
||||
|
||||
def get_token():
|
||||
if os.getenv("BOT_TOKEN") is not None:
|
||||
return os.getenv("BOT_TOKEN")
|
||||
|
||||
if config.get('General', 'Token') != "":
|
||||
return config.get('General', 'Token')
|
||||
|
||||
print("Set BOT_TOKEN environment variable or use config.ini to set bot token.")
|
||||
exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Add table
Reference in a new issue