Skip to content

Commit adc25e2

Browse files
committed
added ip api
1 parent cac7d51 commit adc25e2

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

.idea/amplicode-jpa.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@
6363
<artifactId>jaxb-api</artifactId>
6464
<version>2.3.1</version>
6565
</dependency>
66+
<!-- https://mvnrepository.com/artifact/com.github.plexpt/chatgpt -->
6667
<dependency>
6768
<groupId>com.github.plexpt</groupId>
6869
<artifactId>chatgpt</artifactId>
69-
<version>4.3.0</version>
70+
<version>5.1.0</version>
7071
</dependency>
72+
7173
<dependency>
7274
<groupId>org.junit.jupiter</groupId>
7375
<artifactId>junit-jupiter-engine</artifactId>

src/main/java/org/example/project/service/ChatGPTService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
55
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;
66
import com.plexpt.chatgpt.entity.chat.Message;
7+
import lombok.extern.slf4j.Slf4j;
78
import org.example.project.configuration.ApplicationConfig;
89
import org.springframework.stereotype.Component;
910

@@ -12,6 +13,7 @@
1213
import java.util.ArrayList;
1314
import java.util.Arrays;
1415

16+
@Slf4j
1517
@Component
1618
public class ChatGPTService {
1719
private ChatGPT chatGPT;
@@ -31,6 +33,8 @@ public ChatGPTService(ApplicationConfig config) {
3133
}
3234

3335
public String sendMessage(String prompt, String question) {
36+
log.info("Question: {}", question);
37+
3438
Message system = Message.ofSystem(prompt);
3539
Message message = Message.of(question);
3640
messageHistory.add(system);
@@ -41,12 +45,14 @@ public String sendMessage(String prompt, String question) {
4145

4246
private String sendMessagesToChatGPT(){
4347
ChatCompletion chatCompletion = ChatCompletion.builder()
44-
.model(ChatCompletion.Model.GPT4Turbo.getName()) // GPT4Turbo or GPT_3_5_TURBO
48+
.model(ChatCompletion.Model.GPT4)
4549
.messages(messageHistory)
4650
.maxTokens(3000)
4751
.temperature(0.9)
4852
.build();
4953

54+
log.info("{} tokens: {}", chatCompletion.getModel(), chatCompletion.countTokens());
55+
5056
ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
5157
Message res = response.getChoices().get(0).getMessage();
5258
messageHistory.add(res);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.example.project.service;
2+
3+
import lombok.NoArgsConstructor;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.io.IOException;
7+
import java.net.URI;
8+
import java.net.http.HttpClient;
9+
import java.net.http.HttpRequest;
10+
import java.net.http.HttpResponse;
11+
12+
@Service
13+
@NoArgsConstructor
14+
public class IPService {
15+
public String getIP() throws IOException, InterruptedException {
16+
try {
17+
HttpClient client = HttpClient.newHttpClient();
18+
HttpRequest request = HttpRequest.newBuilder()
19+
.uri(URI.create("https://api.ipify.org?format=json"))
20+
.build();
21+
22+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
23+
24+
System.out.println(response.body());
25+
System.out.println(response.statusCode());
26+
return response.body();
27+
} catch (IOException | InterruptedException e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
}

src/main/java/org/example/project/service/TelegramBot.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.telegram.telegrambots.meta.api.objects.commands.scope.BotCommandScopeDefault;
1515
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
1616

17+
import java.io.IOException;
1718
import java.util.ArrayList;
1819
import java.util.List;
1920

@@ -33,6 +34,9 @@ public class TelegramBot extends TelegramLongPollingBot {
3334
@Autowired
3435
private ChatGPTService chatGPTService;
3536

37+
@Autowired
38+
private IPService ipService;
39+
3640
final ApplicationConfig config;
3741

3842
private DialogMode dialogMode;
@@ -58,6 +62,7 @@ public TelegramBot(ApplicationConfig config) {
5862
listOfCommand.add(new BotCommand("/mynote", "get your notes"));
5963
listOfCommand.add(new BotCommand("/busschedule", "check schedule of buses"));
6064
listOfCommand.add(new BotCommand("/gpt", "get answer from ChatGpt"));
65+
listOfCommand.add(new BotCommand("/ip", "get your current ip"));
6166
try {
6267
this.execute(new SetMyCommands(listOfCommand, new BotCommandScopeDefault(), null));
6368
} catch (TelegramApiException e) {
@@ -95,6 +100,9 @@ public void onUpdateReceived(Update update) {
95100
case "/gpt":
96101
gptReceived(chatId);
97102
break;
103+
case "/ip":
104+
getIP(chatId);
105+
break;
98106
default:
99107
sendMessage(chatId, "Sorry, command was not recognized");
100108
break;
@@ -114,10 +122,7 @@ public void onUpdateReceived(Update update) {
114122
}
115123

116124
private void questionToGptReceived(long chatId, String question) {
117-
String answer = chatGPTService.sendMessage("Напиши максимально подробный ответ(если не требуется обратного)" +
118-
" на поставленный вопрос. В ответах в которых необходимо писать код НЕ пиши комментарии внутри кода, напиши " +
119-
"их отдельно. В случае если ответ получается большим то не обрезай ответ, то есть всегда выдавай целостный " +
120-
"ответ. ВСЕГДА отвечай по русски, даже если вопрос на английском.", question);
125+
String answer = chatGPTService.sendMessage("", question);
121126
sendMessage(chatId, answer);
122127
dialogMode = DialogMode.MAIN;
123128
}
@@ -128,6 +133,20 @@ private void gptReceived(long chatId) {
128133
dialogMode = DialogMode.WAIT_QUESTION;
129134
}
130135

136+
private void getIP(long chatId) {
137+
String answer = "Your current IP: ";
138+
try {
139+
answer += ipService.getIP();
140+
} catch (NullPointerException e) {
141+
answer = e.getMessage();
142+
log.error("Exception: %s".formatted(e.getMessage()));
143+
} catch (IOException | InterruptedException e) {
144+
throw new RuntimeException(e);
145+
}
146+
sendMessage(chatId, answer);
147+
dialogMode = DialogMode.MAIN;
148+
}
149+
131150
private void sendBusSchedule(long chatId, String variant) {
132151
switch (variant) {
133152
case "1":

0 commit comments

Comments
 (0)