@Bean @ConditionalOnMissingBean(TelegramBotsApi.class) public TelegramBotsApi telegramBotsApi() { return new TelegramBotsApi(); }
@Override public void afterPropertiesSet() throws Exception { try { for (LongPollingBot bot : longPollingBots) { BotSession session = telegramBotsApi.registerBot(bot); handleAfterRegistrationHook(bot, session); } for (WebhookBot bot : webHookBots) { telegramBotsApi.registerBot(bot); } } catch (TelegramApiException e) { throw new RuntimeException(e); } }
/** * Creates an HTTP server to receive webhook request * @param externalUrl External base url for the webhook * @param internalUrl Internal base url for the webhook * * @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(); }
/** * @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(); }
@PostConstruct public void start() throws TelegramApiRequestException { log.info("Starting auto config for telegram bots"); TelegramBotsApi api = getApi(); pollingBots.forEach(bot -> { try { log.info("Registering polling bot: {}", bot.getBotUsername()); sessions.add(api.registerBot(bot)); } catch (TelegramApiException e) { log.error("Failed to register bot {} due to error {}", bot.getBotUsername(), e.getMessage()); } }); webHookBots.forEach(bot -> { try { log.info("Registering web hook bot: {}", bot.getBotUsername()); api.registerBot(bot); } catch (TelegramApiException e) { log.error("Failed to register bot {} due to error {}", bot.getBotUsername(), e.getMessage()); } }); }
/** * 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(); }
/** * @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); }
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; }
this.externalUrl = fixExternalUrl(externalUrl); this.pathToCertificate = pathToCertificate; webhook = ApiContext.getInstance(Webhook.class);
/** * @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); } }
public static void main(String[] args) { BotLogger.setLevel(Level.ALL); BotLogger.registerLogger(new ConsoleHandler()); try { BotLogger.registerLogger(new BotsFileHandler()); } catch (IOException e) { BotLogger.severe(LOGTAG, e); } try { ApiContextInitializer.init(); TelegramBotsApi telegramBotsApi = createTelegramBotsApi(); try { // Register long polling bots. They work regardless type of TelegramBotsApi we are creating telegramBotsApi.registerBot(new ChannelHandlers()); telegramBotsApi.registerBot(new DirectionsHandlers()); telegramBotsApi.registerBot(new RaeHandlers()); telegramBotsApi.registerBot(new WeatherHandlers()); telegramBotsApi.registerBot(new TransifexHandlers()); telegramBotsApi.registerBot(new FilesHandlers()); telegramBotsApi.registerBot(new CommandsHandler(BotConfig.COMMANDS_USER)); telegramBotsApi.registerBot(new ElektrollArtFanHandler()); } catch (TelegramApiException e) { BotLogger.error(LOGTAG, e); } } catch (Exception e) { BotLogger.error(LOGTAG, e); } }
/** * Creates an HTTP server to receive webhook request * @param externalUrl External base url for the webhook * @param internalUrl Internal base url for the webhook * * @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(); }
/** * Get API object depending on configured properties. * If no properties are configured, the API object won't support webhooks. * @return api object * @throws TelegramApiRequestException */ @Bean @ConditionalOnMissingBean public TelegramBotsApi getApi() throws TelegramApiRequestException { TelegramBotsApi result; if (!StringUtils.isEmpty(externalUrl) && !StringUtils.isEmpty(internalUrl)) { if (!StringUtils.isEmpty(keyStore) && !StringUtils.isEmpty(keyStorePassword)) { if (!StringUtils.isEmpty(pathToCertificate)) { log.info("Initializing API with webhook support and configured keystore and path to certificate"); result = new TelegramBotsApi(keyStore, keyStorePassword, externalUrl, internalUrl, pathToCertificate); } else { log.info("Initializing API with webhook support and configured keystore"); result = new TelegramBotsApi(keyStore, keyStorePassword, externalUrl, internalUrl); } } else { log.info("Initializing API with webhook support"); result = new TelegramBotsApi(externalUrl, internalUrl); } } else { log.info("Initializing API without webhook support"); result = new TelegramBotsApi(); } return result; }
/** * 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(); }
this.externalUrl = fixExternalUrl(externalUrl); this.pathToCertificate = pathToCertificate; webhook = ApiContext.getInstance(Webhook.class);