@@ -96,91 +96,4 @@ public InlineKeyboardMarkup createDescriptionOrganizationKeyboard(Context contex
9696 .build ()));
9797 return InlineKeyboardMarkup .builder ().keyboard (rowList ).build ();
9898 }
99-
100- @ Component
101- @ RequiredArgsConstructor
102- public static class TelegramBot extends TelegramLongPollingBot {
103-
104- private final ChatEditMessageTextFactory chatEditMessageTextFactory ;
105- private final ChatSendMessageFactory chatSendMessageFactory ;
106- private final Map <Long , Context > contextMap = new ConcurrentHashMap <>();
107-
108- @ Value ("${bot.username}" )
109- private String username ;
110-
111- @ Value ("${bot.token}" )
112- private String token ;
113-
114- @ Override
115- public String getBotUsername () {
116- return username ;
117- }
118-
119- @ Override
120- public String getBotToken () {
121- return token ;
122- }
123-
124- @ Override
125- public void onUpdateReceived (Update update ) {
126- if (update .hasMessage ()) {
127- try {
128- handleMessage (update .getMessage ());
129- } catch (TelegramApiException | JsonProcessingException e ) {
130- e .printStackTrace ();
131- }
132- }
133- if (update .hasCallbackQuery ()) {
134- try {
135- handleCallback (update .getCallbackQuery ());
136- } catch (TelegramApiException e ) {
137- e .printStackTrace ();
138- } catch (JsonProcessingException e ) {
139- throw new RuntimeException (e );
140- }
141- }
142- }
143-
144- private Context getContext (Message message ) {
145- return contextMap .get (message .getChatId ());
146- }
147-
148- private void handleCallback (CallbackQuery callbackQuery ) throws TelegramApiException , JsonProcessingException {
149-
150- Context context = getContext (callbackQuery .getMessage ());
151- if (!callbackQuery .getData ().isEmpty ()) {
152- context .setCallbackData (callbackQuery .getData ().split (":" )[1 ]);
153- if (Enums .getIfPresent (Stage .class , callbackQuery .getData ().split (":" )[0 ]).isPresent ()) {
154- context .setStage (Stage .valueOf (callbackQuery .getData ().split (":" )[0 ]));
155- }
156- execute (
157- chatEditMessageTextFactory .createEditMessageText (context , callbackQuery .getMessage ())
158- );
159- } else {
160- System .out .println ("callbackQuery is empty" );
161- }
162- }
163-
164- private void handleMessage (Message message ) throws TelegramApiException , JsonProcessingException {
165- contextMap .putIfAbsent (message .getFrom ().getId (), new Context (message .getFrom ().getId ()));
166-
167- Context context = getContext (message );
168-
169- if (message .isCommand ()) {
170- Optional <MessageEntity > commandEntity =
171- message .getEntities ().stream ().filter (e -> "bot_command" .equals (e .getType ())).findFirst ();
172- if (commandEntity .isPresent ()) {
173- String command =
174- message .getText ().substring (commandEntity .get ().getOffset (), commandEntity .get ().getLength ());
175- if (command .equals (Command .START .getValue ())) {
176- context .setStage (Stage .MAIN );
177- context .setMessageId (
178- execute (chatSendMessageFactory .createSendMessage (contextMap
179- .get (message .getFrom ().getId ()), message ))
180- .getMessageId ());
181- }
182- }
183- }
184- }
185- }
18699}
0 commit comments