public/Invoke-AutomatedSocialMediaPost.ps1

function Invoke-AutomatedSocialMediaPost {

    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string]$BucketName,

        [Parameter(Mandatory=$true)]
        [ValidateSet('corners','goals','match','outcomes')]
        [string]$Content,

        [Parameter(Mandatory=$true)]
        [ValidateSet('australia','europe','europe-uk','europe-uk-cup','europe-uk-fl','europe-uk-wsl','south-america','north-america','asia')]
        [string]$Continent,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Date,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$PageId,

        [Parameter(Mandatory=$false)]
        [switch]$Post,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [ValidateSet('facebook','instagram')]
        [string]$Platform,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Token

    )
    process{

        $ErrorActionPreference = 'Stop'


        try {

            $Competions = Select-FootballCompetition -Continent $Continent
            $Competions | Format-Table
            $Competions.GetEnumerator() | ForEach-Object -Process {

                Write-Warning -Message "$($MyInvocation.MyCommand.Name): Processing $Platform $Content $($_.Key) $Date."
                $Prompt1 = Read-UserInput -Title $($MyInvocation.MyCommand.Name) -Message "Post $Content content $($_.Key) $Date."

                if ($Prompt1 -eq 1) {

                    Write-Warning -Message "$($MyInvocation.MyCommand.Name): Skipping post $Content content $($_.Key) $Date."

                }
                else {

                    # Convert png to jpg
                    Convert-FootballFixtureMediaFile -Competition ($_.Key) -Date $Date -Content $Content -Path $Path

                    # Copy to central location
                    Copy-FootballFixtureConvertedMedia -SourcePath "$Path\fixture-artifact\working-set\$($_.Key)\$Date" -TargetPath $Path

                    if ($Platform -eq 'facebook') { 

                        $FBParams =@{
                            PageId = $PageId
                            Token  = $Token 
                            Competition = $($_.Key)
                            Date = $Date
                            Content = $Content
                            Path = $Path
                        }
                        
                        if ($Post) {

                            $FBParams.Add('post',$true)

                        } # if

                        # Facebook post
                        Invoke-AutomatedFacebookPostWithPhoto @FBParams

                        # Facebook story
                        Invoke-AutomatedFacebookStoryWithPhoto @FBParams

                    } # if

                    if ($Platform -eq 'instagram' -and $($PSBoundParameters.ContainsKey('BucketName'))) {

                        $InstaParams =@{
                            PageId = $PageId
                            Token  = $Token 
                            Competition = $($_.Key)
                            Date = $Date
                            Content = $Content
                            BucketName = $BucketName
                            Path = $Path
                        }

                        if ($Post) {

                            $InstaParams.Add('post',$true)

                        } # if

                        # Sync to S3 bucket
                        aws s3 sync "$Path\fixture-artifact\instagram" "s3://$BucketName/instagram" --acl public-read

                        # Instagram post
                        Invoke-AutomatedInstagramSingleMediaPost @InstaParams

                        # Instagram story
                        Invoke-AutomatedInstagramSingleMediaStoryPost @InstaParams

                        # Remove files
                        Remove-Item -Path "$Path\fixture-artifact\instagram\*" -Force -Recurse -Confirm:$false -ErrorAction Stop

                    } # if

                } # if

            } # foreach-object

            # Prompt to post youtube advert
            $Prompt1 = Read-UserInput -Title $($MyInvocation.MyCommand.Name) -Message "Post youtube advert."

            if ($Prompt1 -eq 1) {

                Write-Warning -Message "$($MyInvocation.MyCommand.Name): Skipping posting youtube advert."

            }
            else {

                # Set template to use
                $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\youtube_post.txt"

                # Test if it exists
                $TemplatePathExists = Test-Path -Path $TemplatePath

                if ($TemplatePathExists) {

                    # Get content of template to post
                    $Message = Get-Content -Path $TemplatePath -Raw -ErrorAction Stop

                    # Link to include in the feed
                    $Link = 'https://www.youtube.com/@AISportPredicts'

                    # Post the temple to the feed
                    Invoke-FacebookPost -Token $Token -PageId $PageId -Message $Message -Link $Link

                } # if

            } # if

        }
        catch {

             throw "$($MyInvocation.MyCommand.Name): $_.Exception.Message"

        } # try catch

    } # process

} # function