Skip to content

Instantly share code, notes, and snippets.

@HunterXProgrammer
Last active February 15, 2023 10:54
Show Gist options
  • Save HunterXProgrammer/b657e8eae8f0b5959f612e6fa536f719 to your computer and use it in GitHub Desktop.
Save HunterXProgrammer/b657e8eae8f0b5959f612e6fa536f719 to your computer and use it in GitHub Desktop.
A bash script to install whatsmeow mdtest into Termux in one line. Also adds support for sending videos/pdf/documents and vloce messages which is missing.
#!/data/data/com.termux/files/usr/bin/bash
echo -ne "\n Checking if Termux has storage permission..."
rm -r ~/storage >/dev/null 2>&1
if ! touch /storage/emulated/0/.tmp_check_termux >/dev/null 2>&1
then
echo -e "\nGrant Termux storage permission and run the script again\n"
termux-setup-storage
exit 1
fi
rm -rf /storage/emulated/0/.tmp_check_termux >/dev/null 2>&1
termux-setup-storage
echo "done"
apt update
pkg install -y git golang 2>/dev/null | grep -E '(Need to get |Get:|Unpacking |Setting up )'
tmpdir="$(mktemp -d)"
cd $tmpdir
git clone https://github.com/tulir/whatsmeow
i="$(grep -m 1 -n 'c := make(chan os.Signal)' whatsmeow/mdtest/main.go | grep -Eo '^[0-9]+')"
stop_line="$(wc -l whatsmeow/mdtest/main.go | grep -Eo '^[0-9]+')"
while (( $i < $stop_line ))
do
((i++))
if sed -n "${i}p" whatsmeow/mdtest/main.go | grep -q "}()"
then
sed -i "${i} a args := os.Args[1:]\nif len(args) > 0 {\nhandleCmd(strings.ToLower(args[0]), args[1:])\nreturn\n}" whatsmeow/mdtest/main.go
break
fi
done
del_start="$(grep -m 1 -n 'case "sendimg":' whatsmeow/mdtest/main.go | grep -Eo '^[0-9]+')"
del_stop="$(grep -m 1 -n 'case "setstatus":' whatsmeow/mdtest/main.go | grep -Eo '^[0-9]+')"
((del_stop--))
sed -i "$del_start,${del_stop}d" whatsmeow/mdtest/main.go
curl -s -O "https://gist.githubusercontent.com/HunterXProgrammer/0278cc5508e04a3cc88a1ee1fc84893f/raw/95b1b0a15806954e7e1e359127639df93a133322/extension_media_whatsmeow_main.go"
sed -i -e '/case "setstatus":/{r extension_media_whatsmeow_main.go' -e 'd}' whatsmeow/mdtest/main.go
rm extension_media_whatsmeow_main.go
value="true"; key="allow-external-apps"; file="/data/data/com.termux/files/home/.termux/termux.properties"; mkdir -p "$(dirname "$file")"; chmod 700 "$(dirname "$file")"; if ! grep -E '^'"$key"'=.*' $file &>/dev/null; then [[ -s "$file" && ! -z "$(tail -c 1 "$file")" ]] && newline=$'\n' || newline=""; echo "$newline$key=$value" >> "$file"; else sed -i'' -E 's/^'"$key"'=.*/'"$key=$value"'/' $file; fi
cd whatsmeow/mdtest
go mod tidy
clear
echo -e "\nFinal step, building mdtest binary. Takes about 10s~1min"
go build
if [ $? -eq 0 ]
then
mv -f ~/whatsmeow/mdtest/mdtest.db ${tmpdir}/whatsmeow/mdtest >/dev/null 2>&1
rm -rf ~/whatsmeow >/dev/null 2>&1
mv ${tmpdir}/whatsmeow ~ >/dev/null 2>&1
echo -e "\nDone, now type \"cd ~/whatsmeow/mdtest && ./mdtest\" to\ncheck if WhatsApp qr code is generated properly.\n\nNote:- In case qr code is too big, you can pinch the\nscreen to resize it.\n\nThe code refreshes after some time so quickly take a\npicture of it using a spare phone and\nopen WhatsApp -> ⋮ (menu) -> Linked Devices\nand scan this code in the main device.\n\nGreat, you will now be able to use CLI commands to send WhatsApp text messages/images/videos/pdf/documents, etc.\nYou can integrate this with automation apps like Tasker and even create WhatsApp chatbots.\n\nCheck Reddit post for more info -\n\nhttps://www.reddit.com/r/tasker/comments/10wiahq/howto_send_imagesvideospdfdocuments_in_whatsapp/\n"
fi
rm -rf $tmpdir >/dev/null 2>&1
go clean -cache
pkg clean
@HunterXProgrammer
Copy link
Author

HunterXProgrammer commented Feb 7, 2023

Thanks to this Reddit Tasker post and efforts of many, you can now use Tasker to send any WhatsApp messages or images.

However, some people find it difficult to install it.

This script is meant to super simplify the installation of whatsmeow mdtest into Termux in one line.

It also adds support for sending videos/pdf/documents which is missing.

Just open Termux and type this and press enter -

curl -s "https://gist.githubusercontent.com/HunterXProgrammer/b657e8eae8f0b5959f612e6fa536f719/raw/b3c39fef8e91c2a461a03bb9a1798fd8a8bc4358/install_whatsmeow_termux.sh" | bash

This will fully automate the installation.

Now to connect it to WhatsApp as described in the Reddit post -

Type cd ~/whatsmeow/mdtest && ./mdtest to
check if WhatsApp qr code is generated properly.

Note:- In case qr code is too big, you can pinch the screen to resize it.

The code refreshes after some time so quickly take a picture of it using a spare phone and
open WhatsApp -> ⋮ (menu) -> Linked Devices
and scan this code in the main device.

Great, you will now be able to use CLI commands to send WhatsApp text messages/images/videos/pdf/documents, etc.

You can integrate this with automation apps like Tasker and even create WhatsApp chatbots.

Here is the Tasker Reddit post that has the Taskernet importable Tasks -

https://www.reddit.com/r/tasker/comments/10wiahq/howto_send_imagesvideospdfdocuments_in_whatsapp/

Here is a full list of commands that whatsmeow mdtest handles. Here are some examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment