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

net.mossol.bot.controller.MossolMessageController Maven / Gradle / Ivy

There is a newer version: 0.0.3.8
Show newest version
package net.mossol.bot.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.MediaType;
import com.linecorp.armeria.server.annotation.Path;
import com.linecorp.armeria.server.annotation.Post;
import com.linecorp.armeria.server.annotation.RequestObject;

import net.mossol.bot.util.MossolUtil;
import net.mossol.bot.model.ReplyMessage;
import net.mossol.bot.model.TextType;
import net.mossol.bot.service.MessageHandler;
import net.mossol.bot.util.MessageBuildUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class MossolMessageController {
    private static final Logger logger = LoggerFactory.getLogger(MossolMessageController.class);

    @Resource
    private MessageHandler messageHandler;

    @Post
    @Path("/getMessage")
    public HttpResponse getMessage(@RequestObject JsonNode request) {
        final String message = request.get("message").textValue();
        final Map ret = new HashMap<>();
        HttpResponse httpResponse;

        logger.info("request {}", request);

        ReplyMessage replyMessage;
        try {
            replyMessage = messageHandler.replyMessage(message);
            if (replyMessage == null) {
                logger.debug("INFO: there is no matching reply message");
                throw new Exception();
            }
        } catch (Exception e) {
            httpResponse = HttpResponse.of(HttpStatus.NOT_FOUND, MediaType.JSON_UTF_8, MossolUtil
                    .writeJsonString(Collections.emptyMap()));
            logger.debug("httpResponse <{}>", httpResponse);
            return httpResponse;
        }

        TextType type = replyMessage.getType();

        String response;
        switch(type) {
            case SELECT_MENU_K:
            case SELECT_MENU_J:
            case SELECT_MENU_D:
                final String foodMessage = MessageBuildUtil.sendFoodMessage(replyMessage.getLocationInfo());
                ret.put("message", foodMessage);
                response = MossolUtil.writeJsonString(ret);
                httpResponse = HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8, response);
                return httpResponse;
            case LEAVE_ROOM:
                break;
            default:
                ret.put("message", replyMessage.getText());
                response = MossolUtil.writeJsonString(ret);
                httpResponse = HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8, response);
                return httpResponse;
        }

        httpResponse = HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
        return httpResponse;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy