Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountMethodsExample.java
More file actions
Latest commit
58 lines (49 loc) · 2.02 KB
/
Copy pathAccountMethodsExample.java
File metadata and controls
58 lines (49 loc) · 2.02 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
packagecom.greenapi.client.examples;
importcom.greenapi.client.pkg.api.GreenApi;
importlombok.extern.log4j.Log4j2;
importorg.springframework.web.client.RestTemplate;
importjava.util.Objects;
@Log4j2
publicclassAccountMethodsExample {
publicstaticvoidmain(String[] args) {
varrestTemplate = newRestTemplate();
vargreenApi = newGreenApi(
restTemplate,
"https://media.green-api.com",
"https://api.green-api.com",
"YOUR_INSTANCE_ID",
"YOUR_TOKEN");
varexample = newAccountMethodsExample();
example.getStateInstanceHistoryExample(greenApi);
example.updateApiTokenExample(greenApi);
}
/**
* Returns the last 50 authorization state changes of the instance.
*/
privatevoidgetStateInstanceHistoryExample(GreenApigreenApi) {
varresponse = greenApi.account.getStateInstanceHistory(50);
if (response.getStatusCode().is2xxSuccessful()) {
varhistory = Objects.requireNonNull(response.getBody());
log.info("State history records: " + history.size());
history.forEach(record -> log.info(
"State: " + record.getStateInstance() +
" | phone: " + record.getPhoneNumber() +
" | at: " + record.getTimestamp()));
} else {
log.warn("Failed to get state history, status: " + response.getStatusCode());
}
}
/**
* Generates a new API token, invalidating the current one.
* Beta feature. After calling this method the current token becomes invalid.
*/
privatevoidupdateApiTokenExample(GreenApigreenApi) {
varresponse = greenApi.account.updateApiToken();
if (response.getStatusCode().is2xxSuccessful()) {
varnewToken = Objects.requireNonNull(response.getBody()).getApiTokenInstance();
log.info("New API token: " + newToken);
} else {
log.warn("Failed to update token, status: " + response.getStatusCode());
}
}
}