Public/Send-TwitterDm.ps1
function Send-TwitterDm { <# .SYNOPSIS This sends a DM to another Twitter user. NOTE: You can only send up to 250 DMs in a 24 hour period. .EXAMPLE Send-TwitterDm -Message 'hello, Adam' -Username 'adam','bill' This sends a DM with the text 'hello, Adam' to the username 'adam' and 'bill' .PARAMETER Message The text of the DM. .PARAMETER Username The username(s) you'd like to send the DM to. #> [CmdletBinding()] [OutputType('System.Management.Automation.PSCustomObject')] param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateLength(1, 140)] [string]$Message, [Parameter(Mandatory)] [string[]]$Username ) process { $HttpEndPoint = 'https://api.twitter.com/1.1/direct_messages/new.json' $AuthorizationString = Get-OAuthAuthorization -Api 'DirectMessage' -ApiParameters @{ 'screen_name' = $Username; 'text' = $Message } -HttpEndPoint $HttpEndPoint -HttpVerb 'POST' ## Convert the message to a Byte array $Message = [System.Uri]::EscapeDataString($Message) foreach ($User in $Username) { $User = [System.Uri]::EscapeDataString($User) $Body = [System.Text.Encoding]::ASCII.GetBytes("text=$Message&screen_name=$User"); Write-Verbose "Using POST body '$Body'" Invoke-RestMethod -URI $HttpEndPoint -Method Post -Body $Body -Headers @{ 'Authorization' = $AuthorizationString } -ContentType "application/x-www-form-urlencoded" } } } |