dreamworker

Untitled

Sep 13th, 2020 (edited)
1,732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. @Service
  2. public class Telebot {
  3.     private final String API_KEY = "####################:####################################";
  4.     private final Long GROUP_ID = -############;
  5.     private TelegramBot bot;
  6.  
  7.     private final Map<String, BiConsumer<Long, String>> handlers = new ConcurrentHashMap<>();
  8.  
  9.  
  10.     @PostConstruct
  11.     private void postConstruct() {
  12.         log.info("Staring telegram bot ...");
  13.  
  14.         //assign handlers
  15.         handlers.put("/help", this::help);
  16.         handlers.put("/echo", this::echo);
  17.         handlers.put("/ping", this::ping);
  18.  
  19.  
  20.         bot = new TelegramBot.Builder(API_KEY).build();
  21.  
  22.         bot.setUpdatesListener(updates -> {
  23.  
  24.             // process updates
  25.             for (Update update : updates) {
  26.  
  27.                 try {
  28.  
  29.  
  30.                     Long chatId = update.message().chat().id();
  31.                     String text = update.message().text();
  32.  
  33.                     if (isEmpty(trimAllWhitespace(text))) {
  34.                         continue;
  35.                     }
  36.  
  37.                     String[] commandArray = text.split("[ @]");
  38.                     if (commandArray.length > 0) {
  39.  
  40.                         String command = commandArray[0];
  41.  
  42.                         if (handlers.containsKey(command)) {
  43.                             String body = trimWhitespace(text.substring(command.length(), text.length()));
  44.  
  45.                             handlers.get(command).accept(chatId, body);
  46.                         }
  47.                     }
  48.  
  49.                 }
  50.                 catch (Exception e) {
  51.                     log.error("bot receive/handle message error: ", e);
  52.                 }
  53.             }
  54.  
  55.             // return id of last processed update or confirm them all
  56.             return UpdatesListener.CONFIRMED_UPDATES_ALL;
  57.         });
  58.  
  59.     }
  60.  
  61.     // TELEGRAM BOT HANDLERS ===========================================
  62.  
  63.  
  64.  
  65.     private void help(Long chatId, String text) {
  66.  
  67.         String resp =
  68.             "/help - this help" +
  69.                 "\n" + "/echo [text] - echo [text]" +
  70.                 "\n" + "/ping - echo-reply";
  71.  
  72.         SendResponse response = bot.execute(new SendMessage(chatId, resp));
  73.  
  74.     }
  75.  
  76.     private void echo(Long chatId, String text) {
  77.  
  78.         SendResponse response = bot.execute(new SendMessage(chatId, text));
  79.  
  80.     }
  81.  
  82.     private void ping(Long chatId, String text) {
  83.  
  84.         SendResponse response = bot.execute(new SendMessage(chatId, "pong"));
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment