Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Update v1.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
natanimn committed Jul 9, 2024
1 parent ace9443 commit 8362199
Show file tree
Hide file tree
Showing 278 changed files with 3,108 additions and 1,090 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class MyFirstEchoBot {
context.reply(message.text).exec();
});

bot.start(); // finally run the bot
bot.run(); // finally run the bot
}
}
```
Expand Down Expand Up @@ -138,7 +138,7 @@ public class MyFirstEchoBot {
context.reply(message.text).exec();
});

bot.start();
bot.run();
}
}
```
Expand All @@ -159,7 +159,7 @@ context.sendMessage("*Hello, World*")
.parseMode(ParseMode.MARKDOWN)
.exec();
```
Lastly we start our bot by using `start()` which does not take any parameter and run our bot via **long polling.**
Lastly we start our bot by using `run()` which does not take any parameter and run our bot via **long polling.**

**IMPORTANT: All handlers are handled in the order in which they were registered.**

Expand Down Expand Up @@ -558,7 +558,7 @@ class MyWebhookBot {
bot.setWebhook(webhook); // set webhook

//...
bot.start(); // start our bot on webhook
bot.run(); // start our bot on webhook

}
}
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/et/telebof/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
package et.telebof;

import et.telebof.types.JsonSerializable;
import java.util.Objects;

import java.util.Map;

public class ApiResponse extends JsonSerializable {
public class ApiResponse<T> {
public boolean ok;
public String description;
public int error_code;
public Object result;
public T result;
public ResponseParameters parameters;

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
ApiResponse<?> that = (ApiResponse<?>) object;
return ok == that.ok && error_code == that.error_code &&
Objects.equals(description, that.description) &&
Objects.equals(result, that.result) &&
Objects.equals(parameters, that.parameters);
}

@Override
public int hashCode() {
return Objects.hash(ok, description, error_code, result, parameters);
}

@Override
public String toString() {
return "ApiResponse{" +
"ok=" + ok +
", description='" + description + '\'' +
", error_code=" + error_code +
", result=" + result +
", parameters=" + parameters +
'}';
}
}

9 changes: 4 additions & 5 deletions src/main/java/et/telebof/BotClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,8 @@ private void processWebhook(){
server.createContext(path, httpExchange -> {
if (httpExchange.getRequestMethod().equals("POST")){
String response = getString(httpExchange);
ApiResponse apiResponse = Util.parseApiResponse(response);
List<Object> objects = Util.parse(apiResponse.result, List.class);
List<Update> updates = Util.parseList(objects, Update.class);
ApiResponse<List<Update>> apiResponse = Util.parseApiResponse(response, Update.class);
List<Update> updates = Util.parseUpdates(apiResponse.result);
httpExchange.sendResponseHeaders(200, response.length());
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write("!".getBytes());
Expand Down Expand Up @@ -804,15 +803,15 @@ private void sleep(int seconds){
}
}

public void start(){
public void run(){
if (webhook != null){
BotLog.info("Bot started running via webhook");
deleteWebhook();
setWebhook(webhook);
processWebhook();
} else {
BotLog.info("Bot started running via longPolling");
deleteWebhook();
// deleteWebhook();
startPolling();
}
}
Expand Down
39 changes: 18 additions & 21 deletions src/main/java/et/telebof/BotContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,28 +329,28 @@ public <T extends InputMedia> SendMediaGroup replyMediaGroup(T[] medias) {
.parseMode(parseMode);
}

public SendLocation sendLocation(Object chatId, float latitude, float longitude) {
public SendLocation sendLocation(Object chatId, double latitude, double longitude) {
return new SendLocation(chatId, latitude, longitude, this.requestSender);
}

public SendLocation sendLocation(float latitude, float longitude) {
public SendLocation sendLocation(double latitude, double longitude) {
return new SendLocation(getChatId(), latitude, longitude, this.requestSender);
}

public SendLocation replyLocation(float latitude, float longitude) {
public SendLocation replyLocation(double latitude, double longitude) {
return new SendLocation(getChatId(), latitude, longitude, this.requestSender)
.replyParameters(new ReplyParameters(getMessageId()).allowSendingWithoutReply(true));
}

public EditMessageLiveLocation editMessageLiveLocation(Object chatId, float latitude, float longitude, int messageId) {
public EditMessageLiveLocation editMessageLiveLocation(Object chatId, double latitude, double longitude, int messageId) {
return new EditMessageLiveLocation(chatId, latitude, longitude, messageId, this.requestSender);
}

public EditMessageLiveLocation editMessageLiveLocation(float latitude, float longitude, int messageId) {
public EditMessageLiveLocation editMessageLiveLocation(double latitude, double longitude, int messageId) {
return new EditMessageLiveLocation(getChatId(), latitude, longitude, messageId, this.requestSender);
}

public EditMessageLiveLocation editMessageLiveLocation(String inlineMessageId, float latitude, float longitude) {
public EditMessageLiveLocation editMessageLiveLocation(String inlineMessageId, double latitude, double longitude) {
return new EditMessageLiveLocation(inlineMessageId, latitude, longitude, this.requestSender);
}

Expand All @@ -366,15 +366,15 @@ public StopMessageLiveLocation stopMessageLiveLocation(String inlineMessageId) {
return new StopMessageLiveLocation(inlineMessageId, this.requestSender);
}

public SendVenue sendVenue(Object chatId, float latitude, float longitude, String title, String address) {
public SendVenue sendVenue(Object chatId, double latitude, double longitude, String title, String address) {
return new SendVenue(chatId, latitude, longitude, title, address, this.requestSender);
}

public SendVenue sendVenue(float latitude, float longitude, String title, String address) {
public SendVenue sendVenue(double latitude, double longitude, String title, String address) {
return new SendVenue(getChatId(), latitude, longitude, title, address, this.requestSender);
}

public SendVenue replyVenue(float latitude, float longitude, String title, String address) {
public SendVenue replyVenue(double latitude, double longitude, String title, String address) {
return new SendVenue(getChatId(), latitude, longitude, title, address, this.requestSender)
.replyParameters(new ReplyParameters(getMessageId()).allowSendingWithoutReply(true));
}
Expand Down Expand Up @@ -961,31 +961,28 @@ public AnswerWebAppQuery answerWebAppQuery(String webAppQueryId, InlineQueryResu

public SendInvoice sendInvoice(Object chatId, String title, String description, String payload, String providerToken,
String currency, LabeledPrice[] prices) {
return new SendInvoice(chatId, title, description, payload, providerToken, currency, prices, this.requestSender)
return new SendInvoice(chatId, title, description, payload, currency, prices, this.requestSender)
.parseMode(parseMode);
}

public SendInvoice sendInvoice(String title, String description, String payload, String providerToken,
public SendInvoice sendInvoice(String title, String description, String payload,
String currency, LabeledPrice[] prices) {
return new SendInvoice(getChatId(), title, description, payload, providerToken, currency, prices, this.requestSender)
return new SendInvoice(getChatId(), title, description, payload, currency, prices, this.requestSender)
.parseMode(parseMode);
}

public SendInvoice replyInvoice(String title, String description, String payload, String providerToken,
String currency, LabeledPrice[] prices) {
return new SendInvoice(getChatId(), title, description, payload, providerToken, currency, prices, this.requestSender)
public SendInvoice replyInvoice(String title, String description, String payload, String currency, LabeledPrice[] prices) {
return new SendInvoice(getChatId(), title, description, payload, currency, prices, this.requestSender)
.replyParameters(new ReplyParameters(getMessageId()).allowSendingWithoutReply(true))
.parseMode(parseMode);
}

public CreateInvoiceLink createInvoiceLink(Object chatId, String title, String description, String payload, String providerToken,
String currency, LabeledPrice[] prices) {
return new CreateInvoiceLink(chatId, title, description, payload, providerToken, currency, prices, this.requestSender);
public CreateInvoiceLink createInvoiceLink(Object chatId, String title, String description, String payload, String currency, LabeledPrice[] prices) {
return new CreateInvoiceLink(chatId, title, description, payload, currency, prices, this.requestSender);
}

public CreateInvoiceLink createInvoiceLink(String title, String description, String payload, String providerToken,
String currency, LabeledPrice[] prices) {
return new CreateInvoiceLink(getChatId(), title, description, payload, providerToken, currency, prices, this.requestSender);
public CreateInvoiceLink createInvoiceLink(String title, String description, String payload, String currency, LabeledPrice[] prices) {
return new CreateInvoiceLink(getChatId(), title, description, payload, currency, prices, this.requestSender);
}

public AnswerShippingQuery answerShippingQuery(String shippingQueryId, boolean ok) {
Expand Down
82 changes: 59 additions & 23 deletions src/main/java/et/telebof/Util.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package et.telebof;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import et.telebof.types.BotCommand;
import et.telebof.types.ChatMember;
import et.telebof.types.GameHighScore;
import et.telebof.types.Message;
import et.telebof.types.Sticker;
import et.telebof.types.Update;

import java.util.ArrayList;
import java.util.Arrays;
import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;

abstract public class Util {
static Gson gson = new Gson();

public static boolean isCommand(String text){
if (text == null ) return false;
public static boolean isCommand(String text) {
if (text == null) return false;
return text.startsWith("/");
}

public static String extractCommand(String text){
public static String extractCommand(String text) {
if (isCommand(text)) {
try {
String command;
Expand All @@ -39,13 +44,13 @@ public static String extractText(String text) {
String[] smart = text.split(" ");
int len = smart.length;
if (len == 1) return null;
List<String> smarts = Arrays.stream(smart).collect(Collectors.toList());
List<String> smarts = List.of(smart);
return String.join(" ", smarts.subList(1, len));
}
return null;
}

public static String escapeHtml(String text){
public static String escapeHtml(String text) {
if (text == null)
return null;
else return text
Expand All @@ -54,26 +59,57 @@ public static String escapeHtml(String text){
.replace("<", "&lt;");

}

public static ApiResponse parseApiResponse(String o){
return gson.fromJson(o, ApiResponse.class);


public static <T> ApiResponse<T> parseApiResponse(String json, Type type) {
Type responseType = com.google.gson.reflect.TypeToken.getParameterized(ApiResponse.class, type).getType();
return gson.fromJson(json, responseType);
}

public static <T> T parse(Object jsonString, Class<T> res) {
try{
return gson.fromJson((String) jsonString, res);
} catch (Exception e){
return gson.fromJson(gson.toJson(jsonString), res);
}
return gson.fromJson(gson.toJson(jsonString), res);
}

public static <T> List<T> parseList(List<Object> objectList, Class<T> res) {
List<T> tList = new ArrayList<>();
objectList.forEach(r->{
tList.add(Util.parse(gson.toJson(r), res));
});
public static <T> List<T> parseList(Object object, Class<T> tClass){
Type $type = com.google.gson.reflect.TypeToken.getParameterized(List.class, tClass).getType();
String string = gson.toJson(object);
return gson.fromJson(string, $type);

return tList;
}

public static List<Update> parseUpdates(Object json) {
Type type = new TypeToken<List<Update>>() {}.getType();
return gson.fromJson(gson.toJson(json), type);
}

public static List<Sticker> parseSticker(Object json) {
Type type = new TypeToken<List<Sticker>>() {}.getType();
return gson.fromJson(gson.toJson(json), type);
}

public static List<ChatMember> parseChatMember(Object json) {
Type type = new TypeToken<List<ChatMember>>() {}.getType();
return gson.fromJson(gson.toJson(json), type);
}

public static List<GameHighScore> parseGameHighScore(Object json) {
Type type = new TypeToken<List<GameHighScore>>() {}.getType();
return gson.fromJson(gson.toJson(json), type);
}

public static List<Message> parseMessages(Object json) {
Type type = new TypeToken<List<Message>>() {}.getType();
return gson.fromJson(gson.toJson(json), type);
}

public static List<BotCommand> parseGetMyCommands(Object json) {
Type type = new TypeToken<List<BotCommand>>() {}.getType();
return gson.fromJson(gson.toJson(json), type);
}



}




7 changes: 7 additions & 0 deletions src/main/java/et/telebof/enums/MenuButtonType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package et.telebof.enums;

public enum MenuButtonType {
COMMANDS,
DEFAULT,
WEB_APP;
}
6 changes: 6 additions & 0 deletions src/main/java/et/telebof/enums/MimeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package et.telebof.enums;

public enum MimeType {
PDF,
ZIP
}
18 changes: 17 additions & 1 deletion src/main/java/et/telebof/filters/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import et.telebof.states.StateMemoryStorage;
import et.telebof.types.Message;
import et.telebof.types.User;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class Filter{
private final Update update;
Expand All @@ -20,7 +23,7 @@ public Boolean text(){
return update.message !=null && update.message.text != null;
}

public Boolean privateChat(){
public Boolean Private(){
return chatType("private");
}

Expand Down Expand Up @@ -314,4 +317,17 @@ else if (update.callback_query != null){
else return false;
}

public boolean texts(String... texts){
if (text()){
return List.of(texts).contains(update.message.text);
} else return false;
}

public boolean regex(String pattern){
if (!text()) return false;

Pattern instance = Pattern.compile(pattern);
return instance.matcher(update.message.text).find();
}

}
Loading

0 comments on commit 8362199

Please sign in to comment.