punch-channels 1.2.0

Channel adapters for messaging platforms in the Punch Agent Combat System
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! WhatsApp Business API channel adapter (webhook-based).
//!
//! Receives messages via WhatsApp Cloud API webhooks and sends responses
//! back via the WhatsApp Cloud API messages endpoint.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use tokio::sync::RwLock;
use tracing::{info, warn};

use punch_types::{PunchError, PunchResult};

use crate::{ChannelAdapter, ChannelPlatform, ChannelStatus, IncomingMessage};

const WHATSAPP_API_BASE: &str = "https://graph.facebook.com/v21.0";

/// WhatsApp Business Cloud API adapter.
///
/// Receives: WhatsApp webhook payloads via POST to the Arena webhook endpoint.
/// Sends: responses via the WhatsApp Cloud API messages endpoint.
pub struct WhatsAppAdapter {
    /// API access token for the WhatsApp Business Cloud API.
    api_token: String,
    /// Phone number ID for sending messages.
    phone_number_id: String,
    /// Webhook verify token for incoming webhook validation.
    #[allow(dead_code)]
    webhook_verify_token: String,
    /// HTTP client for API calls.
    client: reqwest::Client,
    /// Whether the adapter is currently running.
    running: AtomicBool,
    /// When the adapter was started.
    started_at: RwLock<Option<DateTime<Utc>>>,
    /// Message counters.
    messages_received: AtomicU64,
    messages_sent: AtomicU64,
}

impl WhatsAppAdapter {
    /// Create a new WhatsApp adapter.
    ///
    /// `api_token`: WhatsApp Cloud API access token.
    /// `phone_number_id`: The phone number ID for sending messages.
    /// `webhook_verify_token`: Token used to verify incoming webhook subscriptions.
    pub fn new(api_token: String, phone_number_id: String, webhook_verify_token: String) -> Self {
        Self {
            api_token,
            phone_number_id,
            webhook_verify_token,
            client: reqwest::Client::new(),
            running: AtomicBool::new(false),
            started_at: RwLock::new(None),
            messages_received: AtomicU64::new(0),
            messages_sent: AtomicU64::new(0),
        }
    }

    /// Parse a WhatsApp Cloud API webhook payload into an `IncomingMessage`.
    ///
    /// Expected JSON format:
    /// ```json
    /// {
    ///   "object": "whatsapp_business_account",
    ///   "entry": [{
    ///     "id": "BIZ_ACCOUNT_ID",
    ///     "changes": [{
    ///       "value": {
    ///         "messaging_product": "whatsapp",
    ///         "metadata": { "phone_number_id": "123", "display_phone_number": "+1..." },
    ///         "contacts": [{ "profile": { "name": "Alice" }, "wa_id": "15551234567" }],
    ///         "messages": [{
    ///           "from": "15551234567",
    ///           "id": "wamid.abc123",
    ///           "timestamp": "1700000000",
    ///           "type": "text",
    ///           "text": { "body": "Hello!" }
    ///         }]
    ///       },
    ///       "field": "messages"
    ///     }]
    ///   }]
    /// }
    /// ```
    pub fn parse_webhook_payload(&self, payload: &serde_json::Value) -> Option<IncomingMessage> {
        let entry = payload.get("entry")?.as_array()?.first()?;
        let changes = entry.get("changes")?.as_array()?.first()?;
        let value = changes.get("value")?;

        // Only process message webhooks
        let field = changes["field"].as_str()?;
        if field != "messages" {
            return None;
        }

        let messages = value.get("messages")?.as_array()?;
        let message = messages.first()?;

        // Only handle text messages
        let msg_type = message["type"].as_str()?;
        if msg_type != "text" {
            return None;
        }

        let from = message["from"].as_str()?;
        let message_id = message["id"].as_str().unwrap_or("unknown");
        let text = message["text"]["body"].as_str()?;
        if text.is_empty() {
            return None;
        }

        // Extract contact display name
        let display_name = value
            .get("contacts")
            .and_then(|c| c.as_array())
            .and_then(|arr| arr.first())
            .and_then(|c| c["profile"]["name"].as_str())
            .unwrap_or(from);

        let timestamp = message["timestamp"]
            .as_str()
            .and_then(|ts| ts.parse::<i64>().ok())
            .and_then(|epoch| DateTime::from_timestamp(epoch, 0))
            .unwrap_or_else(Utc::now);

        self.messages_received.fetch_add(1, Ordering::Relaxed);

        Some(IncomingMessage {
            channel_id: from.to_string(),
            user_id: from.to_string(),
            display_name: display_name.to_string(),
            text: text.to_string(),
            timestamp,
            platform: ChannelPlatform::WhatsApp,
            platform_message_id: message_id.to_string(),
            is_group: false,
            metadata: HashMap::new(),
        })
    }

