org.telegram.telegrambots.meta.generics.BotSession Java Examples

The following examples show how to use org.telegram.telegrambots.meta.generics.BotSession. 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: TelegramBotInitializer.java    From TelegramBots with MIT License 6 votes vote down vote up
private void handleAnnotatedMethod(Object bot, Method method, BotSession session) {
    try {
        TelegramApiRequestException test = new TelegramApiRequestException("Error getting updates", new ApiResponse());
        log.error(test.getMessage(), test);

        if (method.getParameterCount() > 1) {
            log.warn(format("Method %s of Type %s has too many parameters",
                            method.getName(), method.getDeclaringClass().getCanonicalName()));
            return;
        }
        if (method.getParameterCount() == 0) {
            method.invoke(bot);
            return;
        }
        if (method.getParameterTypes()[0].equals(BotSession.class)) {
            method.invoke(bot, session);
            return;
        }
        log.warn(format("Method %s of Type %s has invalid parameter type",
                        method.getName(), method.getDeclaringClass().getCanonicalName()));
    } catch (InvocationTargetException | IllegalAccessException e) {
        log.error(format("Couldn't invoke Method %s of Type %s",
                method.getName(), method.getDeclaringClass().getCanonicalName()));
    }
}
 
Example #2
Source File: TelegramBotsApi.java    From TelegramBots with MIT License 5 votes vote down vote up
/**
 * 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 {
    bot.clearWebhook();
    BotSession session = ApiContext.getInstance(BotSession.class);
    session.setToken(bot.getBotToken());
    session.setOptions(bot.getOptions());
    session.setCallback(bot);
    session.start();
    return session;
}
 
Example #3
Source File: TestBase.java    From TelegramBots with MIT License 4 votes vote down vote up
@BeforeAll
public static void beforeClass() {
    ApiContext.register(BotSession.class, FakeBotSession.class);
    ApiContext.register(Webhook.class, FakeWebhook.class);
}
 
Example #4
Source File: TelegramBotInitializer.java    From TelegramBots with MIT License 4 votes vote down vote up
private void handleAfterRegistrationHook(Object bot, BotSession botSession) {
    Stream.of(bot.getClass().getMethods())
            .filter(method -> method.getAnnotation(AfterBotRegistration.class) != null)
            .forEach(method -> handleAnnotatedMethod(bot, method, botSession));

}
 
Example #5
Source File: TestTelegramBotStarterRegistrationHooks.java    From TelegramBots with MIT License 4 votes vote down vote up
@AfterBotRegistration
public void afterBotHookWithSession(BotSession session) {
          hookCalledWithSession = session.equals(someBotSession);
      }
 
Example #6
Source File: ApiContextInitializer.java    From TelegramBots with MIT License 4 votes vote down vote up
public static void init() {
    ApiContext.register(BotSession.class, DefaultBotSession.class);
    ApiContext.register(Webhook.class, DefaultWebhook.class);
}