AI 對話紀錄  March 27, 2025

【AG-10】Basic Design of WhatsApp Send-out Function

【AG-10】Basic Design of WhatsApp Send-out Function

Q: This morning it comes an idea that the pastor has to coin WhatsApp messages from time to time to connect with volunteers and potential donors. I want to add a messaging table where the pastor's colleagues can draft WhatsApp messages, get it reviewed and approved, and then send out to a WhatsApp number or a group (E.g. Using a calculated field to output the message with the send link). The table can have key fields like "message", "phone_no" and "send_out" (calculated field). The approval mechanism has been discussed earlier (But we can skip that and focus on the messaging part for now).

<>

Q: Sorry for haven't make it clear that I intended the messaging table to be in the AppGini-generated app, not on the public website. I also want to ask if it's possible to facilitate sending the same message to multiple numbers (Imagine a "recipient" field to add numbers manually or from a related table) and generate multiple send out links (But would forwarding directly in WhatsApp Business be simpler?)


A: No worries at all—thanks for clarifying that you want the messaging table in the AppGini-generated app, not the public website! That makes perfect sense for an admin-side feature where the pastor’s colleagues can draft and manage WhatsApp messages securely. I’ll adjust the approach to fit within AppGini’s framework. As for sending the same message to multiple numbers, I’ll explore both options—adding a recipient field (manual entry or linked to a related table) with multiple send-out links, and comparing that to forwarding via WhatsApp Business—to see what’s simpler and more practical.

Adjusted Plan for AppGini

  • Table: Create whatsapp_messages in AppGini with fields tailored to its structure.

  • Fields:

    • message: The text to send.

    • recipients: A field for multiple phone numbers (either text or linked to a related table).

    • send_out: A calculated field generating WhatsApp links for each recipient.

  • AppGini Integration: Use hooks to customize the interface and logic.

  • Multiple Recipients: Support multiple numbers with individual links, then compare to WhatsApp Business forwarding.

Step 1: Define the Table in AppGini

In your AppGini project:

  1. Create a new table called whatsapp_messages.

  2. Add these fields:

    • id: Primary key, auto-increment (default in AppGini).

    • message: Text field (e.g., Text, 255 characters or more, required).

    • recipients: Text field (e.g., Text, allow longer input for comma-separated numbers like +85212345678,+85298765432), or a lookup to a related table (see below).

    • send_out: Calculated field (we’ll define the logic in a hook since AppGini doesn’t natively support multi-value calculated fields).

    • created_at: DateTime field (optional, for tracking).

For recipients, you have two options:

  • Option 1: Simple Text Field: Store numbers as a comma-separated string (e.g., +85212345678,+85298765432).

  • Option 2: Related Table: Create a recipients table (e.g., id, phone_no, whatsapp_message_id) with a one-to-many relationship to whatsapp_messages.

I’ll start with Option 1 (simpler) and outline Option 2 later if you prefer.

Step 2: AppGini Table Setup

In AppGini:

  • Table Name: whatsapp_messages

  • Fields:

    • id: Auto-increment, Primary Key.

    • message: Text, Required.

    • recipients: Text, Description: “Enter phone numbers separated by commas (e.g., +85212345678,+85298765432)”.

    • send_out: Text, Calculated (we’ll populate this via a hook).

    • created_at: DateTime, Default: Current date/time.

Save and generate the app to create the basic structure.

Step 3: Hook to Generate Multiple Send-Out Links

Since send_out needs to generate multiple WhatsApp links based on recipients, we’ll use an AppGini hook. Edit hooks/whatsapp_messages.php:

php

<?php
// hooks/whatsapp_messages.php

function whatsapp_messages_init(&$options, $memberInfo, &$args) {
    return TRUE;
}

function whatsapp_messages_before_insert(&$data, $memberInfo, &$args) {
    // Generate send_out links before saving
    if (!empty($data['recipients']) && !empty($data['message'])) {
        $recipients = explode(',', $data['recipients']);
        $encoded_message = urlencode($data['message']);
        $links = [];
        foreach ($recipients as $phone) {
            $phone = trim($phone);
            if ($phone) {
                $links[] = "https://wa.me/{$phone}?text={$encoded_message}";
            }
        }
        $data['send_out'] = implode('||', $links); // Store links separated by '||'
    }
    return TRUE;
}

