def write_telegram(self, match: Match, offset: int):
        telegram = json.loads(
            TinderDb.get_match_tgs(match_id=match.match_id))[offset]
        asyncio.set_event_loop(self.loop)

        messages = MessageProvider.messages_for_telegram()

        if TextAnalyzer.try_parse_login(strings=[telegram]):
            receiver = f"@{telegram}"

            for message in messages:
                try:
                    self.client.send_message(receiver, message)
                    time.sleep(2)
                except ValueError as e:
                    print(e.args)

        elif TextAnalyzer.try_parse_phone(telegram):
            contact = InputPhoneContact(
                client_id=0,  # For new contacts use client_id = 0
                phone=f"+7{telegram}",
                first_name=f"{match.name} {match.age}",
                last_name='Tinder')

            c = self.client(ImportContactsRequest([contact]))
            if c.users:
                for message in messages:
                    self.client.send_message(c.users[0], message)
Exemplo n.º 2
0
    def switch_telegram(self, update: Update, context: CallbackContext):
        callback_data = update.callback_query.data
        direction, _, tinder_match_id = callback_data.split('/')

        tg_write_button_row_index = -1
        tg_switch_buttons_row_index = -1

        for i, row_of_buttons in enumerate(
                update.callback_query.message.reply_markup.inline_keyboard):
            if 'telegram' in row_of_buttons[0].callback_data:
                tg_write_button_row_index = i
                tg_switch_buttons_row_index = i + 1
                position = int(update.callback_query.message.reply_markup.inline_keyboard[i] \
                    [0].callback_data.split('/')[2])
                break

        telegrams = json.loads(
            TinderDb.get_match_tgs(match_id=tinder_match_id))

        if direction == 'next':
            position += 1

            if position > len(telegrams) - 1:
                return

        elif direction == 'prev':
            position -= 1

            if position < 0:
                return

        new_telegram_login = telegrams[position]

        new_reply_markup = update.callback_query.message.reply_markup
        new_reply_markup.inline_keyboard[tg_write_button_row_index][
            0].text = f'Tg: {new_telegram_login}'
        new_reply_markup.inline_keyboard[tg_write_button_row_index][
            0].callback_data = f'telegram/{tinder_match_id}/{position}'

        if position == 0:

            new_reply_markup.inline_keyboard[tg_switch_buttons_row_index] = [
                InlineKeyboardButton(
                    f'<<', callback_data=f'prev/tg/{tinder_match_id}'),
                InlineKeyboardButton(
                    f'>> {telegrams[position + 1]}',
                    callback_data=f'next/tg/{tinder_match_id}')
            ]

        elif position == len(telegrams) - 1:

            new_reply_markup.inline_keyboard[tg_switch_buttons_row_index] = [
                InlineKeyboardButton(
                    f'<< {telegrams[position - 1]}',
                    callback_data=f'prev/tg/{tinder_match_id}'),
                InlineKeyboardButton(
                    f'>>', callback_data=f'next/tg/{tinder_match_id}')
            ]

        else:

            new_reply_markup.inline_keyboard[tg_switch_buttons_row_index] = [
                InlineKeyboardButton(
                    f'<< {telegrams[position - 1]}',
                    callback_data=f'prev/tg/{tinder_match_id}'),
                InlineKeyboardButton(
                    f'>> {telegrams[position + 1]}',
                    callback_data=f'next/tg/{tinder_match_id}')
            ]

        self.telegram_bot_instance.bot.edit_message_reply_markup(
            chat_id=update.callback_query.message.chat_id,
            message_id=update.callback_query.message.message_id,
            reply_markup=new_reply_markup)