def callback(chat_hash): """ GET payload from Github webhook and send it to handler :param chat_hash unique chat id """ payload = request.json try: repository_id = int(payload['repository']['id']) repository_name = payload['repository']['name'] logging.debug('Repository id: %s, chat hash: %s' % (repository_id, chat_hash)) handler = Notifications(payload) chat_id = handler.get_chat_id_by_hash(chat_hash) if request.headers['X-GitHub-Event'] == "ping": print("add repository {%s} to connected" % repository_name) if not get_repository(repository_id): gh_repository = Github_repositories.create(chat_id=chat_id, repository_id=repository_id, repository_name=repository_name) gh_repository.save() send_to_chat("Репозиторий %s был подключен" % repository_name, chat_id) gh_repository.close() else: handler.process() handler.send_to_chat(chat_hash) except Exception as e: logging.error("Github callback exception {%s}: [%s]" % (chat_hash, e)) print(bcolors.error("Github callback exception {%s}: [%s]" % (chat_hash, e))) return "{False}" return "{True}"
def load_plugins(): """ Invoked before app.run() Find modules and perform their handlers :return: """ for plugin in CConfig.MODULES: try: _temp = __import__('modules.%s.handler' % plugin, globals(), locals(), ['Handler'], 0) handler = _temp.Handler() handler.set_rules(app) handler.init_db() CConfig.PLUGINS.append(handler) print(bcolors.primary("Imported module: [%s]" % handler.name())) except Exception as e: print(bcolors.error("Error: [%s]" % e))
CConfig.PLUGINS.append(handler) print(bcolors.primary("Imported module: [%s]" % handler.name())) except Exception as e: print(bcolors.error("Error: [%s]" % e)) if __name__ == "__main__": plugins = [] import sys reload(sys) sys.setdefaultencoding('utf8') CConfig.SERVER_HOST, CConfig.SERVER_PORT, CConfig.INIT_ENABLE = check_arg(sys.argv[1:]) TELEGRAM_CALLBACK_URL = "%s%s" % (CConfig.SERVER_HOST, CConfig.TELEGRAM_CALLBACK_URI) try: if CConfig.INIT_ENABLE: init() load_plugins() app.run(host='0.0.0.0', port=CConfig.SERVER_PORT) except Exception as e: print(bcolors.error("Error: [%s]" % e))