Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

WhatsApp Template

The WhatsApp template provides specialized tools for WhatsApp Business API integration, including template message sending, task creation, and WhatsApp-specific features.

Topic: WhatsApp Business Integration

This template is perfect for:

  • WhatsApp Business API integration
  • Template message campaigns
  • WhatsApp-based customer service
  • Automated WhatsApp notifications
  • Task management via WhatsApp

The Code: send.bas

PARAM phone AS PHONE LIKE "122233333333" DESCRIPTION "WhatsApp phone number with country code"
PARAM template AS STRING LIKE "newsletter-zap.txt" DESCRIPTION "Template file name to send"
PARAM variables AS OBJECT LIKE "{name: 'John'}" DESCRIPTION "Template variables for personalization" OPTIONAL

DESCRIPTION "Send a WhatsApp template message to a phone number"

SEND TEMPLATE TO phone, template, variables

WITH log
    timestamp = NOW()
    phoneNumber = phone
    templateFile = template
    status = "sent"
END WITH

SAVE "whatsapp_log.csv", log

TALK "WhatsApp message sent to " + phone

RETURN phone

Sample Dialogs

These conversations show how the WhatsApp template works in real-world scenarios.

Dialog 1: Sending a Template Message

Dialog 2: Creating a Task via WhatsApp

Dialog 3: Personalized Template with Variables

Keywords Used

KeywordPurpose
PARAMDefine input parameters for the tool
DESCRIPTIONTool description for AI understanding
SEND TEMPLATE TOSend WhatsApp template message
WITH/END WITHCreate structured log object
SAVELog message to CSV file
TALKConfirm action to user
RETURNReturn result

Template Structure

whatsapp.gbai/
β”œβ”€β”€ whatsapp.gbdialog/
β”‚   β”œβ”€β”€ send.bas           # Send template messages
β”‚   └── create-task.bas    # Create tasks via WhatsApp
β”œβ”€β”€ whatsapp.gbkb/
β”‚   β”œβ”€β”€ articles/          # Knowledge base articles
β”‚   β”‚   └── newsletter-zap.txt
β”‚   └── images/            # Media files
└── whatsapp.gbot/
    └── config.csv         # Bot configuration

Create Task Tool: create-task.bas

PARAM title AS STRING LIKE "Call client" DESCRIPTION "Task title"
PARAM due_date AS DATE LIKE "2025-01-20" DESCRIPTION "Due date" OPTIONAL
PARAM priority AS STRING LIKE "medium" DESCRIPTION "Priority: high, medium, low" OPTIONAL

DESCRIPTION "Create a task from WhatsApp conversation"

IF NOT due_date THEN
    due_date = NOW()
END IF

IF NOT priority THEN
    priority = "medium"
END IF

WITH task
    id = "TASK-" + FORMAT(RANDOM(10000, 99999))
    taskTitle = title
    dueDate = due_date
    taskPriority = priority
    createdBy = FROM
    createdAt = NOW()
    status = "pending"
END WITH

SAVE "tasks.csv", task

CREATE TASK title, priority, FROM

TALK "βœ… Task created: " + title
TALK "πŸ“… Due: " + FORMAT(due_date, "MMM DD, YYYY")
TALK "⚑ Priority: " + priority

RETURN task.id

WhatsApp Template Messages

Understanding Template Messages

WhatsApp Business API requires pre-approved templates for initiating conversations. Templates can include:

  • Text: Plain text with optional variables
  • Media: Images, documents, videos
  • Buttons: Quick reply or call-to-action buttons
  • Headers: Text, image, document, or video headers

Template File Format

Create templates in the .gbkb/articles/ folder:

newsletter-zap.txt
---
Hello {{1}}!

Here's your weekly newsletter:

πŸ“° Top Stories This Week
{{2}}

🎯 Don't miss our special offer!
{{3}}

Reply STOP to unsubscribe.

Variables in Templates

Variables are placeholders replaced with actual values:

VariableDescriptionExample
{{1}}First parameterCustomer name
{{2}}Second parameterContent body
{{3}}Third parameterOffer details

Customization Ideas

Add Bulk Messaging

PARAM template AS STRING DESCRIPTION "Template to send"
PARAM contacts_file AS STRING LIKE "contacts.csv" DESCRIPTION "CSV file with contacts"

