-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateGroupSettingsExample.java
More file actions
68 lines (59 loc) · 2.41 KB
/
Copy pathUpdateGroupSettingsExample.java
File metadata and controls
68 lines (59 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.greenapi.client.examples;
import com.greenapi.client.pkg.api.GreenApi;
import com.greenapi.client.pkg.models.request.UpdateGroupSettingsReq;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.client.RestTemplate;
import java.util.Objects;
@Log4j2
public class UpdateGroupSettingsExample {
public static void main(String[] args) {
var restTemplate = new RestTemplate();
var greenApi = new GreenApi(
restTemplate,
"https://media.green-api.com",
"https://api.green-api.com",
"YOUR_INSTANCE_ID",
"YOUR_TOKEN");
var example = new UpdateGroupSettingsExample();
example.lockGroupExample(greenApi);
example.unlockGroupExample(greenApi);
}
/**
* Restricts the group so that only admins can send messages and change settings.
*/
private void lockGroupExample(GreenApi greenApi) {
var response = greenApi.groups.updateGroupSettings(
UpdateGroupSettingsReq.builder()
.groupId("79001234567-1234567890@g.us")
.allowParticipantsSendMessages(false)
.allowParticipantsEditGroupSettings(false)
.build());
if (response.getStatusCode().is2xxSuccessful()) {
var body = Objects.requireNonNull(response.getBody());
if (Boolean.TRUE.equals(body.getUpdateGroupSettings())) {
log.info("Group locked: only admins can write");
} else {
log.warn("Settings not changed: " + body.getReason());
}
} else {
log.warn("Request failed, status: " + response.getStatusCode());
}
}
/**
* Allows all participants to send messages and edit group settings.
*/
private void unlockGroupExample(GreenApi greenApi) {
var response = greenApi.groups.updateGroupSettings(
UpdateGroupSettingsReq.builder()
.groupId("79001234567-1234567890@g.us")
.allowParticipantsSendMessages(true)
.allowParticipantsEditGroupSettings(true)
.build());
if (response.getStatusCode().is2xxSuccessful()) {
var body = Objects.requireNonNull(response.getBody());
log.info("Group unlocked: " + body.getUpdateGroupSettings());
} else {
log.warn("Request failed, status: " + response.getStatusCode());
}
}
}