Skip to content

Commit

Permalink
Remove Guice
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenlagus committed Nov 3, 2020
1 parent fb5626d commit ab00b18
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 267 deletions.
1 change: 1 addition & 0 deletions TelegramBots.wiki/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
6. When registering a Webhook Bot, a SetWebhook object must be provided.
7. When using Webhook with Spring, extends class SpringWebhookBot instead of WebhookBot
8. New Async methods returning CompletableFutures.
9. No more Guice to define custom class

### <a id="4.9.2"></a>4.9.2 ###
1. Bug fixing: #792, #801, #804, #810, #812, #813, #820 and #814
Expand Down
5 changes: 1 addition & 4 deletions TelegramBots.wiki/abilities/Simple-Example.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ Running the bot is just like running the regular Telegram bots. Create a Java cl
```java
public class Application {
public static void main(String[] args) {
// Initializes dependencies necessary for the base bot - Guice
ApiContextInitializer.init();

// Create the TelegramBotsApi object to register your bots
TelegramBotsApi botsApi = new TelegramBotsApi();
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);

try {
// Register your newly created AbilityBot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
Expand All @@ -24,7 +23,7 @@ public TelegramLongPollingSessionBot(){
}

public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
this(chatIdConverter, ApiContext.getInstance(DefaultBotOptions.class));
this(chatIdConverter, new DefaultBotOptions());
}

public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, DefaultBotOptions defaultBotOptions){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.telegram.telegrambots.extensions.bots.commandbot;


import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;
Expand Down Expand Up @@ -29,7 +28,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
*
*/
public TelegramLongPollingCommandBot() {
this(ApiContext.getInstance(DefaultBotOptions.class));
this(new DefaultBotOptions());
}

/**
Expand Down
12 changes: 0 additions & 12 deletions telegrambots-meta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,13 @@

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<guice.version>4.2.3</guice.version>
<jackson.version>2.11.3</jackson.version>
<jacksonanotation.version>2.11.3</jacksonanotation.version>
<json.version>20180813</json.version>
<guava.version>30.0-jre</guava.version>
</properties>

<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.generics.WebhookBot;

import java.text.MessageFormat;
import java.lang.reflect.InvocationTargetException;

/**
* @author Ruben Bermudez
Expand All @@ -17,110 +16,53 @@
*/
public class TelegramBotsApi {
private static final String webhookUrlFormat = "{0}callback/";

Class<? extends BotSession> botSessionClass;
private boolean useWebhook; ///< True to enable webhook usage
private Webhook webhook; ///< Webhook instance
private String externalUrl; ///< External url of the bots
private String pathToCertificate; ///< Path to public key certificate

/**
*
*/
public TelegramBotsApi() {
public TelegramBotsApi(Class<? extends BotSession> botSessionClass) throws TelegramApiException {
if (botSessionClass == null) {
throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
this.botSessionClass = botSessionClass;
}

/**
* Creates an HTTP server to receive webhook request
* @param externalUrl External base url for the webhook
* @param internalUrl Internal base url for the webhook
* @param webhook Webhook class
*
* @implSpec This option requires externally handled HTTPS support (i.e. via proxy)
*/
public TelegramBotsApi(String externalUrl, String internalUrl) throws TelegramApiRequestException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
}

this.useWebhook = true;
this.externalUrl = fixExternalUrl(externalUrl);
webhook = ApiContext.getInstance(Webhook.class);
webhook.setInternalUrl(internalUrl);
webhook.startServer();
}

/**
* Creates an HTTPS server to receive webhook request
* @param keyStore KeyStore for the server
* @param keyStorePassword Key store password for the server
* @param externalUrl External base url for the webhook
* @param internalUrl Internal base url for the webhook
*/
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiRequestException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
}
if (keyStore == null || keyStore.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStore can not be null or empty");
}
if (keyStorePassword == null || keyStorePassword.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty");
}

this.useWebhook = true;
this.externalUrl = fixExternalUrl(externalUrl);
webhook = ApiContext.getInstance(Webhook.class);
webhook.setInternalUrl(internalUrl);
webhook.setKeyStore(keyStore, keyStorePassword);
webhook.startServer();
}

/**
* Creates an HTTPS server with self-signed certificate to receive webhook request
* @param keyStore KeyStore for the server
* @param keyStorePassword Key store password for the server
* @param externalUrl External base url for the webhook
* @param internalUrl Internal base url for the webhook
* @param pathToCertificate Full path until .pem public certificate keys
* @implSpec This option may require externally handled HTTPS support (i.e. via proxy)
*/
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate) throws TelegramApiRequestException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
public TelegramBotsApi(Class<? extends BotSession> botSessionClass, Webhook webhook) throws TelegramApiException {
if (botSessionClass == null) {
throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
if (keyStore == null || keyStore.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStore can not be null or empty");
if (webhook == null) {
throw new TelegramApiException("Parameter webhook can not be null or empty");
}
if (keyStorePassword == null || keyStorePassword.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty");
}
if (pathToCertificate == null || pathToCertificate.isEmpty()) {
throw new TelegramApiRequestException("Parameter pathToCertificate can not be null or empty");
}

this.botSessionClass = botSessionClass;
this.useWebhook = true;
this.externalUrl = fixExternalUrl(externalUrl);
this.pathToCertificate = pathToCertificate;
webhook = ApiContext.getInstance(Webhook.class);
webhook.setInternalUrl(internalUrl);
webhook.setKeyStore(keyStore, keyStorePassword);
webhook.startServer();
this.webhook = webhook;
this.webhook.startServer();
}

/**
* Register a bot. The Bot Session is started immediately, and may be disconnected by calling close.
* @param bot the bot to register
*/
public BotSession registerBot(LongPollingBot bot) throws TelegramApiRequestException {
public BotSession registerBot(LongPollingBot bot) throws TelegramApiException {
bot.onRegister();
bot.clearWebhook();
BotSession session = ApiContext.getInstance(BotSession.class);
BotSession session;
try {
session = botSessionClass.getConstructor().newInstance();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new TelegramApiException(e);
}
session.setToken(bot.getBotToken());
session.setOptions(bot.getOptions());
session.setCallback(bot);
Expand All @@ -140,11 +82,4 @@ public void registerBot(WebhookBot bot, SetWebhook setWebhook) throws TelegramAp
bot.setWebhook(setWebhook);
}
}

private static String fixExternalUrl(String externalUrl) {
if (externalUrl != null && !externalUrl.endsWith("/")) {
externalUrl = externalUrl + "/";
}
return MessageFormat.format(webhookUrlFormat, externalUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.telegram.telegrambots.meta.generics;

import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

/**
* @author Ruben Bermudez
* @version 1.0
* Webhook interface
*/
public interface Webhook {
void startServer() throws TelegramApiRequestException;
void startServer() throws TelegramApiException;
void registerWebhook(WebhookBot callback);
void setInternalUrl(String internalUrl);
void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiRequestException;
void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void TestListUpdates() throws Exception {
"\"message\":{\"message_id\":97,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154360,\"video_note\":{\"duration\":3,\"length\":240,\"thumb\":{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":6852,\"width\":240,\"height\":240},\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":100544}}},{\"update_id\":79995152,\n" +
"\"message\":{\"message_id\":98,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154363,\"voice\":{\"duration\":1,\"mime_type\":\"audio/ogg\",\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":5198}}},{\"update_id\":79995153,\n" +
"\"message\":{\"message_id\":99,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154371,\"photo\":[{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":14395,\"width\":180,\"height\":320},{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":52852,\"width\":450,\"height\":800},{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":84493,\"width\":720,\"height\":1280}]}},{\"update_id\":79995154,\n" +
"\"message\":{\"message_id\":6,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":-110011556359722345678,\"title\":\"test group\",\"type\":\"supergroup\"},\"date\":1604163105,\"new_chat_members\":[{\"id\":123455678,\"is_bot\":true,\"first_name\":\"Testing\",\"username\":\"TestingBot\"}]}}]}";
"\"message\":{\"message_id\":6,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":-1556359722345678,\"title\":\"test group\",\"type\":\"supergroup\"},\"date\":1604163105,\"new_chat_members\":[{\"id\":123455678,\"is_bot\":true,\"first_name\":\"Testing\",\"username\":\"TestingBot\"}]}}]}";

ArrayList<Update> response = new GetUpdates().deserializeResponse(updateText);

Expand Down
Loading

0 comments on commit ab00b18

Please sign in to comment.