rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.78k stars 1.22k forks source link

execute(SendPhoto) fails when bot is injected as Spring dependency #536

Closed sergey124 closed 6 years ago

sergey124 commented 6 years ago

In Spring, when bot is an injected dependency of MyBotClass type, execute(SendPhoto) fails as well as the other PartialBotApiMethod.

Solution: Inject bot as AbsSender

@Autowired
@Lazy
private AbsSender bot;

Cause: DefaultAbsSender.execute(SendPhoto) - final, CGLIB proxy cannot call the real class https://stackoverflow.com/questions/34563058/spring-aop-cglib-proxys-field-is-null selection_075

java.lang.NullPointerException: null
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendHttpPostRequest(DefaultAbsSender.java:765) ~[telegrambots-4.1.jar:na]
    at org.telegram.telegrambots.bots.DefaultAbsSender.execute(DefaultAbsSender.java:226) ~[telegrambots-4.1.jar:na]
    at org.vors.telegram.fivemin.service.MessageService.sendPhoto(MessageService.java:107) ~[classes/:na]
sergey124 commented 6 years ago

Closing, solution is in the description