Saturday, February 17, 2024

Automating WhatsApp - Part 2: On Windows Machines

 


In my previous post (Automating WhatsApp Using AppleScript), I created a simple script that allows a personal user to make delivering personalized messages using WhatsApp streamlined (still requiring some manual work, but very little...).

The problem with the previous solution was that it only works on Mac, and I know many of my friends are using Windows machines. Some asked me to adapt my solution to Windows, so I had to install Parallels on my Mac (a very smooth and nice experience - more on that later), and instal Visual Basic .Net (the free community edition), and re-learn my VisualBasic/.Net skills... 

The solution I came up with is not totally robust and has some drawbacks, but it might help someone, so here it is..

How To Use

1. Install the app: and Installer for the app is available here: https://bit.ly/3I2mSu7, the file is a zip file with a setup.exe installer. There is also a sample data files folder to help you get started.

2. Make Sure Microsoft Edge is your default browser, and log in to web.whatsapp.com on the browser before you run the tool. Close WhatsApp tabs, but leave the session open (have some other tab open).

Run the TellMyFriends app, select the data file (data.csv), which should live in a folder alongside the text templates (explained later). Once you select the file, a preview of your list is shown, and you can then click the start button when you want to send the messages.

3. For each recipient, the tool will open the browser with a whatsapp tab, and start a chat with the phone number, and the message for the user.

Example Files

1, Data.csv

name,phone,template

John,12123334444,friend.txt

Elena,12123335555,work.txt

Noa,12123336666,friend.txt


2. Templates (in the above file three templates are referenced, here is an example of one - work.txt):

Dear FNAME,
as a work colleague I would love to see you at my birthday party this Saturday 8pm
My address is 102 Main St. Someville
Hope to see you there!
Srool

Any occurence of the FNAME in the template will be replaced by the recipient name.

Behind The Scenes - For Developers

OK, the code is written in VB.Net (using the community edition of Visual Studio).

This is the form I designed:



And the code can be found in this GitHub repo:




Thursday, February 15, 2024

Automating WhatsApp Using AppleScript



Edit: added a windows version, and all source code is available here: https://github.com/iroth/TellMyFriends




I was looking for a way to send a message to a distribution list. This is a feature that in its simplest form is part of the WhatsApp mobile app, but its a bit cumbersome, and what's more - it does not allow to customize messages for each recipient.

There are third party tools, and WhatsApp business API, but I am just a simple guy who needs to send his friends some personal messages (think about invitations to a party or greetings for the holidays), so I was trying all kind of ways, and then found a reasonable solution using JavaScript in Mac Script Editor.

The idea is to create a data file with a list of recipients, at the minimum it is something like:

{
"recipients": [
{"name": "John", "phone": "12123334444"},
{"name": "Marry", "phone": "12123335555"},
{"name": "Anna", "phone": "12123336666"},
]
}

With a first name and phone number.

Save the list to a file named "data.json" in a new empty folder.

Create a message template file, like this:

Hello FNAME,
I would love to invite you to my birthday party on Saturday night at 8PM.
Lots of food and beer - don't eat too much before the party...
see ya!

Save this to a file called "template.txt" in that same folder as the data.json file.

Then create this script (since I use Hebrew, I needed to read the file as UTF-8):

ObjC.import('Foundation')


const readFileEnc = function (path, encoding) {

    !encoding && (encoding = $.NSUTF8StringEncoding)


    const fm = $.NSFileManager.defaultManager

    const data = fm.contentsAtPath(path)

    const str = $.NSString.alloc.initWithDataEncoding(data, encoding)

    return ObjC.unwrap(str)

}


const sendOneMessage = function (chromeapp, window, url) {


 var ic = window.tabs.length;

 window.tabs.push(new chrome.Tab());

  window.tabs[1].url = url;

  while (window.tabs.length > ic) {

  this.console.log("waiting...");

  }


}


var app = Application("Script Editor");

app.includeStandardAdditions = true;

var folder = app.chooseFolder({ withPrompt: 'Please select the data folder' })

var file = folder + "/data.json";

var dataStr = readFileEnc(file)

var data = JSON.parse(dataStr);


var chrome = Application('Google Chrome');

var window = chrome.Window().make();

var countDoc = 0;


for (var i = 0 ; i < data.recipients.length ; i++) {

var tfile = folder + "/template.txt";

var text = readFileEnc(tfile);

text = encodeURIComponent(text.replace(/FNAME/g, data.recipients[i].name));

var dest_url = "https://web.whatsapp.com/send/?phone=" + data.recipients[i].phone + "&text=" + text + "&type=phone_number";

sendOneMessage(chrome, window, dest_url);

countDoc++;

}


window.close();


countDoc


Before you run this script, make sure you logged in to WhatsApp Web in Chrome. The script will open a new window in Chrome, and then a tab with WhatsApp Web setup with the phone number and text ready for you, click the send button manually, and close the tab to get the next record opened. 

If you only have 30-40 recipients, this will be quite a smooth and fulfilling experience.

Enjoy!