Java
Chilkat2 Java
WhatsApp Cloud API Send Message
See more WhatsApp Examples
Demonstrates how to send a message using the WhatsApp Cloud API (Business App)Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// The following JSON is sent in the request body.
// {
// "messaging_product": "whatsapp",
// "to": "16302581871",
// "type": "template",
// "template": {
// "name": "hello_world",
// "language": {
// "code": "en_US"
// }
// }
// }
JsonObject json = new JsonObject();
json.updateString("messaging_product", "whatsapp");
json.updateString("to", "15555555555");
json.updateString("type", "template");
json.updateString("template.name", "hello_world");
json.updateString("template.language.code", "en_US");
http.setAuthToken("EAANrS5....yFB9Ma");
http.setRequestHeader("Content-Type", "application/json");
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", "https://graph.facebook.com/v18.0/111111111111111/messages", json, "application/json", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(resp.getStatusCode());
System.out.println(resp.getBodyStr());
// Here's a screenshot of our WhatsApp test app.
//
}
}