xiongzhu 2 سال پیش
والد
کامیت
a2a73dace3

+ 6 - 0
pom.xml

@@ -362,6 +362,12 @@
             <artifactId>redisson-spring-boot-starter</artifactId>
             <version>3.17.3</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.telegram</groupId>
+            <artifactId>telegrambots</artifactId>
+            <version>6.5.0</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 4 - 1
src/main/java/com/izouma/awesomeAdmin/Application.java

@@ -1,11 +1,14 @@
 package com.izouma.awesomeAdmin;
 
+import com.izouma.awesomeAdmin.web.TelegramBot;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
 import org.springframework.scheduling.annotation.EnableScheduling;
-import springfox.documentation.swagger2.annotations.EnableSwagger2;
+import org.telegram.telegrambots.meta.TelegramBotsApi;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
+import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
 
 @SpringBootApplication
 @EnableJpaAuditing

+ 1 - 1
src/main/java/com/izouma/awesomeAdmin/security/WebSecurityConfig.java

@@ -83,7 +83,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/product/get/**").permitAll()
                 .antMatchers("/user/rankByProfit").permitAll()
                 .antMatchers("/user/rankByInvite").permitAll()
-                .antMatchers("/article/all").permitAll()
+                .antMatchers("/article/all", "/article/get/*").permitAll()
                 .antMatchers("/luckPay/notify/**", "/cloudPay/notify/**").permitAll()
                 .antMatchers("/test/**").permitAll()
                 .antMatchers("/bonus/today", "/financeProduct/today").permitAll()

+ 1 - 1
src/main/java/com/izouma/awesomeAdmin/service/CommissionService.java

@@ -162,7 +162,7 @@ public class CommissionService {
     public void invite(Long superiorId, Long juniorId) {
         CommissionStat commissionStat = commissionStatRepo.findByUserId(superiorId)
                 .orElse(new CommissionStat(superiorId));
-        commissionStat.setInviteNum(commissionStat.getInviteNum() + 1);
+        commissionStat.setInviteNum((int) userRepo.countBySuperiorId(superiorId));
         commissionStatRepo.save(commissionStat);
 
         JuniorContribution juniorContribution = juniorContributionRepo.findByUserIdAndJuniorId(superiorId, juniorId)

+ 11 - 0
src/main/java/com/izouma/awesomeAdmin/service/TelegramBotConfig.java

@@ -0,0 +1,11 @@
+package com.izouma.awesomeAdmin.service;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@Data
+@ConfigurationProperties(prefix = "tg-bot")
+public class TelegramBotConfig {
+    private String username;
+    private String token;
+}

+ 29 - 0
src/main/java/com/izouma/awesomeAdmin/service/TelegramBotInitializer.java

@@ -0,0 +1,29 @@
+package com.izouma.awesomeAdmin.service;
+
+import com.izouma.awesomeAdmin.web.TelegramBot;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.telegram.telegrambots.meta.TelegramBotsApi;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
+import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
+
+import javax.annotation.PostConstruct;
+
+@Service
+@Slf4j
+@AllArgsConstructor
+public class TelegramBotInitializer {
+
+    private final TelegramBot telegramBot;
+
+    @PostConstruct
+    public void init() {
+        try {
+            TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
+            telegramBotsApi.registerBot(telegramBot);
+        } catch (TelegramApiException e) {
+            log.error("Error occurred: " + e.getMessage());
+        }
+    }
+}

+ 9 - 2
src/main/java/com/izouma/awesomeAdmin/service/UserService.java

@@ -82,6 +82,7 @@ public class UserService {
     private UserBalanceRepo      userBalanceRepo;
     @PersistenceContext
     private EntityManager        manager;
+    private UserBalanceService   userBalanceService;
 
     public void del(Long id) {
         userRepo.deleteById(id);
@@ -115,6 +116,7 @@ public class UserService {
             userRepo.updateJuniorCount(invitor, (int) userRepo.countBySuperiorId(invitor));
         }
         user.setNewRegister(newRegister);
+        userBalanceService.balance(user.getId());
         return user;
     }
 
@@ -145,7 +147,7 @@ public class UserService {
             commissionService.invite(invitor, user.getId());
             userRepo.updateJuniorCount(invitor, (int) userRepo.countBySuperiorId(invitor));
         }
-
+        userBalanceService.balance(user.getId());
         return user;
     }
 
@@ -172,7 +174,7 @@ public class UserService {
             commissionService.invite(invitor, user.getId());
             userRepo.updateJuniorCount(invitor, (int) userRepo.countBySuperiorId(invitor));
         }
-
+        userBalanceService.balance(user.getId());
         return user;
     }
 
@@ -465,4 +467,9 @@ public class UserService {
         if (info.length() <= 3) return info.charAt(0) + "*";
         return info.charAt(0) + "***" + info.substring(info.length() - 1);
     }
+
+    public void setSuperior(Long userId, Long superiorId) {
+        commissionService.invite(superiorId, userId);
+        userRepo.updateJuniorCount(superiorId, (int) userRepo.countBySuperiorId(superiorId));
+    }
 }

+ 56 - 0
src/main/java/com/izouma/awesomeAdmin/web/TelegramBot.java

@@ -0,0 +1,56 @@
+package com.izouma.awesomeAdmin.web;
+
+import com.izouma.awesomeAdmin.service.TelegramBotConfig;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.stereotype.Service;
+import org.telegram.telegrambots.bots.TelegramLongPollingBot;
+import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
+import org.telegram.telegrambots.meta.api.objects.Chat;
+import org.telegram.telegrambots.meta.api.objects.MessageEntity;
+import org.telegram.telegrambots.meta.api.objects.Update;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
+
+@EnableConfigurationProperties(TelegramBotConfig.class)
+@Service
+@Slf4j
+public class TelegramBot extends TelegramLongPollingBot {
+
+    private final TelegramBotConfig config;
+
+    public TelegramBot(TelegramBotConfig config) {
+        super(config.getToken());
+        this.config = config;
+    }
+
+    @Override
+    public void onUpdateReceived(Update update) {
+        if (update.hasMessage() && update.getMessage().hasText()) {
+            String text = update.getMessage().getText();
+            log.info("Received message: {}", text);
+            Chat chat = update.getMessage().getChat();
+            log.info("Chat: id={} type={} title={}", chat.getId(), chat.getType(), chat.getTitle());
+            if (update.getMessage().getEntities() != null) {
+                for (MessageEntity entity : update.getMessage().getEntities()) {
+                    log.info("Entity: type={} text={}", entity.getType(), entity.getText());
+                }
+            }
+
+            SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields
+            message.setChatId(update.getMessage().getChatId().toString());
+            message.setText("hello");
+
+            try {
+                execute(message); // Call method to send the message
+            } catch (TelegramApiException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    @Override
+    public String getBotUsername() {
+        return config.getUsername();
+    }
+
+}

+ 1 - 3
src/main/java/com/izouma/awesomeAdmin/web/UserController.java

@@ -244,9 +244,7 @@ public class UserController extends BaseController {
     @PreAuthorize("hasRole('ADMIN')")
     @GetMapping("/setSuperior")
     public void clearAliAuth(@RequestParam Long userId, @RequestParam Long superiorId) {
-        User user = userRepo.findById(userId).orElseThrow(new BusinessException(Translator.toLocale("user.not_found")));
-        user.setSuperiorId(superiorId);
-        userRepo.save(user);
+        userService.setSuperior(userId, superiorId);
     }
 
     @PreAuthorize("hasRole('ADMIN')")

+ 3 - 0
src/main/resources/application.yaml

@@ -120,6 +120,9 @@ cloudpay:
   mer-no: da736134
   secret: 676185c0fd5852f67d2b999c845cc3a5
   notify-url: https://paimaide.izouma.com/cloudPay/notify
+tg-bot:
+  token: 5737803982:AAHCXKUQVH9GQXRq8znw6k19Kht0RnDxNSE
+  username: firstcashbot
 ---
 
 spring: