25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.6KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Giacomo Ferretti
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. import traceback
  17. from telegram import Update
  18. from telegram.ext import Updater, CommandHandler, MessageHandler, \
  19. Filters, CallbackContext, CallbackQueryHandler
  20. from commands import promo, coupon, admin
  21. from utils import config, users, logger
  22. __major__ = 0
  23. __minor__ = 3
  24. __patch__ = 0
  25. __metadata__ = ''
  26. __version__ = '{}.{}.{}{}'.format(__major__, __minor__, __patch__, __metadata__)
  27. def setup():
  28. global __logger__, __users__, __config__
  29. logger.init()
  30. __logger__ = logging.getLogger(__name__)
  31. __logger__.info('Starting mcdapi-telegram-bot ({})...'.format(__version__))
  32. __users__ = users.Users()
  33. __config__ = config.Config()
  34. __logger__.info('Done loading!')
  35. def error(update: Update, context: CallbackContext):
  36. __logger__.warning('Update "%s" caused error "%s"', update, context.error)
  37. traceback.print_exc()
  38. def main():
  39. updater = Updater(__config__.get_token(), use_context=True)
  40. # Handlers
  41. __logger_handler = logger.LoggerHandler(__config__, __users__)
  42. __admin_handler = admin.AdminManager(__config__, __users__)
  43. __promo_handler = promo.PromoCommand(__config__)
  44. __coupon_handler = coupon.CouponHandler(__config__)
  45. # Logger handler - Logs all messages and callbacks
  46. updater.dispatcher.add_handler(MessageHandler(Filters.text | Filters.command, __logger_handler.log_message), group=1)
  47. updater.dispatcher.add_handler(CallbackQueryHandler(__logger_handler.log_callback), group=1)
  48. # Main handlers - Coupons and homepage
  49. updater.dispatcher.add_handler(CommandHandler('start', __coupon_handler.home))
  50. updater.dispatcher.add_handler(CallbackQueryHandler(__coupon_handler.callback, pattern='{}.*'
  51. .format(__coupon_handler.name)))
  52. # Promo handler - Generates promocodes
  53. updater.dispatcher.add_handler(CommandHandler('promo', __promo_handler.handler))
  54. updater.dispatcher.add_handler(CallbackQueryHandler(__promo_handler.callback_handler, pattern='{}.*'
  55. .format(__promo_handler.name)))
  56. # Admin handler - Handles all the admin commands
  57. updater.dispatcher.add_handler(CommandHandler('broadcast', __admin_handler.broadcast))
  58. updater.dispatcher.add_handler(CommandHandler('send', __admin_handler.send_message))
  59. updater.dispatcher.add_handler(CommandHandler('uptime', __admin_handler.uptime))
  60. updater.dispatcher.add_handler(CommandHandler('users', __admin_handler.users_info))
  61. updater.dispatcher.add_handler(CommandHandler('users_list', __admin_handler.users_list))
  62. updater.dispatcher.add_handler(CommandHandler('check', __admin_handler.check_users))
  63. updater.dispatcher.add_handler(MessageHandler(Filters.reply, __admin_handler.send_broadcast))
  64. # Error handler
  65. updater.dispatcher.add_error_handler(error)
  66. # Start the Bot
  67. updater.start_polling()
  68. # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
  69. # SIGTERM or SIGABRT
  70. updater.idle()
  71. if __name__ == '__main__':
  72. setup()
  73. main()