Beispiel #1
0
def forward_to_support(message):
    """ Forward all clients messages to support group if dialogue is open
        Also send to support info about client's tariff """

    time.sleep(1)
    user_info = db_find_value("tg_id", message.chat.id)
    user_state = user_info['state']

    # transfer client's message to support group
    tg_to_tg(config.group_id, message)

    # Client sent a message after being reminded about the payment, open dialogue
    if user_state == "REMINDED":
        open_dialogue("tg_id", message.chat.id, state="PAY")
        if message.photo:
            filename = save_file(message,
                                 folder=config.pay_imgs_path,
                                 check=True)
            autopay(message.chat.id, 'tg_id', filename)

    # Client sent a photo after he has chosen the payment option. Consider this photo as payment screenshot
    elif user_state == "PAY":
        if message.photo:
            filename = save_file(message,
                                 folder=config.pay_imgs_path,
                                 check=True)
            autopay(message.chat.id, 'tg_id', filename)

    # Notify client that his message was received (once per dialogue)
    if user_info['received'] == "NO":
        bot.send_message(
            message.chat.id, "Ваше сообщение передано в поддержку. "
            "Мы постараемся ответить как можно быстрее!")
        update_clients(["tg_id", message.chat.id], ["received", "YES"])
Beispiel #2
0
def unknown_user(message):
    """ Handle all messages if user is not identified """

    time.sleep(1)
    # Check if message text has '@' between some non-space symbols
    if not message.text or not re.findall(r"\S+@\S+", message.text):
        bot.send_message(message.chat.id, messages.send_email)
        return

    email = re.findall(r"\S+@\S+", message.text)[0].lower()
    # Suppose user entered email, look for it in database
    email_info = db_find_value("email", email)

    # Email not found, insert new row in DB with that email and user ID
    if not email_info:
        new_client(email, "tg_id", message.chat.id)
        initial_buttons(message)

    # Email is already used by user with other ID, ask to immediately contact us
    elif email_info['tg_id'] != "0":
        new_client("-", "tg_id", message.chat.id)
        open_dialogue("tg_id", message.chat.id)
        initial_buttons(message, messages.email_already_used)

    # Email found in DB and not used by other ID, update DB
    else:
        update_clients(["email", email], ["tg_id", message.chat.id])
        initial_buttons(message)
Beispiel #3
0
def support(message, urgent=False):
    """ Handles every attempt to open support dialogue. Does not open if not urgent and not in working time """

    time.sleep(1)
    buttons = types.InlineKeyboardMarkup()

    # User trying to contact support in non working time
    if not urgent:
        if not 17 <= datetime.datetime.today(
        ).hour < 22 or datetime.datetime.today().isoweekday() in [6, 7]:
            buttons.add(
                types.InlineKeyboardButton(text="Срочная связь",
                                           callback_data="urgent"))
            bot.send_message(message.chat.id,
                             messages.non_working,
                             reply_markup=buttons)
            return

    open_dialogue("tg_id", message.chat.id)
    buttons = types.InlineKeyboardMarkup()
    buttons.add(
        types.InlineKeyboardButton(text="Первичная настройка",
                                   callback_data="install"))
    buttons.add(
        types.InlineKeyboardButton(text="Другое", callback_data="other"))
    buttons.add(
        types.InlineKeyboardButton(text="ZGC SHOP", callback_data="market"))

    # Ask user to choose problem type
    msg = messages.type_support
    user_info = db_find_value("tg_id", message.chat.id)
    sub = user_info['sub']
    if sub != '-' and int(user_info['verified']):
        msg += f"\U000026A1 Ваша подписка: {sub}"
    bot.send_message(message.chat.id, msg, reply_markup=buttons)
Beispiel #4
0
def pay(message):
    """ Handles payment button """

    time.sleep(1)
    open_dialogue("tg_id", message.chat.id, state="PAY")

    buttons = types.InlineKeyboardMarkup()
    buttons.add(
        types.InlineKeyboardButton(text="В рублях ₽ или в гривнах ₴",
                                   callback_data="rub"))
    buttons.add(
        types.InlineKeyboardButton(text="В юанях ¥", callback_data="yuan"))
    buttons.add(
        types.InlineKeyboardButton(text="\U00002753 Связаться с поддержкой",
                                   callback_data="sup"))
    bot.send_message(message.chat.id, messages.pay_type, reply_markup=buttons)
