8
8
import com .pengrad .telegrambot .model .PhotoSize ;
9
9
import com .pengrad .telegrambot .model .Update ;
10
10
import com .pengrad .telegrambot .model .request .ParseMode ;
11
+ import com .pengrad .telegrambot .request .DeleteMessage ;
11
12
import com .pengrad .telegrambot .request .GetFile ;
12
13
import com .pengrad .telegrambot .request .GetUpdates ;
13
14
import com .pengrad .telegrambot .request .SendMessage ;
20
21
import java .util .Comparator ;
21
22
import java .util .List ;
22
23
import java .util .Objects ;
24
+ import java .util .regex .Pattern ;
23
25
import java .util .stream .Collectors ;
24
26
import javax .imageio .ImageIO ;
25
27
@@ -42,13 +44,37 @@ public void setAdminId(long adminId) {
42
44
43
45
public void run () {
44
46
bot .setUpdatesListener (updates -> {
47
+ processAdminCommands (updates );
45
48
processUpdates (updates );
46
49
return UpdatesListener .CONFIRMED_UPDATES_ALL ;
47
50
});
48
51
}
49
52
50
53
public void runOnce () {
51
- processUpdates (bot .execute (new GetUpdates ()).updates ());
54
+ final var updates = bot .execute (new GetUpdates ()).updates ();
55
+ processAdminCommands (updates );
56
+ processUpdates (updates );
57
+ }
58
+
59
+ private void processAdminCommands (List <Update > updates ) {
60
+ final var delPattern = Pattern .compile ("/del(\\ d+)m(\\ d+)" );
61
+ updates .stream ()
62
+ .map (Update ::message )
63
+ .filter (Objects ::nonNull )
64
+ .filter (msg -> msg .chat ().id () == adminId )
65
+ .map (Message ::text )
66
+ .filter (Objects ::nonNull )
67
+ .forEach (command -> {
68
+ final var m = delPattern .matcher (command );
69
+ if (m .find ()) {
70
+ final var channelId = Long .parseLong ("-100" + m .group (1 ));
71
+ final var messageId = Integer .parseInt (m .group (2 ));
72
+ bot .execute (new DeleteMessage (channelId , messageId ));
73
+ try {
74
+ indexer .deleteImage (channelId , messageId );
75
+ } catch (SQLException ignored ) {}
76
+ }
77
+ });
52
78
}
53
79
54
80
private void processUpdates (List <Update > updates ) {
@@ -84,10 +110,14 @@ private List<Message> getChannelPostsWithPhotos(List<Update> updates) {
84
110
85
111
private void sendReport (List <SimilarImagesInfo > infos ) {
86
112
String report = infos .stream ().map (info -> {
87
- String text = "For post " + formatPostLink (info .getOriginalPost ()) + " found:\n " ;
113
+ final var post = info .getOriginalPost ();
114
+ String text = "For post " + formatPostLink (post ) + " found:\n " ;
88
115
text += info .getResults ().stream ()
89
116
.map (r -> String .format (" %s, dst: %.2f" , formatPostLink (r .getPost ()), r .getDistance ()))
90
117
.collect (Collectors .joining ("\n " ));
118
+ text += String .format ("%n/del%sm%d" ,
119
+ post .getChannelId ().toString ().replace ("-100" , "" ),
120
+ post .getMessageId ());
91
121
return text ;
92
122
}).collect (Collectors .joining ("\n \n " ));
93
123
0 commit comments