use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.
the class Telegram2WonBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
telegramMessageGenerator.setEventListenerContext(ctx);
bus = getEventBus();
// Initiate Telegram Bot Handler
ApiContextInitializer.init();
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
wonTelegramBotHandler = new WonTelegramBotHandler(bus, telegramMessageGenerator, botName, token);
logger.debug("botName: " + wonTelegramBotHandler.getBotUsername());
logger.debug("botTokn: " + wonTelegramBotHandler.getBotToken());
telegramBotsApi.registerBot(wonTelegramBotHandler);
BotBehaviour connectBehaviour = new ConnectBehaviour(ctx);
connectBehaviour.activate();
BotBehaviour closeBehaviour = new CloseBevahiour(ctx);
closeBehaviour.activate();
BotBehaviour connectionMessageBehaviour = new ConnectionMessageBehaviour(ctx);
connectionMessageBehaviour.activate();
// Telegram initiated Events
bus.subscribe(TelegramMessageReceivedEvent.class, new ActionOnEventListener(ctx, "TelegramMessageReceived", new TelegramMessageReceivedAction(ctx, wonTelegramBotHandler, telegramContentExtractor)));
bus.subscribe(SendHelpEvent.class, new ActionOnEventListener(ctx, "TelegramHelpAction", new TelegramHelpAction(ctx, wonTelegramBotHandler)));
bus.subscribe(TelegramCreateNeedEvent.class, new ActionOnEventListener(ctx, "TelegramCreateAction", new TelegramCreateAction(ctx, wonTelegramBotHandler, telegramContentExtractor)));
// WON initiated Events
bus.subscribe(HintFromMatcherEvent.class, new ActionOnEventListener(ctx, "HintReceived", new Hint2TelegramAction(ctx, wonTelegramBotHandler)));
bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, "ConnectReceived", new Connect2TelegramAction(ctx, wonTelegramBotHandler)));
bus.subscribe(MessageFromOtherNeedEvent.class, new ActionOnEventListener(ctx, "ReceivedTextMessage", new Message2TelegramAction(ctx, wonTelegramBotHandler)));
} catch (TelegramApiRequestException e) {
logger.error(e.getMessage());
}
}
use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.
the class SecurityBotTests method runBot.
private void runBot(Class botClass) throws ExecutionException, InterruptedException {
IntegrationtestBot bot = null;
// create a bot instance and auto-wire it
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
bot = (IntegrationtestBot) beanFactory.autowire(botClass, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
Object botBean = beanFactory.initializeBean(bot, "theBot");
bot = (IntegrationtestBot) botBean;
// the bot also needs a trigger so its act() method is called regularly.
// (there is no trigger bean in the context)
PeriodicTrigger trigger = new PeriodicTrigger(500, TimeUnit.MILLISECONDS);
trigger.setInitialDelay(100);
((TriggeredBot) bot).setTrigger(trigger);
// adding the bot to the bot manager will cause it to be initialized.
// at that point, the trigger starts.
botManager.addBot(bot);
final SettableListenableFuture<TestFinishedEvent> futureTestResult = new SettableListenableFuture();
final EventListenerContext ctx = bot.getExposedEventListenerContext();
// action for setting the future when we get a test result
EventBotAction setFutureAction = new SetFutureAction(ctx, futureTestResult);
// action for setting the future when an error occurs
EventBotAction setFutureFromErrorAction = new SetFutureFromErrorEventAction(ctx, futureTestResult, bot);
// add a listener for test success
ctx.getEventBus().subscribe(TestPassedEvent.class, new ActionOnEventListener(ctx, setFutureAction));
// add a listener for test failure
ctx.getEventBus().subscribe(TestFailedEvent.class, new ActionOnEventListener(ctx, setFutureAction));
// add a listener for errors
ctx.getEventBus().subscribe(ErrorEvent.class, new ActionOnEventListener(ctx, setFutureFromErrorAction));
// wait for the result
TestFinishedEvent result = futureTestResult.get();
if (result instanceof TestFailedEvent) {
Assert.fail(((TestFailedEvent) result).getMessage());
}
}
use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.
the class DuplicateMessageSendingConversationBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getDuplicateMessageSenderDecorator(getEventListenerContext());
final EventBus bus = getEventBus();
// we're not expecting any failure messages in this test:
bus.subscribe(FailureResponseEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
FailureResponseEvent failureResponseEvent = (FailureResponseEvent) event;
bus.publish(new TestFailedEvent(DuplicateMessageSendingConversationBot.this, "Message failed: " + failureResponseEvent.getOriginalMessageURI() + ": " + WonRdfUtils.MessageUtils.getTextMessage(failureResponseEvent.getFailureMessage())));
}
}));
// create needs every trigger execution until 2 needs are created
bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS));
// connect needs
bus.subscribe(NeedCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_NEEDS * 2, new ConnectFromListToListAction(ctx, getBotContextWrapper().getNeedCreateListName(), getBotContextWrapper().getNeedCreateListName(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!")));
// add a listener that is informed of the connect/open events and that auto-opens
// subscribe it to:
// * connect events - so it responds with open
// * open events - so it responds with open (if the open received was the first open, and we still need to accept the connection)
bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi!")));
// add a listener that auto-responds to messages by a message
// after 10 messages, it unsubscribes from all events
// subscribe it to:
// * message events - so it responds
// * open events - so it initiates the chain reaction of responses
BaseEventListener autoResponder = new AutomaticMessageResponderListener(ctx, NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
bus.subscribe(OpenFromOtherNeedEvent.class, autoResponder);
bus.subscribe(MessageFromOtherNeedEvent.class, autoResponder);
// add a listener that closes the connection after it has seen 10 messages
bus.subscribe(MessageFromOtherNeedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_MESSAGES, new CloseConnectionAction(ctx, "Bye!")));
// add a listener that closes the connection when a failureEvent occurs
EventListener onFailureConnectionCloser = new ActionOnEventListener(ctx, new CloseConnectionAction(ctx, "Bye!"));
bus.subscribe(FailureResponseEvent.class, onFailureConnectionCloser);
// add a listener that auto-responds to a close message with a deactivation of both needs.
// subscribe it to:
// * close events
bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, new DeactivateAllNeedsAction(ctx), new PublishEventAction(ctx, new TestPassedEvent(this))), 1));
// add a listener that counts two NeedDeactivatedEvents and then tells the
// framework that the bot's work is done
bus.subscribe(NeedDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new SignalWorkDoneAction(ctx)));
}
use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.
the class DuplicateMessageURIFailureBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
EventBus bus = getEventBus();
// create needs every trigger execution until 2 needs are created
bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(// use a decorator that will cause the same need URI to be used in each create message
new ConstantNewEventURIDecorator(ctx, "constantMsgURI" + System.currentTimeMillis()), getBotContextWrapper().getNeedCreateListName()), 2));
// log error if we can create 2 needs
bus.subscribe(NeedCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogErrorAction(ctx, "Should not have been able to create 2 needs using message with identical URIs"), new PublishEventAction(ctx, new TestFailedEvent(this, "Should not have been able to create 2 needs with identical URI")))));
// log success if we could create 1 need
bus.subscribe(NeedCreatedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: could create one need"), new PublishEventAction(ctx, new SuccessEvent()))));
// log success if we got an error for 2nd need
bus.subscribe(NeedCreationFailedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: need creation failed for 2nd need."), new PublishEventAction(ctx, new SuccessEvent()))));
// when we have 2 SuccessEvents, we're done. Deacivate the needs and signal we're finished
bus.subscribe(SuccessEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogAction(ctx, "Test passed."), new PublishEventAction(ctx, new TestPassedEvent(this)), new DeactivateAllNeedsAction(ctx))));
// when we have a FailureEvent, we're done, too. Deacivate the needs and signal we're finished
bus.subscribe(TestFailedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Test failed."), new DeactivateAllNeedsAction(ctx))));
// wait for the needDeactivated event, then say we're done.
bus.subscribe(NeedDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new SignalWorkDoneAction(ctx, this)));
// TODO: fix: bot runs forever even if test fails.
// TODO: fix: need 1 is not deactivated if test fails.
}
use of won.bot.framework.eventbot.EventListenerContext in project webofneeds by researchstudio-sat.
the class DuplicateNeedURIFailureBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
EventBus bus = getEventBus();
// create needs every trigger execution until 2 needs are created
bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(// use a decorator that will cause the same need URI to be used in each create message
new ConstantNewNeedURIDecorator(ctx, "constantNeedURI" + System.currentTimeMillis()), getBotContextWrapper().getNeedCreateListName()), 2));
// log error if we can create 2 needs
bus.subscribe(NeedCreatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogErrorAction(ctx, "Should not have been able to create 2 needs with identical URI"), new PublishEventAction(ctx, new TestFailedEvent(this, "Should not have been able to create 2 needs with identical URI")))));
// log success if we could create 1 need
bus.subscribe(NeedCreatedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: could create one need"), new PublishEventAction(ctx, new SuccessEvent()))));
// log success if we got an error for 2nd need
bus.subscribe(NeedCreationFailedEvent.class, new ActionOnFirstNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Good: need creation failed for 2nd need."), new PublishEventAction(ctx, new SuccessEvent()))));
// when we have 2 SuccessEvents, we're done. Deacivate the needs and signal we're finished
bus.subscribe(SuccessEvent.class, new ActionOnceAfterNEventsListener(ctx, 2, new MultipleActions(ctx, new LogAction(ctx, "Test passed."), new PublishEventAction(ctx, new TestPassedEvent(this)), new DeactivateAllNeedsAction(ctx))));
// when we have a FailureEvent, we're done, too. Deacivate the needs and signal we're finished
bus.subscribe(TestFailedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new MultipleActions(ctx, new LogAction(ctx, "Test failed."), new DeactivateAllNeedsAction(ctx))));
// wait for the needDeactivated event, then say we're done.
bus.subscribe(NeedDeactivatedEvent.class, new ActionOnceAfterNEventsListener(ctx, 1, new SignalWorkDoneAction(ctx, this)));
// TODO: fix: bot runs forever even if test fails.
// TODO: fix: need 1 is not deactivated if test fails.
}
Aggregations