    /// Send a text message via the WhatsApp Cloud API.
    async fn api_send_message(&self, recipient_phone: &str, text: &str) -> PunchResult<()> {
        let url = format!("{}/{}/messages", WHATSAPP_API_BASE, self.phone_number_id);

        let body = serde_json::json!({
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": recipient_phone,
            "type": "text",
            "text": {
                "preview_url": false,
                "body": text
            }
        });

        let resp = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.api_token))
            .header("Content-Type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| PunchError::Channel {
                channel: "whatsapp".to_string(),
                message: format!("failed to send message: {e}"),
            })?;

        let status = resp.status();
        if !status.is_success() {
            let body_text = resp.text().await.unwrap_or_default();
            warn!("WhatsApp send message failed ({status}): {body_text}");
        }

        self.messages_sent.fetch_add(1, Ordering::Relaxed);
        Ok(())
    }
}

#[async_trait]
impl ChannelAdapter for WhatsAppAdapter {
    fn name(&self) -> &str {
        "whatsapp"
    }

    fn platform(&self) -> ChannelPlatform {
        ChannelPlatform::WhatsApp
    }

    async fn start(&self) -> PunchResult<()> {
        self.running.store(true, Ordering::Relaxed);
        *self.started_at.write().await = Some(Utc::now());
        info!("WhatsApp adapter started (webhook mode)");
        Ok(())
    }

    async fn stop(&self) -> PunchResult<()> {
        self.running.store(false, Ordering::Relaxed);
        info!("WhatsApp adapter stopped");
        Ok(())
    }

    async fn send_response(&self, channel_id: &str, message: &str) -> PunchResult<()> {
        self.api_send_message(channel_id, message).await
    }

    fn status(&self) -> ChannelStatus {
        ChannelStatus {
            connected: self.running.load(Ordering::Relaxed),
            started_at: self.started_at.try_read().ok().and_then(|g| *g),
            messages_received: self.messages_received.load(Ordering::Relaxed),
            messages_sent: self.messages_sent.load(Ordering::Relaxed),
            last_error: None,
        }
    }

