Как отправить WhatsApp API с помощью Java

Введение

В этом руководстве мы создадим простые примеры для отправки сообщений через API WhatsApp с использованием Java.

Изначально необходимо убедиться, что okhttp3 библиотека готова Потому что в этой статье мы будем использовать эту библиотеку для отправки сообщений на шлюз Ultramsg.

Первое сообщение WhatsApp API с использованием Java

package com.mycompany.mavenproject1; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * * @author Ultramsg team */ public class newJavaFile { /** * @param args the command line arguments */ public static void main(String args[]) throws IOException { ////////////////////////////////////// OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&body=WhatsApp API on UltraMsg.com works good&priority=10&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/chat") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute(); ////////////////////////////////////// System.out.println("==================="); System.out.println(response.body().string()); } }
package com.mycompany.mavenproject1; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * * @author Ultramsg team */ public class newJavaFile { /** * @param args the command line arguments */ public static void main(String args[]) throws IOException { ////////////////////////////////////// OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&body=WhatsApp API on UltraMsg.com works good&priority=10&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/chat") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute(); ////////////////////////////////////// System.out.println("==================="); System.out.println(response.body().string()); } }

При выполнении предыдущего кода и успешной отправке сообщения ответ будет таким:

{"sent":"true","message":"ok","id":43007}
{"sent":"true","message":"ok","id":43007}

Отправить изображение

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg&caption=image Caption&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/image") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg&caption=image Caption&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/image") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить документ

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&filename=hello.pdf&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/document") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&filename=hello.pdf&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/document") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить аудио

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/audio") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/audio") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить голос

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/voice") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/voice") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить видео

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/video") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/video") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&link=https://en.wikipedia.org/wiki/COVID-19&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/link") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&link=https://en.wikipedia.org/wiki/COVID-19&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/link") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить контакт

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&contact=14000000001@c.us&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/contact") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&contact=14000000001@c.us&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/contact") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить местоположение

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&address=ABC company \n Sixth floor , office 38&lat=25.197197&lng=55.2721877&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/location") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&address=ABC company \n Sixth floor , office 38&lat=25.197197&lng=55.2721877&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/location") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

Отправить открытку

OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&vcard= BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/vcard") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();
OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "token=JATafg4422g0K54&to=14155552671&vcard= BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD&referenceId="); Request request = new Request.Builder() .url("https://api.ultramsg.com/instance19/messages/vcard") .post(body) .addHeader("content-type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute();

наконец, вы можете ознакомиться с полной документацией по Whatsapp API и часто задаваемыми вопросами.