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());
}
}
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));
}
}
}
Aggregations