Webhook problem
ByteInternetHK opened this issue · comments
Lorenzo Stflorant commented
Hello, i'm trying to use Webhook method to handle the incoming message.
But i get a problem: i get the update object always shows null value
There are some code below:
TelegramBot bot = new TelegramBot(botToken);
SetWebhook request = new SetWebhook().url(url + "/api/telegram/webhook");
BaseResponse response = bot.execute(request);
log.info("webhook response: {}", response.description());
It will get log:
webhook response: Webhook was set
And the getWebhookInfo shows:
{
"ok": true,
"result": {
"url": "https://xx.xx/api/telegram/webhook",
"has_custom_certificate": false,
"pending_update_count": 0,
"max_connections": 40,
"ip_address": "xx.xx.xx.xx",
"allowed_updates": [
"message",
"callback_query"
],
"last_synchronization_error_date": 1714206571
}
}
I write a Controller to get income upadte object:
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class TelegramBotController {
private final TelegramService telegramService;
@PostMapping("/telegram/webhook")
public boolean telegramWebhook(@RequestBody Update update) {
return telegramService.handleWebhook(update);
}
}
TelegramService:
public boolean handleWebhook(Update update) {
log.info("handleWebhook: {}", update);
if (ObjectUtil.isEmpty(update.message())) {
return false;
}
Message message = update.message();
if (ObjectUtil.isNotEmpty(message.entities())) {
if (message.text().startsWith("/start")) {
handleStart(message);
}
}
return true;
}
Here log always shows update object:
handleWebhook: Update{update_id=null, message=null, edited_message=null, channel_post=null, edited_channel_post=null, business_connection=null, business_message=null, edited_business_message=null, deleted_business_messages=null, inline_query=null, chosen_inline_result=null, callback_query=null, shipping_query=null, pre_checkout_query=null, poll=null, poll_answer=null, my_chat_member=null, chat_member=null, chat_join_request=null, message_reaction=null, message_reaction_count=null, chat_boost=null, removed_chat_boost=null}
what's the problem in my code
Lorenzo Stflorant commented
Problem solved.
for security, set secretToken here
SetWebhook request = new SetWebhook().url(url + "/api/telegram/webhook").secretToken("MetePayBot");
Controller
@PostMapping("/telegram/webhook")
public boolean telegramWebhook(@RequestHeader("X-Telegram-Bot-Api-Secret-Token") String token, @RequestBody String requestBody) {
return telegramService.handleWebhook(token, requestBody);
}
Service
public boolean handleWebhook(String token, Update update) {
log.debug("token: {}", token);
if (ObjectUtil.notEqual(token, "Your Security Token")) {
return false;
}
log.debug("requestBody: {}", requestBody);
Update update = BotUtils.parseUpdate(requestBody);
log.debug("handleWebhook: {}", update);
if (ObjectUtil.isEmpty(update.message())) {
return false;
}
Message message = update.message();
if (ObjectUtil.isNotEmpty(message.entities())) {
if (message.text().startsWith("/start")) {
handleStart(message);
}
}
return true;
}