Skip to content

How to create new group and send message#

Import#

Maven

<dependency>
  <groupId>com.green-api</groupId>
  <artifactId>whatsapp-api-client-java</artifactId>
  <version>version</version>
</dependency>

Gradle

implementation group: 'com.green-api', name: 'whatsapp-api-client-java', version: 'version'

Examples#

How to initialize an object#

You can configure your bean, use application.yml, or instantiate an object via the constructor.

Via configuration:

@Configuration
public class GreenApiConf {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplateBuilder().build();
    }

    @Bean
    public GreenApi greenApi(RestTemplate restTemplate) {
        return new GreenApi(
            restTemplate,
            "https://media.greenapi.com",
            "https://api.greenapi.com",
            "{{YOUR-ID}}",
            "{{YOUR-TOKEN}}");
    }
}

Via application.yml:

To use a ready-made bean that is created based on application.yml parameters, specify the parameters of your instance in the application.yml file as follows:

green-api:
   host: https://api.green-api.com
   hostMedia: https://media.green-api.com
   instanceId: {{yourInstance}}
   token: {{yourToken}}

Make sure you have a RestTemplate bean with your configuration, like this:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){
    return restTemplateBuilder.build();
    }

And add "com.greenapi.client" to the base scanning packages using the @ComponentScan annotation:

@SpringBootApplication
@ComponentScan(basePackages = {"com.greenapi.client", "com.example"})
public class Application {

    public static void main(String[] args) {
        var context = SpringApplication.run(Application.class, args);
    }
}

Via constructor:

var restTemplate = new RestTemplateBuilder().build();

var greenApi1 = new GreenApi(
    restTemplate,
    {{MediaUrl}},
    "https://api.green-api.com",
    {{instanceId1}},
    {{instanceToken1}});

var greenApi2 = new GreenApi(
    restTemplate,
    "https://media.greenapi.com",
    "https://api.greenapi.com",
    {{instanceId2}},
    {{instanceToken2}});

How to create a group and send message#

Link to example: CreateGroupSendMessageExample.java.

@Log4j2
class CreateGroupSendMessageExample {

    private void createGroupAndSendMessage(GreenApi greenApi) {
        var groupMembers = new ArrayList<String>();
        groupMembers.add("11001234567@c.us");
        groupMembers.add("11001234566@c.us");
        groupMembers.add("11001234565@c.us");

        var group = greenApi.groups.createGroup(
            CreateGroupReq.builder()
                .groupName("Test Group")
                .chatIds(groupMembers)
                .build()).getBody();

        if (group != null) {
            var message = greenApi.sending.sendMessage(
                OutgoingMessage.builder()
                    .chatId(group.getChatId())
                    .message("hola a todos")
                    .build()).getBody();

            if (message != null) {
                log.info("Create group: " + group.isCreated() +
                    "\nSend message: " + message.getIdMessage());
            }
        }
    }
}

Example's list#

Description Link to example
How to send message SendMessageExample.java
How to create a group and send message CreateGroupSendMessageExample.java
How to send a file by uploading from the disk SendFileByUploadExample.java
How to send a file by URL SendFileByUrlExample.java
How to send a file by UploadFile + SendFileByUrl UploadFileAndSendByUrlExample.java
How to send poll SendPollExample.java
How to receive incoming notifications WebhookExample.java