Created
February 4, 2023 04:34
-
-
Save kugland/3c29ec133b1d56a0d839ddf9d72f4dde to your computer and use it in GitHub Desktop.
Open WhatsApp from the shell with Termux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/data/data/com.termux/files/usr/bin/env lua | |
-- Escape a string for use in a shell command | |
local function shell_escape(args) | |
local ret = {} | |
local s | |
for _, a in pairs(args) do | |
s = tostring(a) | |
if s:match("[^A-Za-z0-9_/:=-]") then | |
s = "'" .. s:gsub("'", "'\\''") .. "'" | |
end | |
table.insert(ret, s) | |
end | |
return table.concat(ret, " ") | |
end | |
-- Convert a character to its hex representation | |
local char_to_hex = function(c) | |
return string.format("%%%02X", string.byte(c)) | |
end | |
-- URL encode a string | |
local function urlencode(url) | |
if url == nil then | |
return | |
end | |
url = url:gsub("\n", "\r\n") | |
url = url:gsub("([^%w ])", char_to_hex) | |
url = url:gsub(" ", "+") | |
return url | |
end | |
-- Get the phone number and message from the arguments | |
local phone = arg[1] | |
local message = arg[2] | |
-- The phone number is required | |
if phone == nil then | |
print("Usage: open-whatsapp <phone> [message]") | |
os.exit(1) | |
else | |
-- Strip any non-digit characters from the phone number | |
phone = phone:gsub("%D", "") | |
end | |
-- Generate a wa.me link (with the message if it exists) | |
local link = "https://wa.me/" .. phone | |
if message ~= nil then | |
link = link .. "?text=" .. urlencode(message) | |
end | |
-- Open the link | |
os.execute("( " .. shell_escape({ "termux-open-url", link }) .. " >/dev/null 2>&1 & )") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment