Search in sources :

Example 1 with WonTelegramBotHandler

use of won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler 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());
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BotBehaviour(won.bot.framework.eventbot.behaviour.BotBehaviour) ConnectBehaviour(won.bot.framework.eventbot.behaviour.ConnectBehaviour) TelegramMessageReceivedAction(won.bot.framework.eventbot.action.impl.telegram.receive.TelegramMessageReceivedAction) TelegramBotsApi(org.telegram.telegrambots.TelegramBotsApi) ConnectionMessageBehaviour(won.bot.framework.eventbot.behaviour.ConnectionMessageBehaviour) TelegramApiRequestException(org.telegram.telegrambots.exceptions.TelegramApiRequestException) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) WonTelegramBotHandler(won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler) CloseBevahiour(won.bot.framework.eventbot.behaviour.CloseBevahiour)

Example 2 with WonTelegramBotHandler

use of won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler 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)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 WonTelegramBotHandler (won.bot.framework.eventbot.action.impl.telegram.WonTelegramBotHandler)2 URI (java.net.URI)1 Dataset (org.apache.jena.query.Dataset)1 Model (org.apache.jena.rdf.model.Model)1 TelegramBotsApi (org.telegram.telegrambots.TelegramBotsApi)1 AnswerCallbackQuery (org.telegram.telegrambots.api.methods.AnswerCallbackQuery)1 EditMessageReplyMarkup (org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup)1 CallbackQuery (org.telegram.telegrambots.api.objects.CallbackQuery)1 Message (org.telegram.telegrambots.api.objects.Message)1 Update (org.telegram.telegrambots.api.objects.Update)1 TelegramApiRequestException (org.telegram.telegrambots.exceptions.TelegramApiRequestException)1 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)1 TelegramMessageReceivedAction (won.bot.framework.eventbot.action.impl.telegram.receive.TelegramMessageReceivedAction)1 TelegramContentExtractor (won.bot.framework.eventbot.action.impl.telegram.util.TelegramContentExtractor)1 BotBehaviour (won.bot.framework.eventbot.behaviour.BotBehaviour)1 CloseBevahiour (won.bot.framework.eventbot.behaviour.CloseBevahiour)1 ConnectBehaviour (won.bot.framework.eventbot.behaviour.ConnectBehaviour)1