package com.red.circle.order.app.framework;

import com.alibaba.fastjson.JSON;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.request.SetWebhook;
import com.pengrad.telegrambot.response.BaseResponse;
import com.pengrad.telegrambot.utility.BotUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description:
 *
 * @author 金泽强
 * @date 2024/12/10 20:04
 */
@Slf4j
@RequiredArgsConstructor
//@Configuration
@EnableConfigurationProperties({TelegramProperties.class})
public class TelegramConfig {

    private final TelegramProperties telegramProperties;

    @Bean
    public TelegramBot telegramBot() {
        log.info("init telegramBot{}", JSON.toJSONString(telegramProperties));
        TelegramBot telegramBot = new TelegramBot.Builder(telegramProperties.getBotToken()).build();
        if (StringUtils.isNotBlank(telegramProperties.getWebhookUrl())) {
            SetWebhook setWebhook = new SetWebhook().url(telegramProperties.getWebhookUrl());
            BaseResponse response = telegramBot.execute(setWebhook);
            log.info("setWebhook:{}", BotUtils.toJson(response));
        }
        return telegramBot;
    }

}
