Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Engine Works

Under the hood of Alteryx: tips, tricks and how-tos.
Aguisande
15 - Aurora
15 - Aurora

Some time ago, somebody asked me if it was possible to send WhatsApp messages with Alteryx. The use case was basically following up the execution of a workflow (a 12 hour running workflow BTW), without being in front of the computer all the time.

 

Of course, the answer was yes (my answer is always is yes when someday asks “can you do this in Alteryx?”). My only concern was the HOW, so thanks to this customer, we will see how to do this.

 

First note: We are going to use personal accounts, not business accounts to send the messages, because that is what the customer had available.

 

Second note (and first hiccup): This procedure only works on WhatsApp Web.

 

Third note (and second hiccup): Your browser, where the WhatsApp interface will open, MUST be in the same screen of your Designer (this is the way this package works, at least for now with version 5.2).

 

Last hiccup: This doesn’t work with AMP enabled.

 

Also, remember to scan the WhatsApp Web QR Code before using this tool, otherwise, it’ll fail to use your account and send the messages.

 

With all this said, let’s jump into the solution.

 

I came across this nice Python package (pywhatkit) that allows you to automate the usage of WhatsApp Web via certain methods, among other things (like creating ASCII art from a picture, send emails with HTML code, convert text to handwriting and even shutdown your computer).

 

We’ll be focusing on these methods related to using WhatsApp:

 

sendwhatmsg(phone_no: str, message: str, time_hour: int, time_min: int, wait_time: int = 15, tab_close: bool = False, close_time: int = 3) -> None

 

This function is used to send WhatsApp messages to a particular number at a time given by the user. The time should be given in 24 hour format. The tab_close parameter specifies whether the tab should we closed after sending the message or not. You can control the time after which the tab should be closed by specifying the close_time parameter (in seconds).

 

Example: pywhatkit.sendwhatmsg("+910123456789", "Hello", 12, 12, 30)

 

sendwhatmsg_to_group(group_id: str, message: str, time_hour: int, time_min: int, wait_time: int = 15, tab_close: bool = False, close_time: int = 3) -> None

 

This function is used to send WhatsApp messages to a group. The Group ID here is something that you can find in the group invite link.

 

For example in https://chat.whatsapp.com/AB123CDEFGHijklmn, AB123CDEFGHijklmn is the Group ID. Please note only the group admin can provide you with the Group Invite Link. The tab_close parameter specifies whether the tab should be closed or not after sending the message. You can control the time after which the tab is closed by specifying the close_time parameter (in seconds).

 

Example: pywhatkit.sendwhatmsg_to_group("AB123CDEFGHijklmn", "hello", 12, 12, 30, True, 5)

 

In the above example, the tab will be closed in 5 seconds after the message has been delivered.

 

sendwhatmsg_instantly(phone_no: str, message: str, wait_time: int = 15, tab_close: bool = False, close_time: int = 3) -> None

 

This function is used to send the message instantly. The tab_close parameter specifies whether the tab should be closed or not after sending the message.

 

Example: pywhatkit.sendwhatmsg_instantly("+911234567890", "hello", 15, True, 4) (Closes the tab in 4 seconds after the message has been delivered)

 

pywhatkit.sendwhatmsg_instantly("+911234567890", "hello", 15) (Doesn't close the tab after the message has been delivered)

 

sendwhats_image(receiver: str, img_path: str, caption: str = "", wait_time: int = 15, tab_close: bool = False, close_time: int = 3) -> None

 

This function is used to send Images and GIFs to WhatsApp users. For Linux based distributions you need copyq installed on your system. The caption parameter is used to specify the text that is to be sent with the Image. You can also leave it to its default value.

 

NOTE: Windows users can send Images (all formats) and GIFs. For Linux based distributions, only JPEG and PNG are supported. For MacOS users, only JPEG is supported currently.

 

Example: pywhatkit.sendwhats_image("+911234567890", "C:\\Image.png", "Here is the image", 10, True, 5)

 

In the above example, the tab will be closed in 5 seconds after the image has been delivered.

 

You can use it to send Images to Group also.

 

Example: pywhatkit.sendwhats_image("AB123CDEFGHijklmn", "C:\\Image.png", "Here is the image", 10)

 

So I gave them a try within Alteryx. When I was sure it worked as desired, I built a simple UI to help configuring it.

 

Aguisande_0-1644263896024.png

 

The interface is fairly simple even when there is a lot of text (just for guiding users how to use it) and the macro is simple, too.

 

Aguisande_1-1644263966191.png

 

Since we are going to be using Python to send the messages, we need a mechanism to handle parameters coming from the UI of the tool. That’s where the Text Input comes in place. I used it to setup the parameters, the interface tools will modify them, so we can access them from the Python Tool.

 

Since this is just a notification tool, I added a pass-through segment that allows the workflow to pass the received data to the next tool after sending the message and keep processing downstream.

 

And the Python code:

 

 

from ayx import Alteryx
import pywhatkit as pwt
import datetime

# Read the parameters
params=Alteryx.read("parameters")
# Parse the parameters
if '+' not in params['PHONE']:
    phone='+'+params['PHONE'].to_string(index=False).strip()
message=params['TEXT'].to_string(index=False).strip()
wait_time=params['WAIT'].to_string(index=False).strip()
workflow=params['WORKFLOW'].to_string(index=False)
wfdir=Alteryx.getWorkflowConstant("Engine.WorkflowDirectory")
run_time=str(datetime.datetime.now())
include_dt=params['DT'][0]

message=message.replace('{wfName}',workflow)
message=message.replace('{wfDir}',wfdir)
print("Include_DT= ",include_dt)

# Include datetime if selected
if include_dt == 1:
    message=message + " - "+ run_time
    print(message)
else:
    print(message)

# Send the message
pwt.sendwhatmsg_instantly(phone,message,int(wait_time),True,4)

 

 

This is a fairly simple tool, that helped somebody a lot on his use case… I hope it does that for you, too! And can’t wait to know how you use it.

 

Here is the link to the tool in the Gallery: WhatsApp Message Sender

 

Comments