Beispiel #1
0
def checkUser(update):
    if (not db.has_user(update.message.chat_id)):
        user = dbUser.dbUser(teleid=update.message.chat_id,
                             debugName=update.message.chat.title)
        db.store_user(user)
        return user
    else:
        return db.get_user(update.message.chat_id)
Beispiel #2
0
def adm_drop(bot, update):

    try:
        os.remove(db.dbFileName)
        db.reassign_db()
        db.store_user(dbUser.dbUser(update.message.chat_id))

        bot.send_message(chat_id=update.message.chat_id,
                         text="Database dropped",
                         reply_markup={"remove_keyboard": True})

    except Exception as ex:
        notify_admin(ex)
Beispiel #3
0
def add(bot, update):
    try:
        group = checkUser(update)
        user = update.message.from_user

        bot.send_message(chat_id=update.message.chat_id,
                         text=language.getLang(
                             group.lang)["add_enter_regexp"].format(
                                 user.first_name),
                         reply_markup={"remove_keyboard": True},
                         reply_to_message_id=update.message.message_id)

        group.userActions[user.id] = {}
        group.userActions[user.id]["i"] = WAITING_FOR_REGEX
        db.store_user(group)
        pass

    except Exception as ex:
        notify_admin(ex)
Beispiel #4
0
def disable(bot, update):
    try:
        group = checkUser(update)
        if (not group.enabled):
            bot.send_message(chat_id=update.message.chat_id,
                             text=language.getLang(
                                 group.lang)["err_bot_is_disabled"],
                             reply_markup={"remove_keyboard": True},
                             reply_to_message_id=update.message.message_id)
            return

        group.enabled = False
        db.store_user(group)

        bot.send_message(chat_id=update.message.chat_id,
                         text=language.getLang(group.lang)["bot_disabled"],
                         reply_markup={"remove_keyboard": True},
                         reply_to_message_id=update.message.message_id)
        return

    except Exception as ex:
        notify_admin(ex)
Beispiel #5
0
def remove(bot, update):

    try:
        group = checkUser(update)
        user = update.message.from_user

        if (len(group.triggers) == 0):
            bot.send_message(chat_id=update.message.chat_id,
                             text=language.getLang(
                                 group.lang)["list_is_empty"],
                             reply_markup={"remove_keyboard": True},
                             reply_to_message_id=update.message.message_id)
            return

        custom_keyboard = []
        i = 1
        for x in group.triggers:
            custom_keyboard.append([
                telegram.KeyboardButton(
                    text=u"{} - \"{}\"".format(i, x["text"]))
            ])
            i += 1

        bot.send_message(chat_id=update.message.chat_id,
                         text=language.getLang(group.lang)["remove_trigger"],
                         reply_markup=telegram.ReplyKeyboardMarkup(
                             custom_keyboard, resize_keyboard=True),
                         reply_to_message_id=update.message.message_id)

        group.userActions[user.id] = {}
        group.userActions[user.id]["i"] = WAITING_FOR_EX_TRIGGER
        db.store_user(group)
        pass

    except Exception as ex:
        notify_admin(ex)
Beispiel #6
0
def get_menu(act, user, bot):

    if (act == "btn_tomain" or act == "btn_ign" or act == "btn_adm"):

        if (act == "btn_ign"):
            user.ignoreCase = not user.ignoreCase

        if (act == "btn_adm"):
            user.adminMode = not user.adminMode

        db.store_user(user)
        return get_main_menu(user, bot)

    elif (act == "btn_lang" or act == "btn_ru" or act == "btn_en"):

        if (act == "btn_ru"):
            user.lang = "ru"
            db.store_user(user)

        if (act == "btn_en"):
            user.lang = "en"
            db.store_user(user)

        return telegram.InlineKeyboardMarkup([
            [
                telegram.InlineKeyboardButton(text=language.getLang(
                    user.lang)["menu_ru"].format(u'✅' if user.lang ==
                                                 "ru" else u' '),
                                              callback_data="btn_ru")
            ],
            #[telegram.InlineKeyboardButton(text=language.getLang(user.lang)["menu_en"]
            #    .format(u'✅' if user.lang == "en" else u' '), callback_data="btn_en")],
            [
                telegram.InlineKeyboardButton(text="Назад",
                                              callback_data="btn_tomain")
            ]
        ])

    if (act == "btn_close"):
        return {}
