-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.java
More file actions
55 lines (46 loc) · 1.66 KB
/
Bot.java
File metadata and controls
55 lines (46 loc) · 1.66 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
package Tutorial;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.concurrent.CompletableFuture;
public class Bot extends TelegramLongPollingBot {
@Override
public String getBotUsername() {
return "" ;
}
@Override
public String getBotToken() {
return "";
}
@Override
public void onUpdateReceived(Update update) {
System.out.println(update.getMessage().getText());
System.out.println(update);
if(update.getMessage().getFrom().getIsBot()){
return;
}
RequestAIResponse AI = new RequestAIResponse();
CompletableFuture<String> responseFuture = AI.que(update.getMessage().getText());
responseFuture.thenAccept(requestId ->{
System.out.print("got id, waiting for response");
CompletableFuture<String>futureResponse = AI.getResponse(requestId);
futureResponse.thenAccept(response ->{
sendMessage(update.getMessage().getChatId(), response);
});
});
System.out.print("received update");
}
public void sendMessage(Long recipient, String msg){
System.out.print("Sending message");
SendMessage sm = SendMessage.builder()
.chatId(recipient.toString())
.text(msg).build();
try{
execute(sm);
}
catch (TelegramApiException e){
throw new RuntimeException(e);
}
}
}