function whatsapp_messages_before_update(&$data, $memberInfo, &$args) {
    // Same logic for updates
    if (!empty($data['recipients']) && !empty($data['message'])) {
        $recipients = explode(',', $data['recipients']);
        $encoded_message = urlencode($data['message']);
        $links = [];
        foreach ($recipients as $phone) {
            $phone = trim($phone);
            if ($phone) {
                $links[] = "https://wa.me/{$phone}?text={$encoded_message}";
            }
        }
        $data['send_out'] = implode('||', $links);
    }
    return TRUE;
}

function whatsapp_messages_after_insert($data, $memberInfo, &$args) {
    return TRUE;
}

function whatsapp_messages_after_update($data, $memberInfo, &$args) {
    return TRUE;
}

function whatsapp_messages_dv($selectedID, $memberInfo, &$html, &$args) {
    // Customize the detail view to display clickable links
    if ($selectedID) {
        $send_out = sqlValue("SELECT send_out FROM whatsapp_messages WHERE id='$selectedID'");
        if ($send_out) {
            $links = explode('||', $send_out);
            $html .= "<div class='form-field'><label>Send Out Links:</label><ul>";
            foreach ($links as $index => $link) {
                $html .= "<li><a href='{$link}' target='_blank'>Send to Recipient " . ($index + 1) . "</a></li>";
            }
            $html .= "</ul></div>";
        }
    }
}
?>

How It Works

Step 4: UI in AppGini

  • Table View: Shows id, message, recipients, and send_out (raw string).

  • Detail View: Shows the form fields plus a list of clickable WhatsApp links under “Send Out Links”.

Option 2: Related Table for Recipients

If you prefer a related table:

  1. Create a recipients table in AppGini:

    • id: Primary Key.

    • phone_no: Text, Required.

    • whatsapp_message_id: Lookup to whatsapp_messages.id (Parent table).

  2. Adjust the hook to query related records and generate links:

php

function whatsapp_messages_before_insert(&$data, $memberInfo, &$args) {
    unset($data['send_out']); // Calculated later
    return TRUE;
}

function whatsapp_messages_before_update(&$data, $memberInfo, &$args) {
    unset($data['send_out']); // Calculated later
    return TRUE;
}

function whatsapp_messages_dv($selectedID, $memberInfo, &$html, &$args) {
    if ($selectedID) {
        $message = sqlValue("SELECT message FROM whatsapp_messages WHERE id='$selectedID'");
        $encoded_message = urlencode($message);
        $recipients = sql("SELECT phone_no FROM recipients WHERE whatsapp_message_id='$selectedID'", $eo);
        $html .= "<div class='form-field'><label>Send Out Links:</label><ul>";
        $index = 1;
        while ($row = db_fetch_assoc($recipients)) {
            $phone = $row['phone_no'];
            $link = "https://wa.me/{$phone}?text={$encoded_message}";
            $html .= "<li><a href='{$link}' target='_blank'>Send to Recipient {$index}</a></li>";
            $index++;
        }
        $html .= "</ul></div>";
    }
}
  • UI: Use AppGini’s child table feature to manage recipients directly in the whatsapp_messages detail view.

Comparison to WhatsApp Business Forwarding

  • Multiple Links (Current Solution):

    • Pros: Simple to implement, works with any WhatsApp account, generates individual links for each recipient.

    • Cons: Requires manually clicking each link to send (no bulk send), not scalable for large groups.

  • WhatsApp Business Forwarding:

    • Pros: With WhatsApp Business API or app, you can send one message and forward it to multiple contacts/groups in bulk, reducing manual effort.

    • Cons: Requires a WhatsApp Business account, potentially API setup (more complex), and manual forwarding still needs some clicks unless automated via API.

Recommendation: For simplicity and your current setup, the multiple-links approach (Option 1 or 2) is better to start with—it fits AppGini, requires no external service, and meets your immediate need. If the pastor’s team grows or needs bulk sending (e.g., 50+ recipients), WhatsApp Business with forwarding or API integration would be more efficient. For now, I’d stick with Option 1 (comma-separated recipients) unless you already have a contacts table to link to.

Next Steps

  1. Add the whatsapp_messages table in AppGini as outlined (Option 1).

  2. Upload the hook file to hooks/whatsapp_messages.php.

  3. Test it in your AppGini app (e.g., http://admin.nlt-chaplaincy.org/admin/whatsapp_messages.php).

  4. Let me know if you want Option 2 instead or need styling tweaks!

What do you think—does Option 1 work for your team’s workflow, or should we pivot to a related table or WhatsApp Business?

Original Chat


Sharing is sexy
Claudio
Claudio
liclaudio38@gmail.com
Claudio

About Me

Li

Copyright © 2023 Curation Web 我的資訊「展」輯. All Rights Reserved | Powered by Bludit | Theme by BlThemes