formattingwhatsapp
v1.0.0
Published
A fluent TypeScript library for formatting WhatsApp messages.
Maintainers
Readme
Formatting WhatsApp
A fluent, type-safe TypeScript library for formatting WhatsApp messages.
Features
- Basic Formatting: Bold, Italic, Strikethrough, Monospace, Quote.
- Lists: Bullet, Numbered, and Indented lists.
- Fluent API:
MessageBuilderfor chaining commands.
Installation
bun add formattingwhatsappUsage
Basic Formatting
import { bold, italic, strike, monospace } from 'formattingwhatsapp'
// Example: Sending a simple formatted message
const message = `${bold('Welcome!')} This is ${italic('my bot')}.`
await sock.sendMessage(chatId, { text: message })Combination Formatting
You can combine formatters by nesting functions. The library automatically trims whitespace to ensure WhatsApp recognizes the formatting options.
import { bold, italic, strike } from 'formattingwhatsapp'
// Bold + Italic
// Output: *_Text_*
const bi = bold(italic('Text'))
// Bold + Italic + Strikethrough
// Output: ~*_Text_*~
const bis = strike(bold(italic('Text')))
await sock.sendMessage(chatId, { text: bis })Fluent Message Builder
import { MessageBuilder } from 'formattingwhatsapp'
const message = new MessageBuilder()
.bold('Welcome to the Bot!')
.newline()
.text('Here are your options:')
.newline()
.bulletList(['Option 1', 'Option 2', 'Option 3'])
.newline()
.quote('Reply with your choice.')
.build()
// Sending the built message
await sock.sendMessage(chatId, { text: message })Fluent Message Builder
import { MessageBuilder, bold, italic } from 'formattingwhatsapp'
const message = new MessageBuilder()
.text('Standard text ')
.bold(italic('Bold and Italic')) // Nesting works here too!
.newline()
.text('Standard text again')
.build()
await sock.sendMessage(chatId, { text: message })API Reference
Text Formatters
bold(text: string): stringitalic(text: string): stringstrike(text: string): stringmonospace(text: string): stringcode(text: string): stringquote(text: string): string
List Formatters
bulletList(items: string[], symbol?: string): stringnumberedList(items: string[]): stringindentedList(items: string[], indent?: string, symbol?: string): string
MessageBuilder Methods
text(text: string)bold(text: string)italic(text: string)strike(text: string)monospace(text: string)code(text: string)quote(text: string)bulletList(items: string[], symbol?: string)numberedList(items: string[])newline(count?: number)build(): string
License
ISC
