Easily construct rich WhatsApp messages with bold, italic, strikethrough, monospace, and more! This library extends string-builder
npm package with custom formatting options to simplify building professional, formatted WhatsApp messages programmatically.
Install the package using npm:
npm install whatsapp-messager
yarn add whatsapp-messager
- Rich Text Formatting: Bold, italic, strikethrough, and monospace text.
- Lists: Create ordered and unordered lists effortlessly.
- Template Placeholders: Easily replace placeholders with dynamic values.
- Quoting: Add blockquotes seamlessly.
- Chaining: Use method chaining for clean, readable code.
Here’s how you can get started with the WhatsApp Messager
:
import StringBuilder from 'whatsapp-messager';
const builder = new StringBuilder();
const message = builder
.bold("Welcome to WhatsApp!")
.italic("We hope you enjoy your stay.")
.quote("Here are some tips to get started:")
.unorderedList([
"Send messages to your friends.",
"Create group chats for family or work.",
"Use formatting for emphasis.",
])
.strikethrough("This is an outdated feature.")
.toString();
console.log(message);
*Welcome to WhatsApp!*
_We hope you enjoy your stay._
> Here are some tips to get started:
* Send messages to your friends.
* Create group chats for family or work.
* Use formatting for emphasis.
~This is an outdated feature.~
Append/add text in the string.
Example:
builder.append("This is some text.");
Appends text in a new line.
Example:
builder.appendLine("This is in a new line.");
Formats the given text as bold.
Example:
builder.bold("This is bold text.");
Formats the given text as italic.
Example:
builder.italic("This is italic text.");
Formats the given text with strikethrough.
Example:
builder.strikethrough("This is strikethrough text.");
Formats the given text as monospace.
Example:
builder.monospace("This is monospace text.");
Adds a blockquote.
Example:
builder.quote("This is a quote.");
Creates a list, either ordered or unordered.
Example:
builder.list(["First item", "Second item"], true); // Ordered
builder.list(["First item", "Second item"]); // Unordered
builder.list(["First item", "Second item"], false); // Unordered
Returns the constructed message as a string. Optionally replaces placeholders with values from an object.
Example:
const template = "Hello, {name}! Your order {orderNumber} is ready.";
const message = builder.toString({ name: "Jane", orderNumber: "12345" });
console.log(message);
Output:
Hello, Jane! Your order 12345 is ready.
Clears string.
Example:
builder.clear();
Use placeholders and escape {}
if needed:
const message = builder
.bold("Order Details:")
.append("Your order {reference} has been confirmed.")
.toString({ reference: 12345 });
console.log(message);
Output:
*Order Details:*
Your order 12345 has been confirmed.
const message = builder
.bold("Order Details:")
.append("Your order \{12345\} has been confirmed.")
.toString();
console.log(message);
Output:
*Order Details:*
Your order {12345} has been confirmed.
- Efficiency: Build complex messages programmatically.
- Clean Code: Avoid messy string concatenation.
- Dynamic Messages: Replace placeholders with dynamic values easily.
Feel free to submit issues or pull requests to help improve this library. Contributions are always welcome!
MIT