Example #1
0
def allow_connections(bot: Bot, update: Update, args: List[str]):

    chat = update.effective_chat

    if chat.type != chat.PRIVATE:
        if len(args) >= 1:
            var = args[0]
            if var == "no":
                sql.set_allow_connect_to_chat(chat.id, False)
                send_message(update.effective_message,
                             "Connection has been disabled for this chat")
            elif var == "yes":
                sql.set_allow_connect_to_chat(chat.id, True)
                send_message(update.effective_message,
                             "Connection has been enabled for this chat")
            else:
                send_message(update.effective_message,
                             "Please enter `yes` or `no`!",
                             parse_mode=ParseMode.MARKDOWN)
        else:
            get_settings = sql.allow_connect_to_chat(chat.id)
            if get_settings:
                send_message(
                    update.effective_message,
                    "Connections to this group are *Allowed* for members!",
                    parse_mode=ParseMode.MARKDOWN)
            else:
                send_message(
                    update.effective_message,
                    "Connection to this group are *Not Allowed* for members!",
                    parse_mode=ParseMode.MARKDOWN)
    else:
        send_message(update.effective_message,
                     "This command is for group only. Not in PM!")
Example #2
0
def connect_button(bot: Bot, update: Update):

    query = update.callback_query
    chat = update.effective_chat
    user = update.effective_user

    connect_match = re.match(r"connect\((.+?)\)", query.data)
    disconnect_match = query.data == "connect_disconnect"
    clear_match = query.data == "connect_clear"
    connect_close = query.data == "connect_close"

    if connect_match:
        target_chat = connect_match.group(1)
        getstatusadmin = bot.get_chat_member(target_chat, query.from_user.id)
        isadmin = getstatusadmin.status in ('administrator', 'creator')
        ismember = getstatusadmin.status in ('member')
        isallow = sql.allow_connect_to_chat(target_chat)

        if (isadmin) or (isallow and ismember) or (user.id in SUDO_USERS) or (
                user.id in DEV_USERS):
            connection_status = sql.connect(query.from_user.id, target_chat)

            if connection_status:
                conn_chat = dispatcher.bot.getChat(
                    connected(bot, update, chat, user.id, need_admin=False))
                chat_name = conn_chat.title
                query.message.edit_text(
                    "Successfully connected to *{}*. Use /connection for see current available commands."
                    .format(chat_name),
                    parse_mode=ParseMode.MARKDOWN)
                sql.add_history_conn(user.id, str(conn_chat.id), chat_name)
            else:
                query.message.edit_text("Connection failed!")
        else:
            bot.answer_callback_query(
                query.id,
                "Connection to this chat is not allowed!",
                show_alert=True)
    elif disconnect_match:
        disconnection_status = sql.disconnect(query.from_user.id)
        if disconnection_status:
            sql.disconnected_chat = query.message.edit_text(
                "Disconnected from chat!")
        else:
            bot.answer_callback_query(query.id,
                                      "You're not connected!",
                                      show_alert=True)
    elif clear_match:
        sql.clear_history_conn(query.from_user.id)
        query.message.edit_text("History connected has been cleared!")
    elif connect_close:
        query.message.edit_text("Closed.\nTo open again, type /connect")
    else:
        connect_chat(bot, update, [])
Example #3
0
def connected(bot, update, chat, user_id, need_admin=True):

    user = update.effective_user
    spam = spamfilters(update.effective_message.text,
                       update.effective_message.from_user.id,
                       update.effective_chat.id)

    if spam == True:
        return

    if chat.type == chat.PRIVATE and sql.get_connected_chat(user_id):

        conn_id = sql.get_connected_chat(user_id).chat_id
        getstatusadmin = bot.get_chat_member(
            conn_id, update.effective_message.from_user.id)
        isadmin = getstatusadmin.status in ('administrator', 'creator')
        ismember = getstatusadmin.status in ('member')
        isallow = sql.allow_connect_to_chat(conn_id)

        if (isadmin) or (isallow and ismember) or (user.id in SUDO_USERS) or (
                user.id in DEV_USERS):
            if need_admin == True:
                if getstatusadmin.status in (
                        'administrator', 'creator'
                ) or user_id in SUDO_USERS or user.id in DEV_USERS:
                    return conn_id
                else:
                    send_message(
                        update.effective_message,
                        "You must be an admin in the connected group!")
                    raise Exception("Not admin!")
            else:
                return conn_id
        else:
            send_message(
                update.effective_message,
                "The group changed the connection rights or you are no longer an admin.\nI've disconnected you."
            )
            disconnect_chat(bot, update)
            raise Exception("Not admin!")
    else:
        return False