DESCRIPTION "Send template to multiple contacts"

contacts = FIND contacts_file

sent = 0
failed = 0

FOR EACH contact IN contacts
    variables = {
        "name": contact.name,
        "company": contact.company
    }
    
    result = SEND TEMPLATE TO contact.phone, template, variables
    
    IF result THEN
        sent = sent + 1
    ELSE
        failed = failed + 1
    END IF
    
    WAIT 2  ' Rate limiting
NEXT

TALK "πŸ“Š Bulk send complete!"
TALK "βœ… Sent: " + sent
TALK "❌ Failed: " + failed

Add Message Status Tracking

' After sending
message_id = SEND TEMPLATE TO phone, template, variables

' Store for tracking
WITH messageRecord
    id = message_id
    phone = phone
    template = template
    status = "sent"
    sentAt = NOW()
END WITH

SAVE "message_status.csv", messageRecord

' Webhook handler for status updates
ON WEBHOOK "whatsapp_status"
    status = webhook_data.status
    message_id = webhook_data.message_id
    
    UPDATE "message_status.csv" SET status = status WHERE id = message_id
    
    IF status = "delivered" THEN
        TALK "βœ… Message " + message_id + " delivered"
    ELSE IF status = "read" THEN
        TALK "πŸ‘€ Message " + message_id + " read"
    ELSE IF status = "failed" THEN
        TALK "❌ Message " + message_id + " failed"
    END IF
END ON

Add Interactive Buttons

PARAM phone AS PHONE DESCRIPTION "Recipient phone number"

DESCRIPTION "Send message with quick reply buttons"

template_with_buttons = {
    "template": "order_confirmation",
    "buttons": [
        {"type": "quick_reply", "text": "Track Order"},
        {"type": "quick_reply", "text": "Contact Support"},
        {"type": "quick_reply", "text": "View Details"}
    ]
}

SEND TEMPLATE TO phone, template_with_buttons

TALK "Message with buttons sent to " + phone

Add Media Messages

PARAM phone AS PHONE DESCRIPTION "Recipient phone number"
PARAM image_url AS STRING DESCRIPTION "URL of image to send"
PARAM caption AS STRING DESCRIPTION "Image caption" OPTIONAL

DESCRIPTION "Send WhatsApp message with image"

' Send image with caption
SEND MEDIA TO phone, image_url, caption

WITH log
    timestamp = NOW()
    phone = phone
    mediaType = "image"
    mediaUrl = image_url
    caption = caption
    status = "sent"
END WITH

SAVE "whatsapp_media_log.csv", log

TALK "πŸ“· Image sent to " + phone

WhatsApp Business API Best Practices

Message Timing

  1. Session Messages: Free-form messages within 24-hour window after user message
  2. Template Messages: Pre-approved templates for initiating conversations
  3. Rate Limits: Respect WhatsApp’s messaging limits

Template Approval

  1. Submit templates via WhatsApp Business Manager
  2. Wait for approval (usually 24-48 hours)
  3. Use approved templates only
  4. Follow content guidelines (no promotional content in utility templates)

Phone Number Format

Always use international format without + or spaces:

  • βœ… 5511999999999 (Brazil)
  • βœ… 14155551234 (USA)
  • ❌ +55 11 99999-9999
  • ❌ (11) 99999-9999

Compliance

  1. Opt-in Required: Only message users who have opted in
  2. Opt-out Handling: Honor STOP/unsubscribe requests immediately
  3. Business Verification: Complete WhatsApp business verification
  4. Quality Rating: Maintain high quality rating to avoid restrictions

Logging Structure

The whatsapp_log.csv tracks all messages:

ColumnDescription
timestampWhen message was sent
phoneNumberRecipient phone number
templateFileTemplate used
variablesPersonalization variables
statussent/delivered/read/failed
messageIdWhatsApp message ID

Error Handling

result = SEND TEMPLATE TO phone, template, variables

IF NOT result THEN
    ' Log the failure
    WITH errorLog
        timestamp = NOW()
        phone = phone
        template = template
        error = "Send failed"
    END WITH
    
    SAVE "whatsapp_errors.csv", errorLog
    
    TALK "❌ Failed to send message to " + phone
    TALK "Please verify the phone number and try again."
    RETURN NULL
END IF