Queer European MD passionate about IT

more_bots_together.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """Example showing how to run more bots with davtelepot.
  2. In this example, one bot will ask for updates with long polling and another
  3. one will receive them via webhook.
  4. """
  5. # Standard library modules
  6. import logging
  7. import os
  8. import sys
  9. # Third party modules
  10. try:
  11. from davtelepot.bot import Bot
  12. except ImportError:
  13. logging.error(
  14. "Please install davtelepot library.\n"
  15. "Using a python virtual environment is advised.\n\n"
  16. "```bash\n"
  17. "pip -m venv env\n"
  18. "env/bin/pip install davtelepot\n"
  19. "env/bin/python davtelepot/examples/a_simple_bot.py"
  20. "```"
  21. )
  22. sys.exit(1)
  23. # Project modules
  24. from a_simple_bot import initialize_bot
  25. # Get path of current script
  26. os.path.dirname(os.path.abspath(__file__))
  27. def _main():
  28. # Import or prompt user for bot token
  29. try:
  30. from secrets import webhook_bot_token
  31. except ImportError:
  32. webhook_bot_token = input("Enter bot token:\t\t")
  33. with open(
  34. f'{path}/secrets.py',
  35. 'a' # Append to file, create it if it does not exist
  36. ) as secrets_file:
  37. secrets_file.write(f'webhook_bot_token = "{webhook_bot_token}"\n')
  38. try:
  39. from secrets import hostname
  40. except ImportError:
  41. hostname = input("Enter host name:\t\t")
  42. with open(
  43. f'{path}/secrets.py',
  44. 'a' # Append to file, create it if it does not exist
  45. ) as secrets_file:
  46. secrets_file.write(f'hostname = "{hostname}"\n')
  47. try:
  48. from secrets import certificate
  49. except ImportError:
  50. certificate = input("Enter ssl certificate:\t\t")
  51. with open(
  52. f'{path}/secrets.py',
  53. 'a' # Append to file, create it if it does not exist
  54. ) as secrets_file:
  55. secrets_file.write(f'certificate = "{certificate}"\n')
  56. try:
  57. from secrets import local_host
  58. except ImportError:
  59. local_host = input("Enter local host:\t\t")
  60. with open(
  61. f'{path}/secrets.py',
  62. 'a' # Append to file, create it if it does not exist
  63. ) as secrets_file:
  64. secrets_file.write(f'local_host = "{local_host}"\n')
  65. try:
  66. from secrets import port
  67. except ImportError:
  68. port = input("Enter local port:\t\t")
  69. with open(
  70. f'{path}/secrets.py',
  71. 'a' # Append to file, create it if it does not exist
  72. ) as secrets_file:
  73. secrets_file.write(f'port = "{port}"\n')
  74. try:
  75. from secrets import long_polling_bot_token
  76. except ImportError:
  77. long_polling_bot_token = input("Enter bot token:\t\t")
  78. with open(
  79. f'{path}/secrets.py',
  80. 'a' # Append to file, create it if it does not exist
  81. ) as secrets_file:
  82. secrets_file.write(
  83. f'long_polling_bot_token = "{long_polling_bot_token}"\n'
  84. )
  85. # Set logging preferences
  86. log_formatter = logging.Formatter(
  87. "%(asctime)s [%(module)-15s %(levelname)-8s] %(message)s",
  88. style='%'
  89. )
  90. root_logger = logging.getLogger()
  91. root_logger.setLevel(logging.DEBUG)
  92. consoleHandler = logging.StreamHandler()
  93. consoleHandler.setFormatter(log_formatter)
  94. consoleHandler.setLevel(logging.DEBUG)
  95. root_logger.addHandler(consoleHandler)
  96. # Instantiate, initialize and make bots run.
  97. webhook_bot = Bot(
  98. token=webhook_bot_token,
  99. database_url=f"{path}/webhook_bot.db",
  100. hostname=hostname,
  101. certificate=certificate
  102. )
  103. initialize_bot(webhook_bot)
  104. long_polling_bot = Bot(
  105. token=long_polling_bot_token,
  106. database_url=f"{path}/long_polling_bot.db",
  107. )
  108. initialize_bot(long_polling_bot)
  109. logging.info("Send a KeyboardInterrupt (ctrl+C) to stop bots.")
  110. Bot.run(
  111. local_host=local_host,
  112. port=port
  113. )
  114. if __name__ == '__main__':
  115. _main()