package com.vetti.common.ai; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.google.gson.Gson; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * 发送文本消息到指定的WhatsApp */ public class WapiAiClient { private static final String BASE_URL = "https://wapi.ai/api"; private final String apiKey; private final CloseableHttpClient httpClient; private final Gson gson; public WapiAiClient(String apiKey) { this.apiKey = apiKey; this.httpClient = HttpClients.createDefault(); this.gson = new Gson(); } /** * 发送文本消息到指定的WhatsApp号码 * @param phoneNumber 目标电话号码(带国家代码,如:+1234567890) * @param message 要发送的消息内容 * @return API响应 * @throws IOException 网络请求异常 */ public WapiResponse sendTextMessage(String phoneNumber, String message) throws IOException { HttpPost httpPost = new HttpPost(BASE_URL + "/sendMessage"); httpPost.setHeader("Authorization", "Bearer " + apiKey); httpPost.setHeader("Content-Type", "application/json"); Map payload = new HashMap<>(); payload.put("phone", phoneNumber); payload.put("body", message); StringEntity entity = new StringEntity(gson.toJson(payload), ContentType.APPLICATION_JSON); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { HttpEntity responseEntity = response.getEntity(); String responseBody = EntityUtils.toString(responseEntity); return gson.fromJson(responseBody, WapiResponse.class); } } /** * 获取最近的消息 * @param limit 消息数量限制 * @return 消息列表 * @throws IOException 网络请求异常 */ public WapiMessagesResponse getRecentMessages(int limit) throws IOException { HttpGet httpGet = new HttpGet(BASE_URL + "/messages?limit=" + limit); httpGet.setHeader("Authorization", "Bearer " + apiKey); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { HttpEntity responseEntity = response.getEntity(); String responseBody = EntityUtils.toString(responseEntity); return gson.fromJson(responseBody, WapiMessagesResponse.class); } } // 关闭HTTP客户端 public void close() throws IOException { httpClient.close(); } // 响应模型类 public static class WapiResponse { private boolean success; private String message; private Object data; // getter和setter方法 public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } } // 消息响应模型类 public static class WapiMessagesResponse { private boolean success; private Message[] messages; // getter和setter方法 public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public Message[] getMessages() { return messages; } public void setMessages(Message[] messages) { this.messages = messages; } public static class Message { private String id; private String phone; private String body; private String type; private String timestamp; // getter和setter方法 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } } } // 使用示例 public static void main(String[] args) { String apiKey = "your_wapi_ai_api_key"; WapiAiClient client = new WapiAiClient(apiKey); try { // 发送消息 WapiResponse response = client.sendTextMessage("+1234567890", "Hello from Wapi.ai Java Client!"); System.out.println("Message sent: " + response.isSuccess()); // 获取最近消息 WapiMessagesResponse messagesResponse = client.getRecentMessages(5); if (messagesResponse.isSuccess() && messagesResponse.getMessages() != null) { System.out.println("Recent messages:"); for (WapiMessagesResponse.Message msg : messagesResponse.getMessages()) { System.out.println(msg.getPhone() + ": " + msg.getBody()); } } } catch (IOException e) { e.printStackTrace(); } finally { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } }