Skip to content

Commit c324526

Browse files
committed
Add DugaStopEvent
1 parent b43d777 commit c324526

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

duga-core/src/main/java/net/zomis/duga/chat/StackExchangeChatBot.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import net.zomis.duga.chat.events.DugaEvent;
1212
import net.zomis.duga.chat.events.DugaStartedEvent;
13+
import net.zomis.duga.chat.events.DugaStopEvent;
1314
import org.apache.http.HttpRequest;
1415
import org.apache.http.HttpResponse;
1516
import org.apache.http.ProtocolException;
@@ -303,10 +304,7 @@ private ChatMessageResponse postMessageToChat(final ChatMessage message) {
303304
}
304305

305306
public void stop() {
306-
if (!this.undeployGoodbyeText.isEmpty()) {
307-
// TODO: Use specific chat parameters
308-
postMessages(null, Collections.singletonList(this.undeployGoodbyeText));
309-
}
307+
this.executeEvent(new DugaStopEvent(this));
310308
this.executorService.shutdown();
311309
}
312310

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.zomis.duga.chat.events;
2+
3+
import net.zomis.duga.chat.ChatBot;
4+
5+
public class DugaStopEvent extends DugaEvent {
6+
7+
public DugaStopEvent(ChatBot bot) {
8+
super(bot);
9+
}
10+
11+
}

duga-core/src/test/java/net/zomis/duga/DugaTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import net.zomis.duga.chat.StackExchangeChatBot;
66
import net.zomis.duga.chat.WebhookParameters;
77
import net.zomis.duga.chat.events.DugaStartedEvent;
8+
import net.zomis.duga.chat.events.DugaStopEvent;
89

9-
import java.io.File;
1010
import java.io.FileReader;
1111
import java.io.IOException;
12-
import java.util.Arrays;
13-
import java.util.Collections;
1412
import java.util.Properties;
1513
import java.util.Scanner;
1614

1715
public class DugaTest {
1816

17+
private static final WebhookParameters room = WebhookParameters.toRoom("16134");
18+
1919
public static void main(String[] args) {
2020

2121
BotConfiguration config = new BotConfiguration();
@@ -33,22 +33,26 @@ public static void main(String[] args) {
3333
}
3434

3535
System.out.println("Using email " + config.getBotEmail());
36-
System.out.println("With password " + config.getBotPassword());
3736

3837
config.setChatThrottle(10000);
3938
config.setChatMaxBurst(2);
4039
config.setChatMinimumDelay(500);
4140
ChatBot bot = new StackExchangeChatBot(config);
4241
bot.registerListener(DugaStartedEvent.class, DugaTest::interactive);
42+
bot.registerListener(DugaStopEvent.class, DugaTest::shutdown);
4343
System.out.println("Starting bot...");
4444
bot.start();
4545
}
4646

47+
private static void shutdown(DugaStopEvent event) {
48+
event.getBot().postNow(room.message("Shutting down!"));
49+
}
50+
4751
private static void interactive(DugaStartedEvent event) {
4852
System.out.println("Bot started and ready.");
4953
Scanner scanner = new Scanner(System.in);
5054
ChatBot bot = event.getBot();
51-
WebhookParameters room = WebhookParameters.toRoom("16134");
55+
bot.postNow(room.message("Hello World!"));
5256
while (true) {
5357
String input = scanner.nextLine();
5458
if (input.isEmpty()) {

grails-app/services/net/zomis/duga/DugaBotService.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import net.zomis.duga.chat.ChatMessage
66
import net.zomis.duga.chat.ChatMessageResponse
77
import net.zomis.duga.chat.StackExchangeChatBot
88
import net.zomis.duga.chat.WebhookParameters
9+
import net.zomis.duga.chat.events.DugaEvent
910
import org.springframework.beans.factory.InitializingBean
1011
import org.springframework.beans.factory.annotation.Autowired
1112
import org.springframework.core.env.Environment
1213

1314
import java.util.concurrent.Future
15+
import java.util.function.Consumer
1416

1517
class DugaBotService implements ChatBot, InitializingBean {
1618

@@ -48,6 +50,11 @@ class DugaBotService implements ChatBot, InitializingBean {
4850
bot.stop()
4951
}
5052

53+
@Override
54+
def <E extends DugaEvent> void registerListener(Class<E> eventClass, Consumer<E> handler) {
55+
bot.registerListener(eventClass, handler)
56+
}
57+
5158
@Override
5259
Future<List<ChatMessageResponse>> postChat(WebhookParameters params, List<String> messages) {
5360
messages.each {

0 commit comments

Comments
 (0)