Beispiel #7
0
def allInputHandler(bot, update):

    try:
        group = checkUser(update)
        user = update.message.from_user
        textMessage = update.message.text

        if (str(user.id) in group.userActions):

            if (group.userActions[str(user.id)]["i"] == WAITING_FOR_REGEX):

                if (textMessage != '' and utils.isReValid(textMessage)):

                    if (textMessage in [x["text"] for x in group.triggers]):
                        bot.send_message(
                            chat_id=update.message.chat_id,
                            text=utils.escape_string(
                                language.getLang(group.lang)
                                ["err_same_regex_exists"].format(textMessage)),
                            reply_markup={"remove_keyboard": True},
                            parse_mode=telegram.ParseMode.MARKDOWN,
                            reply_to_message_id=update.message.message_id)

                        del group.userActions[str(user.id)]
                        db.store_user(group)
                        return

                    group.userActions[str(user.id)]["r"] = textMessage
                    group.userActions[str(user.id)]["i"] = WAITING_FOR_TRIGGER
                    bot.send_message(
                        chat_id=update.message.chat_id,
                        text=language.getLang(
                            group.lang)["add_enter_sendTrigger"],
                        reply_markup={"remove_keyboard": True},
                        reply_to_message_id=update.message.message_id)

                    db.store_user(group)
                    return
                else:
                    bot.send_message(
                        chat_id=update.message.chat_id,
                        text=utils.escape_string(
                            language.getLang(group.lang)
                            ["err_wrong_regex"].format(textMessage)),
                        reply_markup={"remove_keyboard": True},
                        parse_mode=telegram.ParseMode.MARKDOWN,
                        reply_to_message_id=update.message.message_id)

                    del group.userActions[str(user.id)]
                    db.store_user(group)
                    return

            elif (group.userActions[str(user.id)]["i"] == WAITING_FOR_TRIGGER):

                if (update.message.caption != None):
                    textMessage = update.message.caption

                attachment = None
                if (update.message.photo != None
                        and len(update.message.photo) != 0):
                    attachment = {
                        "file_id": update.message.photo[-1].file_id,
                        "type": "photo"
                    }
                if (update.message.sticker != None):
                    attachment = {
                        "file_id": update.message.sticker.file_id,
                        "type": "sticker"
                    }
                if (update.message.video != None):
                    attachment = {
                        "file_id": update.message.video.file_id,
                        "type": "video"
                    }
                if (update.message.voice != None):
                    attachment = {
                        "file_id": update.message.voice.file_id,
                        "type": "voice"
                    }
                if (update.message.video_note != None):
                    attachment = {
                        "file_id": update.message.video_note.file_id,
                        "type": "video_note"
                    }
                if (update.message.document != None):
                    attachment = {
                        "file_id": update.message.document.file_id,
                        "type": "document"
                    }
                if (update.message.audio != None):
                    attachment = {
                        "file_id": update.message.audio.fild_id,
                        "type": "audio"
                    }
                if (update.message.animation != None):
                    attachment = {
                        "file_id": update.message.animation.file_id,
                        "type": "animation"
                    }

                group.triggers.append({
                    "text":
                    group.userActions[str(user.id)]["r"],
                    "attachment":
                    attachment,
                    "caption":
                    textMessage
                })
                bot.send_message(chat_id=update.message.chat_id,
                                 text=utils.escape_string(
                                     language.getLang(group.lang)["add_ok"]),
                                 reply_markup={"remove_keyboard": True},
                                 parse_mode=telegram.ParseMode.MARKDOWN,
                                 reply_to_message_id=update.message.message_id)

                del group.userActions[str(user.id)]
                db.store_user(group)
                return

            elif (group.userActions[str(
                    user.id)]["i"] == WAITING_FOR_EX_TRIGGER):

                try:
                    parts = textMessage.split('-')[0]
                    index = int(parts) - 1

                    if (index < 0 or index >= len(group.triggers)):
                        bot.send_message(
                            chat_id=update.message.chat_id,
                            text=utils.escape_string(
                                language.getLang(
                                    group.lang)["err_cant_find_item"]),
                            reply_markup={"remove_keyboard": True},
                            reply_to_message_id=update.message.message_id)

                        del group.userActions[str(user.id)]
                        db.store_user(group)
                        return

                    bot.send_message(
                        chat_id=update.message.chat_id,
                        text=utils.escape_string(
                            language.getLang(group.lang)["delete_ok"]),
                        reply_markup={"remove_keyboard": True},
                        reply_to_message_id=update.message.message_id)

                    del group.triggers[index]
                    del group.userActions[str(user.id)]
                    db.store_user(group)

                except IndexError:

                    bot.send_message(
                        chat_id=update.message.chat_id,
                        text=utils.escape_string(
                            language.getLang(
                                group.lang)["err_cant_find_item"]),
                        reply_markup={"remove_keyboard": True},
                        reply_to_message_id=update.message.message_id)

                    del group.userActions[str(user.id)]
                    db.store_user(group)
                    return

                pass

        else:

            if (not group.enabled):
                return

            if (update.message.caption != None):
                textMessage = update.message.caption

            if (textMessage != None):

                for trigger in group.triggers:

                    if (group.ignoreCase):
                        textMessage = textMessage.lower()

                    regex = re.compile(trigger["text"], 0)
                    if (regex.search(textMessage)):

                        if (trigger["attachment"] != None):
                            if (trigger["attachment"]["type"] == "photo"):
                                bot.send_photo(
                                    chat_id=update.message.chat_id,
                                    photo=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id,
                                    caption=trigger["caption"])

                            elif (trigger["attachment"]["type"] == "sticker"):
                                bot.send_sticker(
                                    chat_id=update.message.chat_id,
                                    sticker=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id)

                            elif (trigger["attachment"]["type"] == "video"):
                                bot.send_video(
                                    chat_id=update.message.chat_id,
                                    video=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id,
                                    caption=trigger["caption"])

                            elif (trigger["attachment"]["type"] == "voice"):
                                bot.send_voice(
                                    chat_id=update.message.chat_id,
                                    voice=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id)

                            elif (trigger["attachment"]["type"] == "document"):
                                bot.send_document(
                                    chat_id=update.message.chat_id,
                                    document=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id,
                                    caption=trigger["caption"])

                            elif (trigger["attachment"]["type"] == "video_note"
                                  ):
                                bot.send_video_note(
                                    chat_id=update.message.chat_id,
                                    video_note=trigger["attachment"]
                                    ["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id)

                            elif (trigger["attachment"]["type"] == "audio"):
                                bot.send_audio(
                                    chat_id=update.message.chat_id,
                                    audio=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id,
                                    caption=trigger["caption"])

                            elif (trigger["attachment"]["type"] == "animation"
                                  ):
                                bot.send_animation(
                                    chat_id=update.message.chat_id,
                                    animation=trigger["attachment"]["file_id"],
                                    reply_to_message_id=update.message.
                                    message_id,
                                    caption=trigger["caption"])

                        else:
                            bot.send_message(
                                chat_id=update.message.chat_id,
                                text=trigger["caption"],
                                reply_markup={"remove_keyboard": True},
                                reply_to_message_id=update.message.message_id)

    except Exception as ex:
        notify_admin(ex)