Ejemplo n.º 1
0
def additional_start_btn():
    try:
        active_buttons = EditionButtons.objects.get(btn_active=True, btn_position_start=True)
        return active_buttons.btn_name
    except Exception as ex:
        logger_telegram().exception("Exception in additional_start_btn()\n%s" % ex)
        return None
Ejemplo n.º 2
0
def btn_and_text(child, us_pos: int) -> (list, str):
    """ Avery Tree Field in the Admin menu has 'User input' option.
    'User input' = text buttons, that must be send to the chat. """
    btn_text_list = []

    normal_chat_buttons = [i.user_input for i in child]

    start_btn = additional_start_btn()
    if start_btn and start_btn not in normal_chat_buttons:
        btn_text_list.extend([start_btn])

    btn_text_list.extend(normal_chat_buttons)

    buttons_out = [[KeyboardButton(text=i)] for i in btn_text_list]

    text_out = ''
    for t in HelpText.objects.filter(relation_to=us_pos):
        if t:
            try:
                text_out += t.text
                if t.telegram_geo_url and t.address:
                    text_out += """\n<a href="{}">{}</a>\n\n""".format(t.telegram_geo_url, t.address)
            except Exception as ex:
                logger_telegram().error("Exception in btn_and_text():\n%s" % ex)
                continue
        else:
            continue

    return buttons_out, text_out
Ejemplo n.º 3
0
def known_user(_chat_id: int, _user_position: int,
               _massage: str) -> (list, str):
    """ user used HelpBot and have last saved position """
    root = NeedHelp.objects.get(id=_user_position)
    child = root.get_children()
    if child:
        for c in child:
            if _massage == c.user_input:
                if c.link_to:
                    """ If this button has a link to the other help option. Select list in Admin. """
                    user_position = c.link_to.id
                    new_child = NeedHelp.objects.get(
                        id=user_position).get_children()
                    if not new_child:
                        new_child = NeedHelp.objects.get(
                            is_default=True).get_children()
                elif c.go_back:
                    """ Back to the main questions. Check_box in Admin. """
                    save_telegram_user(_chat_id, 0, True)
                    return default_output(help_type=True)
                elif c.go_default:
                    """ if user clicked last element of the Tree - go to default branch. """
                    user_position = c.id
                    try:
                        new_child = NeedHelp.objects.get(
                            is_default=True).get_children()
                    except Exception as ex:
                        logger_telegram().exception(
                            "Chat Tree DO NOT have element with is_default=True!\n%s"
                            % ex)
                        return random_input(_chat_id, True, sorry=True)
                else:
                    """ Normal buttons in the chat. Go deeper. """
                    user_position = c.id
                    new_child = c.get_children()

                save_telegram_user(_chat_id, user_position, True)
                return btn_and_text(new_child, user_position)
        """ button text not in the list. """
        return random_input(_chat_id, True, sorry=True)

    elif root.go_default:
        """ if user at the last element of the Tree - go to default branch.
        is_default=True - hidden root node for a default output that repeats at last tree element. """
        try:
            new_root = NeedHelp.objects.get(is_default=True)
        except Exception as ex:
            logger_telegram().exception(
                "Chat Tree DO NOT have element with is_default=True!\n%s" % ex)
            return random_input(_chat_id, True, sorry=True)
        new_child = new_root.get_children()
        for c in new_child:
            if _massage == c.user_input:
                if c.go_back:
                    save_telegram_user(_chat_id, 0, True)
                    return default_output(help_type=True)
                else:
                    return known_user(_chat_id, new_root.id, _massage)

    return random_input(_chat_id, True, sorry=True)
Ejemplo n.º 4
0
def start_msg_text() -> str:
    try:
        text_h = StartMessage.objects.filter(hello_text=True)
        if text_h:
            return text_h.first().text
        logger_telegram().error("Стартовое сообщение приветствия отсутствует!")
        return ''
    except Exception as ex:
        logger_telegram().exception("Exception in start_msg_text()\n%s" % ex)
        return ''
Ejemplo n.º 5
0
def sorry_text_msg() -> str:
    """ Sorry text if wrong input. Adding to the TOP of the "Hello" text. """
    try:
        text_obj = StartMessage.objects.filter(sorry_text=True)
        if any(text_obj):
            return text_obj.first().text
        logger_telegram().error("Сообщение об ошибке ввода отсутствует!")
        return ''
    except Exception as ex:
        logger_telegram().exception("Exception in sorry_text_msg()\n%s" % ex)
        return ''
Ejemplo n.º 6
0
def find_telegram_user(_chat_id: int) -> (bool, int):
    """ If user has ever used HelpBot -> find chat_id id DB and get user last position,
    else set user_position = 0. """
    try:
        users_all = ChatPositionTelegram.objects.all().values()
        for u in users_all:
            if u['chat_id'] == _chat_id:
                return True, u['position']
        save_telegram_user(_chat_id, 0, False)
        return True, 0
    except Exception as ex:
        logger_telegram().error("Exception in find_telegram_user()\n%s" % ex)
        return False, 0
Ejemplo n.º 7
0
def help_type_text_msg() -> str:
    """ костыль, чтобы сообщение приветствия не повторялось при возврате к стартовым вопросам.
        "help_type" - название специального сообщения в разделе "Стартовое сообщение бота".
    """
    try:
        text_obj = StartMessage.objects.filter(name='help_type')
        if text_obj:
            return text_obj.first().text
        logger_telegram().error("Альтернативное сообщение при возврате к стартовым вопросам отсутствует!")
        return ''
    except Exception as ex:
        logger_telegram().exception("Exception in help_type_text_msg()\n%s" % ex)
        return ''
Ejemplo n.º 8
0
def keyboard_button(_massage: str, _chat_id: int) -> (list, str):
    """ Telegram chat bot main logic. """
    if len(_massage) > 1:
        # print("_massage: %s" % _massage)
        user, user_position = find_telegram_user(_chat_id)

        if user and not user_position:
            """ user came from a start questions + /start """
            return user_from_start(_chat_id, _massage)

        elif user_position:
            """ user used HelpBot and have last saved position """
            return known_user(_chat_id, user_position, _massage)

        else:
            """ random input """
            return random_input(_chat_id, True, sorry=True)
    else:
        logger_telegram().warning("Error in keyboard_button(), len(_massage) < 1")
        return default_output(sorry=True)
Ejemplo n.º 9
0
def start(update, context):
    _chat_id = update.message.chat_id

    try:
        key_bord_btn, help_text = keyboard_button(update.message.text,
                                                  _chat_id)
    except Exception as ex:
        logger_telegram().exception("Exception TelegramBot.start().\n%s" % ex)
    else:
        try:
            context.bot.send_message(
                chat_id=_chat_id,
                text=help_text,
                parse_mode=ParseMode.HTML,
                disable_web_page_preview=True,
                reply_markup=ReplyKeyboardMarkup(key_bord_btn,
                                                 resize_keyboard=True),
            )
        except Exception as ex:
            logger_telegram().exception("Exception TelegramBot.start().\n%s" %
                                        ex)
Ejemplo n.º 10
0
def lets_start_a_telegram_bot():
    __my_logger = logger_telegram()
    __restart_bot_in_sec = 10

    print("Let's try to start telegram bot...")
    is_telegram_bot_running = False

    while not is_telegram_bot_running:
        is_telegram_bot_running = go_go_bot(__my_logger)
        if not is_telegram_bot_running:
            print('\tTelegramBot will restart in %s sec.' %
                  __restart_bot_in_sec)
            sleep(__restart_bot_in_sec)

    print("! All OK, Telegram bot STARTed !\n")