We are looking at options to mimic WhatsApp’s broadcast-list feature and came up with the below improved (from reverse engineering) version of the script to send a text message to a WhatsApp number and it does the job:
$LogDir = "$PSScriptRootlogs"
$LogDate = (Get-Date).tostring("yyyyMMdd")
$Entity = "WA-POST"
$LogFile = $LogDir+ "" + $Entity + "_log_" + $LogDate + ".txt"
$AuditFile = $LogDir+ "" + $Entity + "_Audit_" + $LogDate + ".txt"
$SessionLogFile = $LogDir+ "" + $Entity + "_sessionLog_" + $LogDate + ".txt"
Start-Transcript -Path $SessionLogFile -Verbose
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$wshell = New-Object -ComObject wscript.shell
$Image = [System.Drawing.Image]::FromFile("$PSScriptRootmark1.jpg")
$numbersToPost = "1234567890#message1" -split ","
Function doWAPOST ([string] $textForTreatment )
{
$phNum = $textForTreatment.split('#')[0];
$preFilledMessgae = $textForTreatment.split('#')[1];
$encodedPreFilledMessage = [uri]::EscapeDataString($preFilledMessgae);
$url = "https://api.whatsapp.com/send/?phone=" + $phNum + "&text=" + $encodedPreFilledMessage;
Log-Message ("Number to Post: " + $phNum);
Log-Message ("Raw Message to Post: " + $preFilledMessgae);
Log-Message ("Encoded Message to Post: " + $encodedPreFilledMessage);
Log-Message ("Constructed Post URL: " + $url);
Try {
Start-Process "chrome.exe" $url
sleep -m 600 ; $wshell.AppActivate('Share on WhatsApp') ; Start-Sleep -m 200
1..8 | % {; sleep -m 300 ; [System.Windows.Forms.SendKeys]::SendWait("{TAB}")}
1..10 | % {; sleep -m 300 ; [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")}
Audit-Message ("Success for " + $phNum);
} Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
}
Function Log-Message()
{
param
(
[Parameter(Mandatory=$true)] [string] $Message
)
Try {
$TimeStamp = (Get-Date).toString("dd/MM/yyyy HH:mm:ss:fff tt")
$Line = "$TimeStamp - $Message"
Add-content -Path $Logfile -Value $Line
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
}
Function Audit-Message()
{
param
(
[Parameter(Mandatory=$true)] [string] $Message
)
Try {
$Line = "$Message"
Add-content -Path $Auditfile -Value $Line
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
}
$numbersToPost.ForEach{
Log-Message "~~~~~~~Start~~~~~~~~"
doWAPOST -textForTreatment $_
Log-Message "~~~~~~~Stop~~~~~~~~~"
}
Stop-Transcript
We are now looking to enhance this further to create a provision that uploads also an image file. Is this possible? I am not sure how to upload the file held at variable $Image
. Any thoughts? TIA.