org.telegram.telegrambots.meta.TelegramBotsApi Java Examples

The following examples show how to use org.telegram.telegrambots.meta.TelegramBotsApi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From TelegramBotsExample with GNU General Public License v3.0 6 votes vote down vote up
private static TelegramBotsApi createTelegramBotsApi() throws TelegramApiException {
    TelegramBotsApi telegramBotsApi;
    if (!BuildVars.useWebHook) {
        // Default (long polling only)
        telegramBotsApi = createLongPollingTelegramBotsApi();
    } else if (!BuildVars.pathToCertificatePublicKey.isEmpty()) {
        // Filled a path to a pem file ? looks like you're going for the self signed option then, invoke with store and pem file to supply.
        telegramBotsApi = createSelfSignedTelegramBotsApi();
        telegramBotsApi.registerBot(new WebHookExampleHandlers());
    } else {
        // Non self signed, make sure you've added private/public and if needed intermediate to your cert-store.
        telegramBotsApi = createNoSelfSignedTelegramBotsApi();
        telegramBotsApi.registerBot(new WebHookExampleHandlers());
    }
    return telegramBotsApi;
}
 
Example #2
Source File: TestTelegramBotStarterRegistrationHooks.java    From TelegramBots with MIT License 6 votes vote down vote up
@Test
void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiRequestException {

       when(mockTelegramBotsApi.registerBot(any(LongPollingBot.class))).thenReturn(someBotSession);

       this.contextRunner.withUserConfiguration(LongPollingBotConfig.class)
               .run((context) -> {
                   assertThat(context).hasSingleBean(AnnotatedLongPollingBot.class);

                   final LongPollingBot bot = context.getBean(LongPollingBot.class);
                   final TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);

                  assertThat(hookCalled).isTrue();
                  assertThat(hookCalledWithSession).isTrue();
                  verify(telegramBotsApi, times(1)).registerBot(bot);
                  verifyNoMoreInteractions(telegramBotsApi);
               });
   }
 
Example #3
Source File: TestTelegramApi.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void TestTelegramApiMustBeInitializableForWebhookWithoutSecureSupport() {
    try {
        new TelegramBotsApi("externalUrl", "internalUrl");
    } catch (TelegramApiRequestException e) {
        fail();
    }
}
 
Example #4
Source File: TestTelegramBotStarterConfiguration.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void createLongPoolingBotAndWebhookBot() {
	this.contextRunner.withUserConfiguration(LongPollingBotConfig.class, WebhookBotConfig.class)
			.run((context) -> {
				assertThat(context).hasSingleBean(LongPollingBot.class);
				assertThat(context).hasSingleBean(WebhookBot.class);

				TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);
				
		        verify(telegramBotsApi, times(1)).registerBot( context.getBean(LongPollingBot.class) );
		        verify(telegramBotsApi, times(1)).registerBot( context.getBean(WebhookBot.class) );
		        //verifyNoMoreInteractions(telegramBotsApi);
	});
}
 
Example #5
Source File: TestTelegramBotStarterConfiguration.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void createOnlyWebhookBot() {
	this.contextRunner.withUserConfiguration(WebhookBotConfig.class)
			.run((context) -> {
				assertThat(context).hasSingleBean(WebhookBot.class);
				assertThat(context).doesNotHaveBean(LongPollingBot.class);
				
				TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);
				
		        verify(telegramBotsApi, times(1)).registerBot( context.getBean(WebhookBot.class) );
		        verifyNoMoreInteractions(telegramBotsApi);
	});
}
 
Example #6
Source File: TestTelegramBotStarterConfiguration.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void createOnlyLongPollingBot() {
	this.contextRunner.withUserConfiguration(LongPollingBotConfig.class)
			.run((context) -> {
				assertThat(context).hasSingleBean(LongPollingBot.class);
				assertThat(context).doesNotHaveBean(WebhookBot.class);
				
				TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);
		        
				verify(telegramBotsApi, times(1)).registerBot( context.getBean(LongPollingBot.class) );
		        verifyNoMoreInteractions(telegramBotsApi);
	});
}
 
Example #7
Source File: TestTelegramBotStarterConfiguration.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void createMockTelegramBotsApiWithDefaultSettings() {
	this.contextRunner.run((context) -> {
		assertThat(context).hasSingleBean(TelegramBotsApi.class);
		assertThat(context).hasSingleBean(TelegramBotInitializer.class);
		assertThat(context).doesNotHaveBean(LongPollingBot.class);
		assertThat(context).doesNotHaveBean(WebhookBot.class);
		verifyNoMoreInteractions(context.getBean(TelegramBotsApi.class));
	});
}
 
