#![allow(dead_code)]
use anyhow::Result;
use reqwest::Client;
use serde::Serialize;
#[derive(Serialize)]
struct WhatsAppMessage {
messaging_product: String,
to: String,
type_: String,
text: WhatsAppText,
}
#[derive(Serialize)]
struct WhatsAppText {
body: String,
}
pub async fn send_message(api_key: &str, from_phone: &str, to_phone: &str, text: &str) -> Result<()> {
let client = Client::new();
let url = format!("https://graph.facebook.com/v18.0/{}/messages", from_phone);
let message = WhatsAppMessage {
messaging_product: "whatsapp".to_string(),
to: to_phone.to_string(),
type_: "text".to_string(),
text: WhatsAppText {
body: text.to_string(),
},
};
let response = client.post(&url)
.header("Authorization", format!("Bearer {}", api_key))
.header("Content-Type", "application/json")
.json(&message)
.send()
.await?;
if response.status().is_success() {
Ok(())
} else {
let status = response.status();
Err(anyhow::anyhow!("Failed to send WhatsApp message: {}", status))
}
}