org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException Java Examples

The following examples show how to use org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException. 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: TelegramBotsApi.java    From TelegramBots with MIT License 6 votes vote down vote up
/**
 * 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
 */
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");
    }
    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");
    }
    if (pathToCertificate == null || pathToCertificate.isEmpty()) {
        throw new TelegramApiRequestException("Parameter pathToCertificate can not be null or empty");
    }

    this.useWebhook = true;
    this.externalUrl = fixExternalUrl(externalUrl);
    this.pathToCertificate = pathToCertificate;
    webhook = ApiContext.getInstance(Webhook.class);
    webhook.setInternalUrl(internalUrl);
    webhook.setKeyStore(keyStore, keyStorePassword);
    webhook.startServer();
}
 
Example #2
Source File: TelegramBotsApi.java    From TelegramBots with MIT License 6 votes vote down vote up
/**
 * 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();
}
 
Example #3
Source File: DefaultWebhook.java    From TelegramBots with MIT License 6 votes vote down vote up
public void startServer() throws TelegramApiRequestException {
    ResourceConfig rc = new ResourceConfig();
    rc.register(restApi);
    rc.register(JacksonFeature.class);

    final HttpServer grizzlyServer;
    if (keystoreServerFile != null && keystoreServerPwd != null) {
        SSLContextConfigurator sslContext = new SSLContextConfigurator();

        // set up security context
        sslContext.setKeyStoreFile(keystoreServerFile); // contains server keypair
        sslContext.setKeyStorePass(keystoreServerPwd);

        grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc, true,
                new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false));
    } else {
        grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc);
    }

    try {
        grizzlyServer.start();
    } catch (IOException e) {
        throw new TelegramApiRequestException("Error starting webhook server", e);
    }
}
 
Example #4
Source File: RestrictChatMember.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error restricting chat member", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #5
Source File: SetChatPermissions.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting chat description", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #6
Source File: SetChatAdministratorCustomTitle.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting chat description", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #7
Source File: GetChatMember.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public ChatMember deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<ChatMember> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<ChatMember>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error getting chat member", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #8
Source File: GetGameHighScores.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public ArrayList<GameHighScore> deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<ArrayList<GameHighScore>> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<ArrayList<GameHighScore>>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error getting game high scores", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #9
Source File: AnswerPreCheckoutQuery.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error answering pre-checkout query", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #10
Source File: DeleteChatStickerSet.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error deleting chat sticker set", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #11
Source File: GetUserProfilePhotos.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public UserProfilePhotos deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<UserProfilePhotos> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<UserProfilePhotos>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error getting user profile photos", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #12
Source File: SetChatStickerSet.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting chat sticker set", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #13
Source File: SetChatPhoto.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting chat photo", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #14
Source File: UnbanChatMember.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error unbanning chat member", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #15
Source File: SetChatDescription.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting chat description", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #16
Source File: GetChat.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Chat deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Chat> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Chat>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error getting chat", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #17
Source File: KickChatMember.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error kicking chat member", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #18
Source File: SetChatTitle.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting chat title", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #19
Source File: ExportChatInviteLink.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public String deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<String> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<String>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error exporting invite link", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #20
Source File: AnswerShippingQuery.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error answering shipping query", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #21
Source File: SetPassportDataErrors.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error setting passport data errors", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #22
Source File: UnpinChatMessage.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error unpinning chat message", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #23
Source File: PinChatMessage.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Boolean>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error pinning chat message", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #24
Source File: SendContact.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Message>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error sending contact", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #25
Source File: SendPhoto.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Message>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error sending photo", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #26
Source File: SendInvoice.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Message>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error sending invoice", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #27
Source File: SendMessage.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Message>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error sending message", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #28
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 #29
Source File: SendDice.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Message>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error sending dice", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #30
Source File: SendLocation.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Message>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error sending location", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}