Created
January 16, 2018 09:03
-
-
Save IgnatBeresnev/8d681eafab6551fba48bfa0779eb9a3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
@Component | |
public class GroupMessageHandler implements Handler { | |
private final Bot bot; | |
private final MessageService messageService; | |
@Autowired | |
public GroupMessageHandler(@Qualifier("bot") Bot bot, MessageService messageService) { | |
this.bot = bot; | |
this.messageService = messageService; | |
} | |
@Override | |
public void handle(@NotNull Update update) { | |
Message message = update.getMessage(); | |
logger.debug("Group message received: {}", message.toString()); | |
sendProvokeMessageIfEligible(message); | |
} | |
private void sendProvokeMessageIfEligible(Message message) { | |
if (messageService.isProvokeMessageEligible(message.getChat())) { | |
Optional<ProvokeMessage> provokeMessage = messageService.getProvokeMessage(); | |
if (provokeMessage.isPresent()) { | |
SendMessage botProvokeMessage = getSendMessageFor(message, provokeMessage.get()); | |
bot.execute(botProvokeMessage); | |
} | |
} | |
} | |
private SendMessage getSendMessageFor(Message message, ProvokeMessage provokeMessage) { | |
SendMessage sendMessage = new SendMessage() | |
.setChatId(getChatId(message)) | |
.setText(provokeMessage.message); | |
if (provokeMessage.type == MessageType.REPLY) { | |
int replyToMsgIdRandom = message.getMessageId() - randomIntWithLimit(4); | |
sendMessage.setReplyToMessageId(replyToMsgIdRandom); | |
} | |
return sendMessage; | |
} | |
private int randomIntWithLimit(int limit){ | |
return ThreadLocalRandom.current().nextInt(4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment