Queer European MD passionate about IT

webhook_powered_bot.py 3.1 KB

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