| 
 | 1 | +# WhatsApp Integration Setup Guide  | 
 | 2 | + | 
 | 3 | +This guide will help you set up and use the WhatsApp messaging functionality that has been added to your Rails application.  | 
 | 4 | + | 
 | 5 | +## What Was Added  | 
 | 6 | + | 
 | 7 | +The following components have been integrated into your existing Rails app:  | 
 | 8 | + | 
 | 9 | +### 1. Database  | 
 | 10 | +- **Migration**: `db/migrate/20251015112429_create_whatsapp_messages.rb`  | 
 | 11 | +- **Model**: `app/models/whatsapp_message.rb`  | 
 | 12 | +- Stores WhatsApp message records with fields: `to`, `from`, `text`, `status`, `message_uuid`, `is_inbound`  | 
 | 13 | + | 
 | 14 | +### 2. Controller  | 
 | 15 | +- **File**: `app/controllers/outbound_whatsapp_controller.rb`  | 
 | 16 | +- **Actions**:  | 
 | 17 | +  - `new` - Display the form for sending WhatsApp messages  | 
 | 18 | +  - `create` - Send a text WhatsApp message  | 
 | 19 | +  - `interactive` - Send an interactive WhatsApp message with reply buttons  | 
 | 20 | + | 
 | 21 | +### 3. View  | 
 | 22 | +- **File**: `app/views/outbound_whatsapp/new.html.erb`  | 
 | 23 | +- Contains forms for both text and interactive WhatsApp messages  | 
 | 24 | + | 
 | 25 | +### 4. Routes  | 
 | 26 | +Added to `config/routes.rb`:  | 
 | 27 | +```ruby  | 
 | 28 | +get  '/outbound_whatsapp/new', to: 'outbound_whatsapp#new', as: :new_outbound_whatsapp  | 
 | 29 | +post '/outbound_whatsapp',     to: 'outbound_whatsapp#create', as: :outbound_whatsapp  | 
 | 30 | +post '/outbound_whatsapp/interactive', to: 'outbound_whatsapp#interactive', as: :interactive_whatsapp  | 
 | 31 | +```  | 
 | 32 | + | 
 | 33 | +### 5. Environment Configuration  | 
 | 34 | +Added `VONAGE_WHATSAPP_NUMBER` to `.env.example`  | 
 | 35 | + | 
 | 36 | +## Setup Instructions  | 
 | 37 | + | 
 | 38 | +### Step 1: Run the Migration  | 
 | 39 | +```bash  | 
 | 40 | +rails db:migrate  | 
 | 41 | +```  | 
 | 42 | + | 
 | 43 | +### Step 2: Configure Your Vonage WhatsApp Application  | 
 | 44 | + | 
 | 45 | +1. Log in to your [Vonage Dashboard](https://dashboard.nexmo.com/)  | 
 | 46 | +2. Create a new application (or use an existing one) and enable the **Messages** capability  | 
 | 47 | +3. Add webhook URLs for:  | 
 | 48 | +   - **Inbound**: `https://your-domain.com/inbound` (or use ngrok for testing)  | 
 | 49 | +   - **Status**: `https://your-domain.com/status`  | 
 | 50 | +4. Click **Generate public and private key**  | 
 | 51 | +5. Move the downloaded `private.key` file to the root of your Rails app  | 
 | 52 | +6. Note your **Application ID**  | 
 | 53 | +7. Click **Save**  | 
 | 54 | +8. Under the **Link external accounts** tab, link your WhatsApp number  | 
 | 55 | + | 
 | 56 | +### Step 3: Update Your .env File  | 
 | 57 | + | 
 | 58 | +Add the following to your `.env` file (create one if it doesn't exist):  | 
 | 59 | + | 
 | 60 | +```bash  | 
 | 61 | +VONAGE_APPLICATION_ID=your_application_id_here  | 
 | 62 | +VONAGE_PRIVATE_KEY=./private.key  | 
 | 63 | +VONAGE_WHATSAPP_NUMBER=14157386102  # Your WhatsApp Business number  | 
 | 64 | +```  | 
 | 65 | + | 
 | 66 | +### Step 4: Test the Integration  | 
 | 67 | + | 
 | 68 | +1. Start your Rails server:  | 
 | 69 | +   ```bash  | 
 | 70 | +   rails s  | 
 | 71 | +   ```  | 
 | 72 | + | 
 | 73 | +2. Navigate to: `http://localhost:3000/outbound_whatsapp/new`  | 
 | 74 | + | 
 | 75 | +3. You should see two forms:  | 
 | 76 | +   - **Send a WhatsApp Message** - For sending text messages  | 
 | 77 | +   - **Try an Interactive Message** - For sending messages with reply buttons  | 
 | 78 | + | 
 | 79 | +## Features  | 
 | 80 | + | 
 | 81 | +### Text Messages  | 
 | 82 | +Send simple text messages to any WhatsApp number. The form includes:  | 
 | 83 | +- **From**: Your WhatsApp Business number (pre-filled from ENV)  | 
 | 84 | +- **To**: Recipient's WhatsApp number (format: +14155551234)  | 
 | 85 | +- **Message**: The text content to send  | 
 | 86 | + | 
 | 87 | +### Interactive Messages  | 
 | 88 | +Send messages with reply buttons that users can tap to respond. The example includes:  | 
 | 89 | +- A header ("Delivery time")  | 
 | 90 | +- Body text with a question  | 
 | 91 | +- Footer text with additional info  | 
 | 92 | +- Three reply buttons with different time slots  | 
 | 93 | + | 
 | 94 | +You can customize the interactive message structure in the `interactive` action of `OutboundWhatsappController`.  | 
 | 95 | + | 
 | 96 | +## How It Works  | 
 | 97 | + | 
 | 98 | +1. **User fills out the form** and submits  | 
 | 99 | +2. **Controller receives the data** and creates a `WhatsappMessage` record  | 
 | 100 | +3. **Vonage client is initialized** with your credentials  | 
 | 101 | +4. **Message is sent** via the Vonage Messages API  | 
 | 102 | +5. **Response is checked** - if successful (HTTP 202), the `message_uuid` is saved  | 
 | 103 | +6. **User is redirected** back to the form with a success notice  | 
 | 104 | + | 
 | 105 | +## Next Steps  | 
 | 106 | + | 
 | 107 | +As mentioned in the tutorial, this implementation covers sending messages. To build a complete WhatsApp integration, you'll want to add:  | 
 | 108 | + | 
 | 109 | +1. **Inbound message handling** - Receive messages from users  | 
 | 110 | +2. **Status webhooks** - Track message delivery and read status  | 
 | 111 | +3. **Interactive response handling** - Process which button the user clicked  | 
 | 112 | + | 
 | 113 | +These features would require additional controllers and webhook endpoints, similar to the existing `inbound_sms` and `sms_message_status` controllers in your app.  | 
 | 114 | + | 
 | 115 | +## Troubleshooting  | 
 | 116 | + | 
 | 117 | +- **Make sure your WhatsApp number is linked** to your Vonage application  | 
 | 118 | +- **Verify your .env file** has the correct credentials  | 
 | 119 | +- **Check that private.key** is in the root directory  | 
 | 120 | +- **Ensure the recipient number** is in E.164 format (e.g., +14155551234)  | 
 | 121 | +- **For testing**, you can use the [Vonage Sandbox for WhatsApp](https://developer.vonage.com/en/messages/concepts/messages-api-sandbox)  | 
 | 122 | + | 
 | 123 | +## Resources  | 
 | 124 | + | 
 | 125 | +- [Vonage Messages API Documentation](https://developer.vonage.com/en/messages/overview)  | 
 | 126 | +- [WhatsApp Business API Guide](https://developer.vonage.com/en/messages/concepts/whatsapp)  | 
 | 127 | +- [Vonage Ruby SDK](https://github.com/Vonage/vonage-ruby-sdk)  | 
0 commit comments