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 append_chat(chat_id): """ Append chat unless it haven't been used :param chat_id: Telegram chat id :return: unique chat hash (as route) """ try: chat = Chat.get((Chat.chat_id == chat_id)) hash = chat.hash except Chat.DoesNotExist: hash = generate_hash() chat = Chat.create(hash=hash, chat_id=chat_id, notifications_mask=255) chat.save() return hash
def get_chat_id_by_hash(self, chat_hash): try: chat = Chat.get((Chat.hash == chat_hash)) return chat.chat_id except Chat.DoesNotExist: return None
def send_to_chat(self, chat_hash): if self.status_publish: try: chat = Chat.get((Chat.hash == chat_hash)) send_to_chat(self.get_output(), chat.chat_id) except Chat.DoesNotExist: return False return True