configurator.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import re
  7. import string
  8. import sys
  9. import typing
  10. def tty_print(text: str, tty: bool):
  11. """
  12. Print text to terminal if tty is True,
  13. otherwise removes all ANSI escape sequences
  14. """
  15. print(text if tty else re.sub(r"\033\[[0-9;]*m", "", text))
  16. def tty_input(text: str, tty: bool) -> str:
  17. """
  18. Print text to terminal if tty is True,
  19. otherwise removes all ANSI escape sequences
  20. """
  21. return input(text if tty else re.sub(r"\033\[[0-9;]*m", "", text))
  22. def api_config(tty: typing.Optional[bool] = None):
  23. """Request API config from user and set"""
  24. from . import main
  25. from ._internal import print_banner
  26. if tty is None:
  27. print("\033[0;91mThe quick brown fox jumps over the lazy dog\033[0m")
  28. tty = input("Is the text above colored? [y/N]").lower() == "y"
  29. if tty:
  30. print_banner("banner.txt")
  31. tty_print("\033[0;95mWelcome to Hikka Userbot!\033[0m", tty)
  32. tty_print("\033[0;96m1. Go to https://my.telegram.org and login\033[0m", tty)
  33. tty_print("\033[0;96m2. Click on \033[1;96mAPI development tools\033[0m", tty)
  34. tty_print(
  35. (
  36. "\033[0;96m3. Create a new application, by entering the required"
  37. " details\033[0m"
  38. ),
  39. tty,
  40. )
  41. tty_print(
  42. (
  43. "\033[0;96m4. Copy your \033[1;96mAPI ID\033[0;96m and \033[1;96mAPI"
  44. " hash\033[0m"
  45. ),
  46. tty,
  47. )
  48. while api_id := tty_input("\033[0;95mEnter API ID: \033[0m", tty):
  49. if api_id.isdigit():
  50. break
  51. tty_print("\033[0;91mInvalid ID\033[0m", tty)
  52. if not api_id:
  53. tty_print("\033[0;91mCancelled\033[0m", tty)
  54. sys.exit(0)
  55. while api_hash := tty_input("\033[0;95mEnter API hash: \033[0m", tty):
  56. if len(api_hash) == 32 and all(
  57. symbol in string.hexdigits for symbol in api_hash
  58. ):
  59. break
  60. tty_print("\033[0;91mInvalid hash\033[0m", tty)
  61. if not api_hash:
  62. tty_print("\033[0;91mCancelled\033[0m", tty)
  63. sys.exit(0)
  64. main.save_config_key("api_id", int(api_id))
  65. main.save_config_key("api_hash", api_hash)
  66. tty_print("\033[0;92mAPI config saved\033[0m", tty)