use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.
the class SendChattyMessageAction method doRun.
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
Set<URI> toRemove = null;
Collection<Object> chattyConnections = getEventListenerContext().getBotContext().loadObjectMap(KEY_CHATTY_CONNECTIONS).values();
if (chattyConnections == null)
return;
theloop: for (Object o : chattyConnections) {
URI conURI = (URI) o;
if (random.nextDouble() > probabilityOfSendingMessage) {
continue;
}
// determine which kind of message to send depending on inactivity of partner.
MessageTimingManager.InactivityPeriod inactivityPeriod = messageTimingManager.getInactivityPeriodOfPartner(conURI);
// don't send a chatty message when we just sent one
if (!this.messageTimingManager.isWaitedLongEnough(conURI)) {
continue;
}
String message = null;
switch(inactivityPeriod) {
case ACTIVE:
// do not send a message
continue theloop;
case SHORT:
message = getRandomMessage(this.messagesForShortInactivity);
break;
case LONG:
message = getRandomMessage(this.messagesForLongInactivity);
break;
case TOO_LONG:
if (toRemove == null)
toRemove = new HashSet<URI>();
toRemove.add(conURI);
message = "Ok, you've been absent for a while now. I will stop bugging you. If you want me to resume " + "doing that, say 'chatty on'. For more information, say 'usage'";
break;
}
// publish an event that causes the message to be sent
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(conURI);
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
if (con != null) {
Model messageModel = WonRdfUtils.MessageUtils.textMessage(message);
getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(con, messageModel));
} else {
logger.warn("could not send chatty message on connection {} - failed to generate Connection object from RDF", conURI);
}
}
if (toRemove != null) {
for (URI uri : toRemove) {
getEventListenerContext().getBotContext().removeFromObjectMap(KEY_CHATTY_CONNECTIONS, uri.toString());
}
}
}
use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.
the class SendMessageOnCrawlResultAction method onCrawlSuccess.
@Override
protected final void onCrawlSuccess(CrawlConnectionCommandSuccessEvent successEvent) {
Model messageModel = makeSuccessMessage(successEvent);
if (messageModel == null)
return;
getEventListenerContext().getEventBus().publish(new ConnectionMessageCommandEvent(con, messageModel));
}
use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.
the class MailCommandAction method processReferenceMailCommands.
private void processReferenceMailCommands(MimeMessage message, WonURI wonUri) {
MailBotContextWrapper botContextWrapper = ((MailBotContextWrapper) getEventListenerContext().getBotContextWrapper());
EventBus bus = getEventListenerContext().getEventBus();
try {
if (wonUri == null) {
throw new NullPointerException("No corresponding wonUri found");
}
URI needUri;
URI remoteNeedUri = null;
Dataset connectionRDF = null;
switch(wonUri.getType()) {
case CONNECTION:
connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(wonUri.getUri());
needUri = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, wonUri.getUri());
remoteNeedUri = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, wonUri.getUri());
break;
case NEED:
default:
needUri = wonUri.getUri();
break;
}
MimeMessage originalMessage = botContextWrapper.getMimeMessageForURI(needUri);
if (originalMessage == null) {
throw new NullPointerException("no originalmessage found");
}
logger.debug("Validate mailorigin with originalmail:");
logger.debug("Command Message Sender: " + message.getFrom());
logger.debug("Original Message Sender: " + originalMessage.getFrom());
String senderNew = ((InternetAddress) message.getFrom()[0]).getAddress();
String senderOriginal = ((InternetAddress) originalMessage.getFrom()[0]).getAddress();
if (!senderNew.equals(senderOriginal)) {
throw new AccessControlException("Sender of original and command mail are not equal");
} else {
logger.debug("Sender of original and command mail are not equal, continue with command processing");
}
ActionType actionType = determineAction(getEventListenerContext(), message, wonUri);
logger.debug("Executing " + actionType + " on uri: " + wonUri.getUri() + " of type " + wonUri.getType());
Connection con;
switch(actionType) {
case CLOSE_CONNECTION:
con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
bus.publish(new CloseCommandEvent(con));
break;
case OPEN_CONNECTION:
bus.publish(new ConnectCommandEvent(needUri, remoteNeedUri));
break;
case IMPLICIT_OPEN_CONNECTION:
bus.publish(new ConnectCommandEvent(needUri, remoteNeedUri, mailContentExtractor.getTextMessage(message)));
break;
case SENDMESSAGE:
con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
Model messageModel = WonRdfUtils.MessageUtils.textMessage(mailContentExtractor.getTextMessage(message));
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
break;
case CLOSE_NEED:
bus.publish(new DeactivateNeedCommandEvent(needUri));
break;
case NO_ACTION:
default:
// INVALID COMMAND
logger.error("No command was given or assumed");
break;
}
} catch (AccessControlException ace) {
logger.error("ACCESS RESTRICTION: sender of original and command mail are not equal, command will be blocked");
} catch (Exception e) {
logger.error("no reply mail was set or found: " + e.getMessage());
}
}
use of won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent in project webofneeds by researchstudio-sat.
the class TelegramMessageReceivedAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventBus bus = getEventListenerContext().getEventBus();
EventListenerContext ctx = getEventListenerContext();
if (event instanceof TelegramMessageReceivedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
Update update = ((TelegramMessageReceivedEvent) event).getUpdate();
Message message = update.getMessage();
CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null && message.isCommand()) {
wonTelegramBotHandler.getCommandRegistry().executeCommand(wonTelegramBotHandler, message);
} else if (callbackQuery != null && update.hasCallbackQuery()) {
message = callbackQuery.getMessage();
String data = callbackQuery.getData();
WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getMessageId());
AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery();
answerCallbackQuery.setCallbackQueryId(callbackQuery.getId());
switch(correspondingURI.getType()) {
case NEED:
break;
case CONNECTION:
if ("0".equals(data)) {
// CLOSE CONNECTION
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
bus.publish(new CloseCommandEvent(con));
answerCallbackQuery.setText("Closed Connection");
} else if ("1".equals(data)) {
// ACCEPT CONNECTION
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
URI remoteNeed = WonRdfUtils.ConnectionUtils.getRemoteNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
URI localNeed = WonRdfUtils.ConnectionUtils.getLocalNeedURIFromConnection(connectionRDF, correspondingURI.getUri());
bus.publish(new ConnectCommandEvent(localNeed, remoteNeed));
answerCallbackQuery.setText("Opened Connection");
}
break;
}
wonTelegramBotHandler.answerCallbackQuery(answerCallbackQuery);
EditMessageReplyMarkup editMessageReplyMarkup = new EditMessageReplyMarkup();
editMessageReplyMarkup.setMessageId(message.getMessageId());
editMessageReplyMarkup.setChatId(message.getChatId());
editMessageReplyMarkup.setReplyMarkup(null);
wonTelegramBotHandler.editMessageReplyMarkup(editMessageReplyMarkup);
} else if (message != null && message.isReply() && message.hasText()) {
WonURI correspondingURI = botContextWrapper.getWonURIForMessageId(message.getReplyToMessage().getMessageId());
Dataset connectionRDF = getEventListenerContext().getLinkedDataSource().getDataForResource(correspondingURI.getUri());
Connection con = RdfUtils.findFirst(connectionRDF, x -> new ConnectionModelMapper().fromModel(x));
Model messageModel = WonRdfUtils.MessageUtils.textMessage(message.getText());
bus.publish(new ConnectionMessageCommandEvent(con, messageModel));
}
}
}
Aggregations