Example usage for org.apache.commons.lang3 StringEscapeUtils escapeJson

List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeJson

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringEscapeUtils escapeJson.

Prototype

public static final String escapeJson(final String input) 

Source Link

Document

Escapes the characters in a String using Json String rules.

Escapes any values it finds into their Json String form.

Usage

From source file:sx.blah.discord.DiscordClient.java

/**
 * Sends a message to the specified channel.
 *
 * @param content   The actual message to send
 * @param channelID The channel to send the message to
 * @return The message that was sent.//  w  w  w.j  a v a2  s  .c  o m
 * @throws IOException
 * @throws ParseException
 */
public Message sendMessage(String content, String channelID) throws IOException, ParseException {
    if (null != ws) {

        content = StringEscapeUtils.escapeJson(content);

        try {
            String response = Requests.POST.makeRequest(DiscordEndpoints.CHANNELS + channelID + "/messages",
                    new StringEntity("{\"content\":\"" + content + "\",\"mentions\":[]}", "UTF-8"),
                    new BasicNameValuePair("authorization", token),
                    new BasicNameValuePair("content-type", "application/json"));

            JSONObject object1 = (JSONObject) JSON_PARSER.parse(response);
            String time = (String) object1.get("timestamp");
            String messageID = (String) object1.get("id");

            Channel channel = getChannelByID(channelID);
            Message message = new Message(messageID, content, this.ourUser, channel,
                    this.convertFromTimestamp(time));
            channel.addMessage(message); //Had to be moved here so that if a message is edited before the MESSAGE_CREATE event, it doesn't error
            DiscordClient.this.dispatcher.dispatch(new MessageSendEvent(message));
            return message;
        } catch (HTTP403Exception e) {
            Discord4J.logger.error("Received 403 error attempting to send message; is your login correct?");
            return null;
        }

    } else {
        Discord4J.logger.error("Bot has not signed in yet!");
        return null;
    }
}

From source file:sx.blah.discord.DiscordClient.java

/**
  * Edits a specified message. Currently, Discord only allows you to edit your own message
  * //  w w w.  j av  a 2s.  c  o  m
  * @param content   The new content of the message
  * @param messageID The id of the message to edit
  * @param channelID The channel the message exists in
  * @throws ParseException
  */
public void editMessage(String content, String messageID, String channelID) throws ParseException {
    if (null != ws) {

        content = StringEscapeUtils.escapeJson(content);

        Channel channel = getChannelByID(channelID);
        if (channel == null) {
            Discord4J.logger.error("Channel id " + channelID + " doesn't exist!");
            return;
        }

        Message oldMessage = channel.getMessageByID(messageID);

        try {
            String response = Requests.PATCH.makeRequest(
                    DiscordEndpoints.CHANNELS + channelID + "/messages/" + messageID,
                    new StringEntity("{\"content\":\"" + content + "\", \"mentions\":[]}", "UTF-8"),
                    new BasicNameValuePair("authorization", token),
                    new BasicNameValuePair("content-type", "application/json"));

            JSONObject object1 = (JSONObject) JSON_PARSER.parse(response);

            Message newMessage = new Message((String) object1.get("id"), content, this.ourUser,
                    getChannelByID(channelID), oldMessage.getTimestamp());
            //Event dispatched here because otherwise there'll be an NPE as for some reason when the bot edits a message,
            // the event chain goes like this:
            //Original message edited to null, then the null message edited to the new content
            DiscordClient.this.dispatcher.dispatch(new MessageUpdateEvent(oldMessage, newMessage));
            oldMessage.setContent(content);
        } catch (HTTP403Exception e) {
            Discord4J.logger.error("Received 403 error attempting to send message; is your login correct?");
        }

    } else {
        Discord4J.logger.error("Bot has not signed in yet!");
    }
}