Converts free-text labels into a safe "slug" format suitable for use as
message button IDs or other identifiers in Whapi API requests.
Ensures that IDs contain only lowercase letters, digits, and underscores,
and are never empty (defaults to "btn" if the input is blank).
Details
This function is particularly useful when creating interactive messages
(buttons, lists) in WhatsApp via Whapi, where each button requires a valid
id. By whapi_slugifying titles automatically, we can safely generate IDs even if
users provide arbitrary labels with spaces, accents, or symbols.
Transformation steps:
Convert to lowercase;
Replace any sequence of non-alphanumeric characters with
_;Trim leading/trailing underscores;
Replace empty results with
"btn".
See also
Used internally in whapi_send_quick_reply() and other
interactive message helpers.
Examples
whapi_slugify(c("Yes!", "Call Us", "Sale!", "###"))
#> [1] "yes" "call_us" "sale" "btn"
# -> "yes", "call_us", "promocao_rapida", "btn"
# Use case in button creation:
titles <- c("Buy Now", "Learn More")
ids <- whapi_slugify(titles)
tibble::tibble(title = titles, id = ids)
#> # A tibble: 2 × 2
#> title id
#> <chr> <chr>
#> 1 Buy Now buy_now
#> 2 Learn More learn_more
