Search in sources :

Example 46 with Connection

use of won.protocol.model.Connection 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));
        }
    }
}
Also used : AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) Message(org.telegram.telegrambots.api.objects.Message) CallbackQuery(org.telegram.telegrambots.api.objects.CallbackQuery) Connection(won.protocol.model.Connection) Update(org.telegram.telegrambots.api.objects.Update) EventBus(won.bot.framework.eventbot.bus.EventBus) Model(org.apache.jena.rdf.model.Model) WonTelegramBotHandler(won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler) TelegramMessageReceivedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramMessageReceivedEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) TelegramContentExtractor(won.bot.framework.eventbot.action.impl.telegram.util.TelegramContentExtractor) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) WonRdfUtils(won.protocol.util.WonRdfUtils) Event(won.bot.framework.eventbot.event.Event) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) RdfUtils(won.protocol.util.RdfUtils) EventListener(won.bot.framework.eventbot.listener.EventListener) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) EditMessageReplyMarkup(org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) CallbackQuery(org.telegram.telegrambots.api.objects.CallbackQuery) AnswerCallbackQuery(org.telegram.telegrambots.api.methods.AnswerCallbackQuery) Message(org.telegram.telegrambots.api.objects.Message) Dataset(org.apache.jena.query.Dataset) Connection(won.protocol.model.Connection) EventBus(won.bot.framework.eventbot.bus.EventBus) CloseCommandEvent(won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent) Update(org.telegram.telegrambots.api.objects.Update) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) EditMessageReplyMarkup(org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup) Model(org.apache.jena.rdf.model.Model) ConnectionModelMapper(won.protocol.model.ConnectionModelMapper) TelegramMessageReceivedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramMessageReceivedEvent) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)

Example 47 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class Message2TelegramAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof MessageFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((MessageFromOtherNeedEvent) event).getCon();
        WonMessage wonMessage = ((MessageFromOtherNeedEvent) event).getWonMessage();
        URI yourNeedUri = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        Long chatId = botContextWrapper.getChatIdForURI(yourNeedUri);
        if (chatId == null) {
            logger.error("No chatId found for the specified needUri");
            return;
        }
        try {
            Message message = wonTelegramBotHandler.sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getConnectionTextMessage(chatId, remoteNeedUri, yourNeedUri, wonMessage));
            botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        } catch (TelegramApiException te) {
            logger.error(te.getMessage());
        }
    }
}
Also used : TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) Message(org.telegram.telegrambots.api.objects.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI)

Example 48 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class AbstractMessageCommandResultEvent method makeConnection.

protected static Connection makeConnection(URI needURI, URI remoteNeedURI, URI connectionURI) {
    Connection con = new Connection();
    con.setConnectionURI(connectionURI);
    con.setNeedURI(needURI);
    con.setRemoteNeedURI(remoteNeedURI);
    return con;
}
Also used : Connection(won.protocol.model.Connection)

Example 49 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class DatasetBackedOwnerCallbackAdapter method makeConnection.

@Override
protected Connection makeConnection(final WonMessage wonMessage) {
    URI connUri = wonMessage.getReceiverURI();
    ParameterizedSparqlString pss = new ParameterizedSparqlString();
    pss.setNsPrefix("won", WON.BASE_URI);
    pss.setCommandText(QUERY_CONNECTION);
    pss.setIri("con", connUri.toString());
    Query query = pss.asQuery();
    QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
    qExec.getContext().set(TDB.symUnionDefaultGraph, true);
    try {
        Connection con = null;
        final ResultSet results = qExec.execSelect();
        if (results.hasNext()) {
            QuerySolution soln = results.next();
            if (results.hasNext()) {
                throw new DataIntegrityException("Query must not yield multiple solutions");
            }
            con = new Connection();
            con.setConnectionURI(getURIFromSolution(soln, "con"));
            con.setTypeURI(getURIFromSolution(soln, "type"));
            con.setNeedURI(getURIFromSolution(soln, "need"));
            con.setState(ConnectionState.fromURI(getURIFromSolution(soln, "state")));
            con.setRemoteNeedURI(getURIFromSolution(soln, "remoteNeed"));
            con.setRemoteConnectionURI(getURIFromSolution(soln, "remoteCon"));
        }
        return con;
    } finally {
        if (!qExec.isClosed()) {
            qExec.close();
        }
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) DataIntegrityException(won.protocol.exception.DataIntegrityException) Connection(won.protocol.model.Connection) ResultSet(org.apache.jena.query.ResultSet) ParameterizedSparqlString(org.apache.jena.query.ParameterizedSparqlString) URI(java.net.URI) QueryExecution(org.apache.jena.query.QueryExecution)

Example 50 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class MessageExtractingOwnerCallbackAdapter method toConnection.

/**
 * Creates a connection object representing the connection
 * that the wonMessage is addressed at, if any.
 * The resulting Connection object will not have a state or type property set.
 * @param wonMessage or null if the message is not directed at
 *                   a connection
 */
private Connection toConnection(WonMessage wonMessage) {
    Connection con = new Connection();
    con.setConnectionURI(wonMessage.getReceiverURI());
    con.setRemoteConnectionURI(wonMessage.getSenderURI());
    con.setNeedURI(wonMessage.getReceiverNeedURI());
    con.setRemoteNeedURI(wonMessage.getSenderNeedURI());
    return con;
}
Also used : Connection(won.protocol.model.Connection)

Aggregations

Connection (won.protocol.model.Connection)50 URI (java.net.URI)30 WonMessage (won.protocol.message.WonMessage)20 Message (org.apache.camel.Message)12 Model (org.apache.jena.rdf.model.Model)12 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)9 Dataset (org.apache.jena.query.Dataset)6 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)6 Resource (org.apache.jena.rdf.model.Resource)5 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)5 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)5 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)4 EventBus (won.bot.framework.eventbot.bus.EventBus)4 Event (won.bot.framework.eventbot.event.Event)4 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 WonRdfUtils (won.protocol.util.WonRdfUtils)4 Message (org.telegram.telegrambots.api.objects.Message)3 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)3 BaseNeedAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent)3