127 lines
4.5 KiB
Java
127 lines
4.5 KiB
Java
package it.acxent.api.wa;
|
|
|
|
import it.acxent.api.ApiClientResult;
|
|
import it.acxent.api._AblApiClient;
|
|
import it.acxent.cc.Attivita;
|
|
import it.acxent.db.ApplParm;
|
|
import it.acxent.db.ApplParmFull;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
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 org.json.JSONObject;
|
|
|
|
public class WhatsappApi extends _AblApiClient {
|
|
public static final String ENDPOINT_PRODUCTION = "https://graph.facebook.com/";
|
|
|
|
public static final String ENDPOINT_SANDBOX = "https://graph.facebook.com/";
|
|
|
|
public static final String API_VERSION = "v17.0/";
|
|
|
|
private String phoneNumberId;
|
|
|
|
private Attivita attivita;
|
|
|
|
private boolean debug = false;
|
|
|
|
private static final String URI_MESSAGES = "/messages";
|
|
|
|
public WhatsappApi(ApplParmFull apFull, String token, String phoneNumberId) {
|
|
super(apFull);
|
|
setToken(token);
|
|
setPhoneNumberId(phoneNumberId);
|
|
setUseSandbox(false);
|
|
}
|
|
|
|
public WhatsappApi() {}
|
|
|
|
public static void main(String[] args) {
|
|
String hostname = "localhost";
|
|
String db = "misericordia";
|
|
ApplParmFull ap = new ApplParmFull(new ApplParm(17, "//" + hostname + ":3308/" + db, db, "root", "root", 1, 10, 300));
|
|
String token = "EAA2Yp61AWZB8BOxl8nthvvLO0okR9BJXA1NNkHox9BbZCMjE0LHGXybachAz78mGCz0RvoSqox3AF7hquq71kt6JL30ETSLdFUZBmcIKxSPT9jwoGHBL6x8X7rCpGdIqVc5FFjXpaUXZALKpf8IrUIvqHgRwyH9eZC6IhmI5U0LPraLJwZA43D6ZCcUer2ZAZAZAzAIQfWNRwmIn0tBNmoElvcr5aadiwR";
|
|
String phoneNumberId = "119770104557287";
|
|
WhatsappApi api = new WhatsappApi(ap, token, phoneNumberId);
|
|
ApiClientResult res = api._postSendTextMsg("ciao testina di vitello");
|
|
System.out.println(res.getMsg());
|
|
}
|
|
|
|
public Attivita getAttivita() {
|
|
if (this.attivita == null)
|
|
this.attivita = Attivita.getDefaultInstance(getApFull());
|
|
return this.attivita;
|
|
}
|
|
|
|
public String getEndpointProduction() {
|
|
return "https://graph.facebook.com/";
|
|
}
|
|
|
|
public String getEndpointSandbox() {
|
|
return "https://graph.facebook.com/";
|
|
}
|
|
|
|
public ApiClientResult _postSendTextMsg(String l_msg) {
|
|
ApiClientResult resER = new ApiClientResult();
|
|
try {
|
|
if (this.debug)
|
|
System.out.println("WhatsappApi --> _postSendTextMsg");
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
HttpPost request = new HttpPost(getEndpoint() + "v17.0//messages");
|
|
request.setHeader("Authorization", getTokenBearer());
|
|
request.setHeader("Accept", "application/json");
|
|
request.setHeader("Content-Type", "application/json");
|
|
request.setHeader("Content-Language", "it-IT");
|
|
JSONObject joDottori = null;
|
|
StringEntity stringEntity = new StringEntity(joDottori.toString(4), "UTF-8");
|
|
request.setEntity((HttpEntity)stringEntity);
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
if (this.debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
JSONObject jo = new JSONObject(content);
|
|
resER.setMsg(jo.toString(2));
|
|
resER.setResult(jo);
|
|
} else {
|
|
JSONObject jo = new JSONObject(content);
|
|
if (this.debug)
|
|
System.out.println(jo.toString(4));
|
|
if (jo.has("success")) {
|
|
if (jo.getBoolean("success") == true) {
|
|
resER.setMsg(jo.getString("msg"));
|
|
resER.setOk(true);
|
|
resER.setResult(Long.valueOf(jo.getLong("id_rigaBolla")));
|
|
} else {
|
|
resER.setMsg(jo.getString("msg"));
|
|
resER.setOk(false);
|
|
}
|
|
} else {
|
|
resER.setMsg("Errore! Risposta non corretta!");
|
|
resER.setOk(false);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
public String getPhoneNumberId() {
|
|
return this.phoneNumberId;
|
|
}
|
|
|
|
public void setPhoneNumberId(String phoneNumberId) {
|
|
this.phoneNumberId = phoneNumberId;
|
|
}
|
|
}
|