Beispiel #5
0
def support_group(message):
    """ Handle all messages in support group """

    time.sleep(1)

    # Bot info message
    if message.text and message.text.lower() == "/info":
        bot.send_message(config.group_id, messages.info)

    # Message is reply to some message
    if message.reply_to_message:
        client_text = message.reply_to_message.text or message.reply_to_message.caption

        if not client_text:
            pass

        # Reply object message was forwarded from tg
        elif client_text and client_text.endswith(
                "Telegram") or client_text.endswith("Telegram\U00002705"):

            tg_id = client_text.split()[-2]

            # User id does not fit 'one or more numeral' regexp
            if not re.fullmatch(r"[0-9]+", str(tg_id)):
                bot.send_message(
                    config.group_id, "Не удалось отправить сообщение. "
                    "Скорее всего это закрытый аккаунт без айди в подписи")
                return

            # Close dialogue
            if message.text and message.text.lower() in [
                    "пока", "/пока", "off", "конец", "/q"
            ]:
                close_dialogue("tg_id", tg_id)
            # Close payment dialogue
            elif message.text and message.text.lower() in ["/оплата", "оп"]:
                close_dialogue("tg_id", tg_id, pay=True)
            # Close dialogue silently
            elif message.text and message.text.lower() == "/закрыть":
                close_dialogue("tg_id", tg_id, silent=True)
            # Check if message was forwarded by bot, not by other user
            elif message.reply_to_message.from_user.id == config.bot_id:
                copy_send_message(
                    tg_id, message.chat.id,
                    message.message_id)  # Finally, send answer to client :)
                open_dialogue("tg_id", tg_id)

        # Reply object message was forwarded from VK
        elif client_text and client_text.endswith(
                "Vkontakte") or client_text.endswith("Vkontakte\U00002705"):
            vk_id = client_text.split()[-2]

            if message.text and message.text.lower() in [
                    "пока", "off", "конец", "/q"
            ]:
                close_dialogue("vk_id", vk_id)
            elif message.text and message.text.lower() in ["/оплата", "оп"]:
                close_dialogue("vk_id", vk_id, pay=True)
            elif message.text and message.text.lower() == "/закрыть":
                close_dialogue("vk_id", vk_id, silent=True)
            else:
                tg_to_vk(message, vk_id)
                open_dialogue("vk_id", vk_id)

        # Reply object message was forwarded from FB
        elif client_text and client_text.endswith("Facebook"):
            fb_id = client_text.split()[-2]

            if message.text and message.text.lower() in [
                    "пока", "/пока", "off", "конец", "/q"
            ]:
                close_dialogue("fb_id", fb_id)
            elif message.text and message.text.lower() in ["/оплата", "оп"]:
                close_dialogue("fb_id", fb_id, pay=True)
            elif message.text and message.text.lower() == "/закрыть":
                close_dialogue("fb_id", fb_id, silent=True)
            else:
                tg_to_fb(message, fb_id)

        # my code
        elif client_text and client_text.endswith("Web_client"):
            import data_structs as ds
            import helpers

            replied_message = message.reply_to_message.text

            reply_email = helpers.get_email_from_message(replied_message)

            ws_message = json.dumps({'from': 'bot', 'message': message.text})

            import asyncio

            asyncio.run(ds.send_ws_msg(reply_email, ws_message))

    # Message is not reply to some message
    else:

        # Message text is dialogue closing command
        if message.text and message.text.lower().split()[0] in [
                "пока", "/пока", "off", "конец", "/q"
        ]:

            # If not replying to some message, closing message must be exactly 2 words
            # and contain ID of user whose dialogue we want to close
            if len(message.text.lower().split()) != 2:
                bot.send_message(
                    message.chat.id,
                    "Отправьте команду и айди через пробел, например:\n"
                    "Пока 1234567")
                return

            # We don't know which messenger this id belongs to, so just try to close this ID for every type
            for id_type in ["tg_id", "vk_id", "fb_id"]:
                close_dialogue(id_type, message.text.lower().split()[1])

        elif message.text and message.text.lower().split()[0] in [
                "/оплата", "оп"
        ]:
            if len(message.text.lower().split()) != 2:
                bot.send_message(
                    message.chat.id,
                    "Отправьте команду и айди через пробел, например:\n"
                    "/оплата 1234567")
                return

            for id_type in ["tg_id", "vk_id", "fb_id"]:
                close_dialogue(id_type,
                               message.text.lower().split()[1],
                               pay=True)

        elif message.text and message.text.lower().split()[0] == "/закрыть":
            if len(message.text.lower().split()) != 2:
                bot.send_message(
                    message.chat.id,
                    "Отправьте команду и айди через пробел, например:\n"
                    "/закрыть 1234567")
                return

            for id_type in ["tg_id", "vk_id", "fb_id"]:
                close_dialogue(id_type,
                               message.text.lower().split()[1],
                               silent=True)
Beispiel #6
0
def react(call):
    """ Handles all callback buttons """

    time.sleep(1)
    buttons = types.InlineKeyboardMarkup()
    user_info = db_find_value('tg_id', call.message.chat.id)

    if call.data == "rub":
        tariff_id = user_info['tariff']
        msg = messages.rub_text

        # If user has active tariff, send button allowing to extend his current tariff
        # Temporarily off
        if False and tariff_id in ['1', '2', '3', '22', '23']:
            buttons.add(
                types.InlineKeyboardButton(text="Продлить текущий тариф",
                                           callback_data=f"Р-{tariff_id}"))
            msg += f"\n\nВаш текущий тариф: {tariff_id}"
            bot.send_message(call.message.chat.id,
                             msg,
                             parse_mode='Markdown',
                             reply_markup=buttons)
        else:
            bot.send_message(call.message.chat.id, msg, parse_mode='Markdown')

    elif call.data == "yuan":
        bot.send_message(call.message.chat.id,
                         messages.yuan_text,
                         parse_mode='Markdown')
    elif call.data == "install":
        bot.send_message(call.message.chat.id, messages.first_install)
    elif call.data == "other":
        bot.send_message(call.message.chat.id,
                         messages.support,
                         parse_mode='Markdown')
    elif call.data == "market":
        open_dialogue("tg_id", call.message.chat.id)
        bot.send_message(
            call.message.chat.id,
            'Здравствуйте! Укажите, пожалуйста, продукт и вопросы по нему')
    elif call.data == "urgent":
        support(call.message, urgent=True)
    elif call.data == "sup":
        support(call.message)

    # If user rated quality less than 5 and pushed feedback button, open dialogue for one message only
    elif call.data == "get_better":
        update_clients(["tg_id", call.message.chat.id],
                       ["state", "ONE MESSAGE"],
                       ["review_time", f"{int(time.time())}"])
        bot.send_message(call.message.chat.id, messages.get_better)

    elif call.data == "pay":
        buttons.add(
            types.InlineKeyboardButton(text="В рублях или в гривнах",
                                       callback_data="rub"))
        buttons.add(
            types.InlineKeyboardButton(text="В юанях", callback_data="yuan"))
        buttons.add(
            types.InlineKeyboardButton(
                text="\U00002753 Связаться с поддержкой", callback_data="sup"))
        bot.send_message(call.message.chat.id,
                         messages.pay_type,
                         reply_markup=buttons)

    elif call.data in ["1", "2", "3", "4", "5"]:

        # User has already rated
        if get_info("rate", "tg_id", call.message.chat.id) != "0":
            bot.send_message(call.message.chat.id,
                             "Вы уже поставили оценку, спасибо!")
            return

        rating = call.data

        # Ask user to make review if he gave the highest rate
        if rating == "5":
            buttons.add(
                types.InlineKeyboardButton(text="\U0001F49B Оставить отзыв",
                                           url=config.review_link))
            bot.send_message(
                call.message.chat.id,
                "Если вам понравился наш сервис - оставьте отзыв, "
                "и мы предоставим вам 10 дней бесплатного VPN!\n\n"
                "_Когда оставите отзыв свяжитесь с нами для получения бонуса_",
                reply_markup=buttons,
                parse_mode='Markdown')
        # Ask user to write feedback
        else:
            buttons.add(
                types.InlineKeyboardButton(
                    text="\U0001F4A1 Оставить пожелание",
                    callback_data="get_better"))
            bot.send_message(call.message.chat.id,
                             "Мы можем что-то улучшить в обслуживании?",
                             reply_markup=buttons)

        bot.send_message(
            config.group_id,
            f"Клиент `{call.message.chat.id}` поставил вам {rating}",
            parse_mode='Markdown')
        update_clients(["tg_id", call.message.chat.id], ["rate", rating])

    # User chose tariff he wants to buy, send him buttons to choose tariff duration in days (ascending order)
    # Temporarily off
    elif False and re.fullmatch(r'[РЮ]-[123]+', call.data):
        currency, tariff_id = call.data.split('-')
        tariffs_list = get_tariffs(['tariff', tariff_id],
                                   ['currency', currency])
        for price, tariff_id, days, currency, desc in sorted(
                tariffs_list, key=lambda x: int(x[2])):
            # Telegram does not support prices lower than 60 rub.
            if int(price) < 60:
                continue
            # Choose correct form of 'день' word
            if days[-1] == '1':
                day_word = 'день'
            elif days[-1] in '234':
                day_word = 'дня'
            else:
                day_word = 'дней'
            buttons.add(
                types.InlineKeyboardButton(
                    text=f"{days} {day_word}",
                    callback_data=f"{currency}-{tariff_id}-{days}"))
        bot.send_message(
            call.message.chat.id,
            f"Выберите, на сколько дней вы хотите продлить тариф {tariff_id}",
            reply_markup=buttons)

    # User chose tariff duration in days, send him payment button
    # Temporarily off
    elif False and re.fullmatch(r'[РЮ]-[123]+-[0-9]+', call.data):
        currency, tariff_id, days = call.data.split('-')
        specific_tariff = get_tariffs(['tariff', tariff_id],
                                      ['currency', currency],
                                      ['days', days])[0]
        price = int(specific_tariff[0])
        prices = [
            types.LabeledPrice(label=f'Тариф {tariff_id} на {days} дней',
                               amount=price * 100)
        ]
        bot.send_invoice(call.message.chat.id,
                         title=f"Тариф {tariff_id}",
                         description='Plati',
                         provider_token=config.pay_token,
                         currency='rub',
                         prices=prices,
                         start_parameter='payment',
                         invoice_payload='HAPPY FRIDAYS COUPON')