|
| 1 | +# Chatery WhatsApp Integration Setup Guide |
| 2 | + |
| 3 | +This guide explains how to set up and configure the Chatery Cloud API integration for WhatsApp messaging in Cloud Brain. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- A Chatery account with WhatsApp Business API access |
| 8 | +- A verified WhatsApp Business phone number |
| 9 | +- Cloud Brain application deployed and accessible via HTTPS |
| 10 | + |
| 11 | +## Step 1: Chatery Dashboard Setup |
| 12 | + |
| 13 | +### 1.1 Create a WhatsApp Connection |
| 14 | + |
| 15 | +1. Log in to your Chatery dashboard at [https://chatery.com](https://chatery.com) |
| 16 | +2. Navigate to **Connections** > **Add New Connection** |
| 17 | +3. Select **WhatsApp** as the connection type |
| 18 | +4. Choose **Cloud API** as the integration method |
| 19 | + |
| 20 | +### 1.2 Configure Phone Number |
| 21 | + |
| 22 | +1. Select your WhatsApp Business phone number from the list |
| 23 | +2. If not connected, follow Chatery's instructions to connect your phone number |
| 24 | +3. Wait for the connection status to show as **Active** |
| 25 | + |
| 26 | +### 1.3 Get API Credentials |
| 27 | + |
| 28 | +1. Go to **Settings** > **API** in your Chatery dashboard |
| 29 | +2. Generate a new API key if you don't have one |
| 30 | +3. Copy the following credentials: |
| 31 | + - **API URL** (e.g., `https://api.chatery.com/v1`) |
| 32 | + - **API Key** (your secret API key) |
| 33 | + - **Phone Number ID** (your WhatsApp phone number ID) |
| 34 | + |
| 35 | +## Step 2: Environment Configuration |
| 36 | + |
| 37 | +### 2.1 Configure Environment Variables |
| 38 | + |
| 39 | +1. Copy the `.env.example` file to `.env` in your Cloud Brain project: |
| 40 | + ```bash |
| 41 | + cp .env.example .env |
| 42 | + ``` |
| 43 | + |
| 44 | +2. Open `.env` and fill in the Chatery configuration: |
| 45 | + ```env |
| 46 | + # Chatery Configuration (WhatsApp Cloud API) |
| 47 | + CHATERY_API_URL=https://api.chatery.com/v1 |
| 48 | + CHATERY_API_KEY=your_chatery_api_key_here |
| 49 | + CHATERY_PHONE_NUMBER_ID=your_phone_number_id_here |
| 50 | + CHATERY_WEBHOOK_SECRET=your_webhook_secret_here |
| 51 | + ``` |
| 52 | + |
| 53 | +3. **Important**: Leave the WAHA configuration empty if you're only using Chatery: |
| 54 | + ```env |
| 55 | + # WAHA Configuration (leave empty if not using) |
| 56 | + WAHA_API_URL= |
| 57 | + WAHA_API_KEY= |
| 58 | + WAHA_SESSION_NAME= |
| 59 | + ``` |
| 60 | + |
| 61 | +### 2.2 Configure Whitelisted Numbers |
| 62 | + |
| 63 | +Add the phone numbers that are allowed to interact with the bot: |
| 64 | +```env |
| 65 | +WHITELISTED_NUMBERS=1234567890,0987654321 |
| 66 | +``` |
| 67 | + |
| 68 | +**Note**: Use international format without the `+` sign. |
| 69 | + |
| 70 | +## Step 3: Webhook Configuration |
| 71 | + |
| 72 | +### 3.1 Set Webhook URL in Chatery |
| 73 | + |
| 74 | +1. In your Chatery dashboard, go to **Connections** > **Your WhatsApp Connection** > **Webhooks** |
| 75 | +2. Click **Add Webhook** |
| 76 | +3. Enter your Cloud Brain webhook URL: |
| 77 | + ``` |
| 78 | + https://your-domain.com/chatery/webhook |
| 79 | + ``` |
| 80 | +4. Select the following events to subscribe to: |
| 81 | + - **Messages** (incoming messages) |
| 82 | + - **Message Status** (delivery receipts, read receipts) |
| 83 | + |
| 84 | +### 3.2 Configure Webhook Secret |
| 85 | + |
| 86 | +1. Generate a secure random string for your webhook secret (at least 32 characters) |
| 87 | +2. Add it to your `.env` file: |
| 88 | + ```env |
| 89 | + CHATERY_WEBHOOK_SECRET=your_secure_random_string_here |
| 90 | + ``` |
| 91 | +3. Enter the same secret in the Chatery dashboard webhook configuration |
| 92 | + |
| 93 | +### 3.3 Verify Webhook |
| 94 | + |
| 95 | +Chatery will send a verification request to your webhook URL. The application will automatically respond with the challenge token if the verify token matches. |
| 96 | + |
| 97 | +## Step 4: Testing |
| 98 | + |
| 99 | +### 4.1 Verify Application Startup |
| 100 | + |
| 101 | +1. Start your Cloud Brain application: |
| 102 | + ```bash |
| 103 | + python main.py |
| 104 | + ``` |
| 105 | + or with uvicorn: |
| 106 | + ```bash |
| 107 | + uvicorn main:app --host 0.0.0.0 --port 8000 |
| 108 | + ``` |
| 109 | + |
| 110 | +2. Check the logs for: |
| 111 | + ``` |
| 112 | + ChateryService initialized with API URL: https://api.chatery.com/v1 and phone number ID: your_phone_number_id |
| 113 | + ``` |
| 114 | + |
| 115 | +### 4.2 Test Webhook Connection |
| 116 | + |
| 117 | +1. In the Chatery dashboard, check the webhook status |
| 118 | +2. It should show as **Active** or **Connected** |
| 119 | +3. Send a test message from your WhatsApp to trigger the webhook |
| 120 | + |
| 121 | +### 4.3 Test Message Flow |
| 122 | + |
| 123 | +1. Send a message from a whitelisted number to your WhatsApp Business number |
| 124 | +2. Check the application logs for: |
| 125 | + ``` |
| 126 | + Message from <number>: <message_body> |
| 127 | + Identified message type: <type> |
| 128 | + ``` |
| 129 | +3. Verify that you receive a response on WhatsApp |
| 130 | + |
| 131 | +### 4.4 Test Commands |
| 132 | + |
| 133 | +- **Sync Command**: Send a message that triggers SYNC type identification to start database synchronization |
| 134 | +- **Query Command**: Send a question to test the RAG (Retrieval-Augmented Generation) functionality |
| 135 | + |
| 136 | +## Step 5: Switching Between WAHA and Chatery |
| 137 | + |
| 138 | +### Using Chatery Only |
| 139 | + |
| 140 | +```env |
| 141 | +# Chatery Configuration (filled) |
| 142 | +CHATERY_API_URL=https://api.chatery.com/v1 |
| 143 | +CHATERY_API_KEY=your_key |
| 144 | +CHATERY_PHONE_NUMBER_ID=your_id |
| 145 | +CHATERY_WEBHOOK_SECRET=your_secret |
| 146 | +
|
| 147 | +# WAHA Configuration (empty) |
| 148 | +WAHA_API_URL= |
| 149 | +WAHA_API_KEY= |
| 150 | +WAHA_SESSION_NAME= |
| 151 | +``` |
| 152 | + |
| 153 | +### Using WAHA Only |
| 154 | + |
| 155 | +```env |
| 156 | +# Chatery Configuration (empty) |
| 157 | +CHATERY_API_URL= |
| 158 | +CHATERY_API_KEY= |
| 159 | +CHATERY_PHONE_NUMBER_ID= |
| 160 | +CHATERY_WEBHOOK_SECRET= |
| 161 | +
|
| 162 | +# WAHA Configuration (filled) |
| 163 | +WAHA_API_URL=http://localhost:3000 |
| 164 | +WAHA_API_KEY=your_waha_key |
| 165 | +WAHA_SESSION_NAME=your_session |
| 166 | +``` |
| 167 | + |
| 168 | +### Using Both (Different Endpoints) |
| 169 | + |
| 170 | +Both integrations can run simultaneously with different webhook URLs: |
| 171 | +- WAHA: `https://your-domain.com/waha/webhook` |
| 172 | +- Chatery: `https://your-domain.com/chatery/webhook` |
| 173 | + |
| 174 | +Configure different phone numbers for each integration. |
| 175 | + |
| 176 | +## Troubleshooting |
| 177 | + |
| 178 | +### Webhook Not Receiving Messages |
| 179 | + |
| 180 | +1. Verify your webhook URL is accessible from the internet (use HTTPS) |
| 181 | +2. Check that the webhook secret matches in both `.env` and Chatery dashboard |
| 182 | +3. Review application logs for any errors |
| 183 | +4. Test webhook endpoint manually: |
| 184 | + ```bash |
| 185 | + curl -X GET "https://your-domain.com/chatery/webhook?hub.mode=subscribe&hub.verify_token=your_secret&hub.challenge=12345" |
| 186 | + ``` |
| 187 | + |
| 188 | +### Messages Not Being Sent |
| 189 | + |
| 190 | +1. Verify `CHATERY_API_URL` and `CHATERY_API_KEY` are correct |
| 191 | +2. Check that `CHATERY_PHONE_NUMBER_ID` is valid |
| 192 | +3. Review application logs for API error responses |
| 193 | +4. Test API connection manually: |
| 194 | + ```bash |
| 195 | + curl -X POST "https://api.chatery.com/v1/messages" \ |
| 196 | + -H "Authorization: Bearer your_api_key" \ |
| 197 | + -H "Content-Type: application/json" \ |
| 198 | + -d '{"messaging_product":"whatsapp","to":"1234567890","type":"text","text":{"body":"test"}}' |
| 199 | + ``` |
| 200 | + |
| 201 | +### Unauthorized Access Warnings |
| 202 | + |
| 203 | +1. Ensure sender numbers are in the `WHITELISTED_NUMBERS` environment variable |
| 204 | +2. Check that numbers are in international format without `+` |
| 205 | +3. Verify no extra spaces in the comma-separated list |
| 206 | + |
| 207 | +## API Reference |
| 208 | + |
| 209 | +### Endpoints |
| 210 | + |
| 211 | +| Endpoint | Method | Description | |
| 212 | +|----------|--------|-------------| |
| 213 | +| `/chatery/webhook` | GET | Webhook verification | |
| 214 | +| `/chatery/webhook` | POST | Receive incoming messages | |
| 215 | + |
| 216 | +### Webhook Payload Format |
| 217 | + |
| 218 | +Chatery sends messages in the following format: |
| 219 | +```json |
| 220 | +{ |
| 221 | + "entry": [{ |
| 222 | + "changes": [{ |
| 223 | + "value": { |
| 224 | + "messages": [{ |
| 225 | + "from": "1234567890", |
| 226 | + "type": "text", |
| 227 | + "text": { |
| 228 | + "body": "Hello" |
| 229 | + } |
| 230 | + }] |
| 231 | + } |
| 232 | + }] |
| 233 | + }] |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +## Security Best Practices |
| 238 | + |
| 239 | +1. **Keep API keys secret**: Never commit `.env` to version control |
| 240 | +2. **Use HTTPS**: Always use HTTPS for webhook URLs in production |
| 241 | +3. **Verify webhooks**: Always enable webhook signature verification |
| 242 | +4. **Limit whitelisted numbers**: Only allow trusted numbers to interact |
| 243 | +5. **Rotate secrets**: Periodically rotate API keys and webhook secrets |
| 244 | + |
| 245 | +## Support |
| 246 | + |
| 247 | +For Chatery-specific issues, refer to: |
| 248 | +- [Chatery Documentation](https://docs.chatery.com/) |
| 249 | +- [Chatery Support](https://chatery.com/support) |
| 250 | + |
| 251 | +For Cloud Brain integration issues, check the application logs and README.md. |
0 commit comments