Skip to content

LarvenLLC/whatsapp-messager

Repository files navigation

WhatsApp Messager (Message Builder)

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.


Installation

Install the package using npm:

npm install whatsapp-messager
yarn add whatsapp-messager

Features

  • 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.

Getting Started

Here’s how you can get started with the WhatsApp Messager:

Example Usage

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);

Output

*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.~

API Reference

1. append(text: string): StringBuilder

Append/add text in the string.

Example:

builder.append("This is some text.");

2. appendLine(text: string): StringBuilder

Appends text in a new line.

Example:

builder.appendLine("This is in a new line.");

3. bold(text: string, isNewLine?: boolean): StringBuilder

Formats the given text as bold.

Example:

builder.bold("This is bold text.");

4. italic(text: string, isNewLine?: boolean): StringBuilder

Formats the given text as italic.

Example:

builder.italic("This is italic text.");

5. strikethrough(text: string, isNewLine?: boolean): StringBuilder

Formats the given text with strikethrough.

Example:

builder.strikethrough("This is strikethrough text.");

6. monospace(text: string, isNewLine?: boolean): StringBuilder

Formats the given text as monospace.

Example:

builder.monospace("This is monospace text.");

7. quote(text: string): StringBuilder

Adds a blockquote.

Example:

builder.quote("This is a quote.");

8. list(texts: string[], isOrdered?: boolean): StringBuilder

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

9. toString(data?: object): string

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.

10. clear(): StringBuilder

Clears string.

Example:

builder.clear();

Advanced Usage

Dynamic Templates with Escaping

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.

Why Use This Library?

  1. Efficiency: Build complex messages programmatically.
  2. Clean Code: Avoid messy string concatenation.
  3. Dynamic Messages: Replace placeholders with dynamic values easily.

Contribution

Feel free to submit issues or pull requests to help improve this library. Contributions are always welcome!


License

MIT

About

Use intuitive functions to construct whatsApp message templates / strings.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published