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
π¬
WhatsApp Bot
online
Today
Dialog 2: Creating a Task via WhatsApp
π¬
WhatsApp Bot
online
Dialog 3: Personalized Template with Variables
π¬
WhatsApp Bot
online
Keywords Used
| Keyword | Purpose |
|---|---|
PARAM | Define input parameters for the tool |
DESCRIPTION | Tool description for AI understanding |
SEND TEMPLATE TO | Send WhatsApp template message |
WITH/END WITH | Create structured log object |
SAVE | Log message to CSV file |
TALK | Confirm action to user |
RETURN | Return 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:
| Variable | Description | Example |
|---|---|---|
{{1}} | First parameter | Customer name |
{{2}} | Second parameter | Content body |
{{3}} | Third parameter | Offer 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
- Session Messages: Free-form messages within 24-hour window after user message
- Template Messages: Pre-approved templates for initiating conversations
- Rate Limits: Respect WhatsAppβs messaging limits
Template Approval
- Submit templates via WhatsApp Business Manager
- Wait for approval (usually 24-48 hours)
- Use approved templates only
- 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
- Opt-in Required: Only message users who have opted in
- Opt-out Handling: Honor STOP/unsubscribe requests immediately
- Business Verification: Complete WhatsApp business verification
- Quality Rating: Maintain high quality rating to avoid restrictions
Logging Structure
The whatsapp_log.csv tracks all messages:
| Column | Description |
|---|---|
| timestamp | When message was sent |
| phoneNumber | Recipient phone number |
| templateFile | Template used |
| variables | Personalization variables |
| status | sent/delivered/read/failed |
| messageId | WhatsApp 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
Related Templates
- broadcast.bas - Mass messaging to contact lists
- store.bas - E-commerce with WhatsApp notifications
- bank.bas - Banking notifications via WhatsApp