def init(): """ Creates tables if they do not exists """ if not Chat.table_exists(): Chat.create_table() if not Metrika.table_exists(): Metrika.create_table() if not Metrika_tokens.table_exists(): Metrika_tokens.create_table() if not Users.table_exists(): Users.create_table() if not Github_repositories.table_exists(): Github_repositories.create_table() data = { 'url': TELEGRAM_CALLBACK_URL } if not CConfig.CERTIFICATE: query = 'https://api.telegram.org/bot%s/setWebhook?%s' % (CConfig.API_TOKEN, urlencode(data)) result = requests.get(query) else: query = 'https://api.telegram.org/bot%s/setWebhook?%s' % (CConfig.API_TOKEN, urlencode(data))
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 github_delete_repository(self, repository_id, chat_id): try: repository = Github_repositories.get(Github_repositories.repository_id == repository_id) repository.delete_instance() except Github_repositories.DoesNotExist: pass send_to_chat("Репозиторий отключен.", chat_id)
def github_help(self, chat_id): msg = "Модуль для работы с сервисом GitHub.\n\n- Оповещения о новых Push-событиях\n- Оповещения о создании Pull-реквестов\n- Оповещения о создании Issues" repositories = Github_repositories.select().where(Github_repositories.chat_id == chat_id) if not len(repositories): msg += "\n\nВ данный момент модуль не активирован.\n\nДля настройки модуля, используйте команду /github_start" else: msg += "\n\nПодключенные репозитории.\n\n" for repository in repositories: msg += "%s\n" % repository.repository_name msg += "\nДля отключения репозитория используйте команду /github_stop\n" \ "Подключить еще один репозиторий можно с помощью команды /github_start\n\n" \ "Меню модуля: /github_help" return msg
def github_stop(self, chat_id): repositories = Github_repositories.select().where(Github_repositories.chat_id == chat_id) if not len(repositories): send_to_chat("У вас не подключено ни одного репозитория.", chat_id) else: buttons = [ { 'text': repository.repository_name, 'callback_data': "/github_delete%s" % str(repository.repository_id) } for repository in repositories ] print(buttons) send_object_to_chat("Выберите репозиторий, который хотите отключить.\n", json.dumps({ 'inline_keyboard': [ buttons ] }), chat_id)
def get_repository(repository_id): try: repository = Github_repositories.get((Github_repositories.repository_id == repository_id)) return repository except Github_repositories.DoesNotExist: return None