    async fn validate_credentials(&self) -> PunchResult<()> {
        let url = format!("{}/{}", WHATSAPP_API_BASE, self.phone_number_id);
        let resp = self
            .client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.api_token))
            .send()
            .await
            .map_err(|e| PunchError::Channel {
                channel: "whatsapp".to_string(),
                message: format!("credential validation failed: {}", e),
            })?;
        if !resp.status().is_success() {
            return Err(PunchError::Channel {
                channel: "whatsapp".to_string(),
                message: "invalid credentials".to_string(),
            });
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_adapter() -> WhatsAppAdapter {
        WhatsAppAdapter::new(
            "test-token".to_string(),
            "123456789".to_string(),
            "verify-token".to_string(),
        )
    }

    #[test]
    fn test_whatsapp_adapter_creation() {
        let adapter = make_adapter();
        assert_eq!(adapter.name(), "whatsapp");
        assert_eq!(adapter.platform(), ChannelPlatform::WhatsApp);
    }

    #[test]
    fn test_parse_whatsapp_webhook_basic() {
        let adapter = make_adapter();

        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": [{
                "id": "BIZ_123",
                "changes": [{
                    "value": {
                        "messaging_product": "whatsapp",
                        "metadata": {
                            "phone_number_id": "123456789",
                            "display_phone_number": "+15551234567"
                        },
                        "contacts": [{
                            "profile": { "name": "Alice" },
                            "wa_id": "15559876543"
                        }],
                        "messages": [{
                            "from": "15559876543",
                            "id": "wamid.abc123",
                            "timestamp": "1700000000",
                            "type": "text",
                            "text": { "body": "Hello from WhatsApp!" }
                        }]
                    },
                    "field": "messages"
                }]
            }]
        });

        let msg = adapter.parse_webhook_payload(&payload).unwrap();
        assert_eq!(msg.platform, ChannelPlatform::WhatsApp);
        assert_eq!(msg.user_id, "15559876543");
        assert_eq!(msg.display_name, "Alice");
        assert_eq!(msg.text, "Hello from WhatsApp!");
        assert_eq!(msg.platform_message_id, "wamid.abc123");
        assert!(!msg.is_group);
    }

    #[test]
    fn test_parse_whatsapp_webhook_non_text() {
        let adapter = make_adapter();

        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": [{
                "id": "BIZ_123",
                "changes": [{
                    "value": {
                        "messaging_product": "whatsapp",
                        "contacts": [{ "profile": { "name": "Bob" }, "wa_id": "15551111111" }],
                        "messages": [{
                            "from": "15551111111",
                            "id": "wamid.xyz",
                            "timestamp": "1700000000",
                            "type": "image",
                            "image": { "id": "img123" }
                        }]
                    },
                    "field": "messages"
                }]
            }]
        });

        let msg = adapter.parse_webhook_payload(&payload);
        assert!(msg.is_none());
    }

    #[tokio::test]
    async fn test_whatsapp_adapter_start_stop() {
        let adapter = make_adapter();

        assert!(!adapter.status().connected);

        adapter.start().await.unwrap();
        let status = adapter.status();
        assert!(status.connected);
        assert!(status.started_at.is_some());

        adapter.stop().await.unwrap();
        assert!(!adapter.status().connected);
    }

    #[test]
    fn test_parse_whatsapp_empty_text() {
        let adapter = make_adapter();
        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": [{"id": "B", "changes": [{"value": {
                "contacts": [{"profile": {"name": "A"}, "wa_id": "1"}],
                "messages": [{"from": "1", "id": "w1", "timestamp": "1700000000",
                    "type": "text", "text": {"body": ""}}]
            }, "field": "messages"}]}]
        });
        assert!(adapter.parse_webhook_payload(&payload).is_none());
    }

    #[test]
    fn test_parse_whatsapp_no_contacts() {
        let adapter = make_adapter();
        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": [{"id": "B", "changes": [{"value": {
                "messages": [{"from": "1234", "id": "w1", "timestamp": "1700000000",
                    "type": "text", "text": {"body": "Hi"}}]
            }, "field": "messages"}]}]
        });
        let msg = adapter.parse_webhook_payload(&payload).unwrap();
        // display_name falls back to phone number
        assert_eq!(msg.display_name, "1234");
    }

    #[test]
    fn test_parse_whatsapp_status_field_ignored() {
        let adapter = make_adapter();
        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": [{"id": "B", "changes": [{"value": {
                "statuses": [{"id": "s1"}]
            }, "field": "statuses"}]}]
        });
        assert!(adapter.parse_webhook_payload(&payload).is_none());
    }

    #[test]
    fn test_parse_whatsapp_video_type_ignored() {
        let adapter = make_adapter();
        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": [{"id": "B", "changes": [{"value": {
                "contacts": [{"profile": {"name": "A"}, "wa_id": "1"}],
                "messages": [{"from": "1", "id": "w1", "timestamp": "1700000000",
                    "type": "video", "video": {"id": "v1"}}]
            }, "field": "messages"}]}]
        });
        assert!(adapter.parse_webhook_payload(&payload).is_none());
    }

    #[test]
    fn test_parse_whatsapp_empty_entry() {
        let adapter = make_adapter();
        let payload = serde_json::json!({
            "object": "whatsapp_business_account",
            "entry": []
        });
        assert!(adapter.parse_webhook_payload(&payload).is_none());
    }
}