Example #8
Source File: TelegramBotInitializer.java    From TelegramBots with MIT License 5 votes vote down vote up
public TelegramBotInitializer(TelegramBotsApi telegramBotsApi,
								List<LongPollingBot> longPollingBots,
                                List<WebhookBot> webHookBots) {
	Objects.requireNonNull(telegramBotsApi);
	Objects.requireNonNull(longPollingBots);
	Objects.requireNonNull(webHookBots);
	this.telegramBotsApi = telegramBotsApi;
    this.longPollingBots = longPollingBots;
    this.webHookBots = webHookBots;
}
 
Example #9
Source File: TelegramBotStarterConfiguration.java    From TelegramBots with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public TelegramBotInitializer telegramBotInitializer(TelegramBotsApi telegramBotsApi,
                                                     ObjectProvider<List<LongPollingBot>> longPollingBots,
                                                     ObjectProvider<List<WebhookBot>> webHookBots) {
    return new TelegramBotInitializer(telegramBotsApi,
            longPollingBots.getIfAvailable(Collections::emptyList),
            webHookBots.getIfAvailable(Collections::emptyList));
}
 
Example #10
Source File: TestTelegramApi.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void TestTelegramApiMustBeInitializableForWebhookWithSelfSignedCertificate() {
    try {
        new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl", "selfSignedPath");
    } catch (TelegramApiRequestException e) {
        fail();
    }
}
 
Example #11
Source File: TestTelegramApi.java    From TelegramBots with MIT License 5 votes vote down vote up
@Test
void TestTelegramApiMustBeInitializableForWebhook() {
    try {
        new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl");
    } catch (TelegramApiRequestException e) {
        fail();
    }
}
 
Example #12
Source File: TelegramBotStarterConfiguration.java    From TelegramBots with MIT License 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(TelegramBotsApi.class)
public TelegramBotsApi telegramBotsApi() {
    return new TelegramBotsApi();
}
 
Example #13
Source File: TestTelegramBotStarterConfiguration.java    From TelegramBots with MIT License 4 votes vote down vote up
@Bean
public TelegramBotsApi telegramBotsApi() {
	return mock(TelegramBotsApi.class);
}
 
Example #14
Source File: TestTelegramBotStarterRegistrationHooks.java    From TelegramBots with MIT License 4 votes vote down vote up
@Bean
public TelegramBotsApi telegramBotsApi() {
	return mockTelegramBotsApi;
}
 
Example #15
Source File: TestTelegramApi.java    From TelegramBots with MIT License 4 votes vote down vote up
@Test
void TestTelegramApiMustBeInitializableForLongPolling() {
    new TelegramBotsApi();
}
 
Example #16
Source File: Main.java    From TelegramBotsExample with GNU General Public License v3.0 2 votes vote down vote up
/**
 * @brief Creates a Telegram Bots Api to use Long Polling (getUpdates) bots.
 * @return TelegramBotsApi to register the bots.
 */
private static TelegramBotsApi createLongPollingTelegramBotsApi() {
    return new TelegramBotsApi();
}
 
Example #17
Source File: Main.java    From TelegramBotsExample with GNU General Public License v3.0 2 votes vote down vote up
/**
 * @brief Creates a Telegram Bots Api to use Long Polling bots and webhooks bots with self-signed certificates.
 * @return TelegramBotsApi to register the bots.
 *
 * @note https://core.telegram.org/bots/self-signed#java-keystore for generating a keypair in store and exporting the pem.
*  @note Don't forget to split the pem bundle (begin/end), use only the public key as input!
 */
private static TelegramBotsApi createSelfSignedTelegramBotsApi() throws TelegramApiException {
    return new TelegramBotsApi(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL, BuildVars.pathToCertificatePublicKey);
}
 
Example #18
Source File: Main.java    From TelegramBotsExample with GNU General Public License v3.0 2 votes vote down vote up
/**
 * @brief Creates a Telegram Bots Api to use Long Polling bots and webhooks bots with no-self-signed certificates.
 * @return TelegramBotsApi to register the bots.
 *
 * @note Coming from a set of pem files here's one way to do it:
 * @code{.sh}
 * openssl pkcs12 -export -in public.pem -inkey private.pem > keypair.p12
 * keytool -importkeystore -srckeystore keypair.p12 -destkeystore server.jks -srcstoretype pkcs12
 * #have (an) intermediate(s) to supply? first:
 * cat public.pem intermediate.pem > set.pem (use set.pem as -in)
 * @endcode
 */
private static TelegramBotsApi createNoSelfSignedTelegramBotsApi() throws TelegramApiException {
    return new TelegramBotsApi(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL);
}