WhatsApp Bot
Stateful conversational bot — FAQ, forum bridge, and broadcasts.
The backend includes a stateful WhatsApp bot keyed by phone number (sessions map[string]*UserSession). Inbound messages arrive at the /webhook endpoint (registered at root, not under /api), parsed asynchronously.
Bot flow
FAQ flow
Typing faq presents a numbered menu (1–8) of curated stroke-health content:
- Good/bad foods for stroke, diabetes, hypertension
- Self-therapy video links
- Herbal medicine guidance
Responses are pre-written health-education messages with source links (hellosehat, rspondokindah, etc.).
Forum flow
Typing forum walks the user through category → title → content, then creates a real forum thread via forumService.CreateThread — which requires the phone number to be registered as a user.
Commands (command.go)
| Command | Action |
|---|---|
bantuan | Help |
faq | FAQ menu |
semuaThread | List threads |
getThread <id> | Show a thread |
getReply <threadId>[:<replyId>[:...]] | Traverse nested replies (colon-separated ID chain) |
reply <threadId>[:<replyId>] <message> | Post a reply |
Nested-reply traversal uses a colon-separated ID chain, so getReply 5:3:2 walks thread 5 → reply 3 → reply 2.
Broadcast notifications
New forum thread
SendNewThreadNotification (whatsapp/service.go):
- Fetches all users.
- Filters by matching
category.Nameagainstuser.Preferences(string array). - Preferences include religious-support categories (Islam/Kristen/Katolik/Hindu/Buddha/Konghucu/Kepercayaan Lainnya) and content categories (Informasi Medis, Kesehatan Mental, Berbagi Pengalaman).
- Stores a
WhatsappThreadContextper send (whatsapp_message_id↔forum_thread_id↔user_whatsapp_number) so WA replies can be correlated back to the thread.
New article / new video
SendNewArticleNotification / SendNewVideoNotification use a randomized normal-distribution delay (mean 15s, σ 5s, floor 4s) between sends to avoid WhatsApp gateway rate-limiting.
Reply correlation
The whatsapp_thread_contexts table is the bridge that lets an inbound WhatsApp reply to a broadcast message be routed back to the originating forum thread — enabling the "reply via WhatsApp" loop.
Gateway
The external WhatsApp gateway uses Basic auth (WHATSAPP_API_USERNAME / WHATSAPP_API_PASSWORD) and POST /send_message at WHATSAPP_API_ENDPOINT. Outbound sends are dispatched in goroutines so the HTTP request that triggered them (e.g. POST /api/forum/threads) returns immediately.