All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viber.bot.api.RequestReceiverImpl Maven / Gradle / Ivy

Go to download

Use this library to communicate with the Viber API to develop a bot for https://developers.viber.com/.

The newest version!
package com.viber.bot.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.util.concurrent.Futures;
import com.viber.bot.Request;
import com.viber.bot.Response;
import com.viber.bot.event.EventEmitter;
import com.viber.bot.event.incoming.*;
import com.viber.bot.message.Message;
import com.viber.bot.middleware.RequestReceiver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.function.Consumer;

import static com.google.common.base.Preconditions.checkNotNull;

class RequestReceiverImpl implements RequestReceiver {

    private static final Logger logger = LoggerFactory.getLogger(RequestReceiverImpl.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();

    private final EventEmitter eventEmitter;
    private final ViberBot bot;

    RequestReceiverImpl(final @Nonnull ViberBot bot, final @Nonnull EventEmitter eventEmitter) {
        this.bot = checkNotNull(bot);
        this.eventEmitter = checkNotNull(eventEmitter);
    }

    @Override
    public void acceptRequest(final @Nonnull Request request) {
        final IncomingEvent incomingEvent = request.getEvent();
        switch (incomingEvent.getEvent()) {
            case ERROR: {
                logger.error("Error from Viber servers: {}", ((IncomingErrorEvent) request.getEvent()).getDescription());
                break;
            }

            case CONVERSATION_STARTED: {
                final IncomingConversationStartedEvent incomingConversationStartedEvent = (IncomingConversationStartedEvent) request.getEvent();
                eventEmitter.>emit(incomingEvent.getEvent(), incomingEvent)
                        .forEach(setReturnedResponse(request, new MessageDestination(incomingConversationStartedEvent.getUser())));
                break;
            }

            case MESSAGE_RECEIVED: {
                final IncomingMessageEvent incomingMessageEvent = (IncomingMessageEvent) request.getEvent();
                final Response response = new Response(new MessageDestination(incomingMessageEvent.getSender(), incomingMessageEvent.getChatId(), incomingMessageEvent.getReplyType()),  bot);
                eventEmitter.emit(incomingEvent.getEvent(), incomingEvent, incomingMessageEvent.getMessage(), response);
                break;
            }

            case SUBSCRIBED: {
                final IncomingSubscribedEvent incomingSubscribedEvent = (IncomingSubscribedEvent) request.getEvent();
                final Response response = new Response(new MessageDestination(incomingSubscribedEvent.getUser()), bot);
                eventEmitter.emit(incomingEvent.getEvent(), incomingEvent, response);
                break;
            }

            default: {
                eventEmitter.emit(incomingEvent.getEvent(), incomingEvent);
                break;
            }
        }
    }

    private Consumer>> setReturnedResponse(final Request request, final MessageDestination userProfile) {
        return messageFuture -> {

            final Optional message = Futures.getUnchecked(messageFuture);
            if (!message.isPresent()) return;

            try {
                final String json = objectMapper.writeValueAsString(getMessageMapping(userProfile, message.get()));
                request.setResponse(json);
            } catch (final Exception e) {
                logger.error("Could not send back response to conversation started event", e);
            }
        };
    }

    private Map getMessageMapping(final MessageDestination userProfile, final Message message) {
        return MessageToMapConverter.mapMessage(
                bot.getBotProfile(), userProfile, message,
                Optional.ofNullable(message.getKeyboard()),
                Optional.ofNullable(message.getTrackingData()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy