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

com.viber.bot.message.MessageKeyboard 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.message;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.ForwardingMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
/**
 * Simple Map(String, Object) wrapper class.
 * To create a MessageKeyboard you can either create a Map yourself and pass it in the ctor, or parse a string like the following:
 * String keyboardMessageStr = JSONString;
 * MessageKeyboard messageKeyboard = new MessageKeyboard(new ObjectMapper().readValue(keyboardMessageStr, new TypeReference.Map.String, Object..(){}));
 */
@Immutable
@JsonDeserialize(using = MessageKeyboard.KeyboardDeserializer.class)
public class MessageKeyboard extends ForwardingMap {
    private final Map map;

    public MessageKeyboard(final @Nullable Map delegate) {
        this.map = Collections.unmodifiableMap(MoreObjects.firstNonNull(delegate, Collections.emptyMap()));
    }

    public MessageKeyboard() {
        this(null);
    }

    @Override
    protected Map delegate() {
        return map;
    }

    static class KeyboardDeserializer extends JsonDeserializer {
        private static final Logger logger = LoggerFactory.getLogger(KeyboardDeserializer.class);
        private static final ObjectMapper objectMapper = new ObjectMapper();
        private static final String EMPTY_JSON_OBJECT = "{}";

        @Override
        public MessageKeyboard deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
            Map messageKeyboard = null;
            try {
                messageKeyboard = objectMapper.readValue(MoreObjects.firstNonNull(
                        Strings.emptyToNull(p.getValueAsString().trim()), EMPTY_JSON_OBJECT), Map.class);
            } catch (JsonMappingException | JsonParseException exception) {
                logger.warn("Could not deserialize message keyboard '{}'", p.getValueAsString().trim());
            }
            return new MessageKeyboard(messageKeyboard);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy