public/Send-TwitterDirectMessages_EventsNew.ps1

function Send-TwitterDirectMessages_EventsNew {
<#
.SYNOPSIS
    Sending and receiving events
 
.DESCRIPTION
    POST direct_messages/events/new (message_create)
     
    Publishes a new message_create event resulting in a Direct Message sent to a specified user from the authenticating user. Returns an event if successful. Supports publishing Direct Messages with optional Quick Reply and media attachment. Replaces behavior currently provided by POST direct_messages/new.
     
    Requires a JSON POST body and Content-Type header to be set to application/json. Setting Content-Length may also be required if it is not automatically.
 
 
.NOTES
    This helper function was generated by the information provided here:
    https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
 
#>

    [CmdletBinding()]
    Param(
        [string]$type = 'message_create',
        [string]$recipient_id,
        [string]$text
    )
    Begin {

        [hashtable]$Parameters = $PSBoundParameters
                   $CmdletBindingParameters | ForEach-Object { $Parameters.Remove($_) }

        [string]$Method      = 'POST'
        [string]$Resource    = '/direct_messages/events/new'
        [string]$ResourceUrl = 'https://api.twitter.com/1.1/direct_messages/events/new.json'

    }
    Process {

        $Body = @{ 
            event = @{
                type = $type
                message_create = @{
                    target = @{ recipient_id = $Parameters['recipient_id'] }
                    message_data = @{ text = $Parameters['text'] }
                }
            }
        }

        # Find & Replace any ResourceUrl parameters.
        $UrlParameters = [regex]::Matches($ResourceUrl, '(?<!\w):\w+')
        ForEach ($UrlParameter in $UrlParameters) {
            $UrlParameterValue = $Parameters["$($UrlParameter.Value.TrimStart(":"))"]
            $ResourceUrl = $ResourceUrl -Replace $UrlParameter.Value, $UrlParameterValue
        }

        If (-Not $OAuthSettings) { $OAuthSettings = Get-TwitterOAuthSettings -Resource $Resource }
        Invoke-TwitterAPI -Method $Method -ResourceUrl $ResourceUrl -Body $Body -OAuthSettings $OAuthSettings

    }
    End {

    }
}