1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SDKを使用せずJavaとJSONObjectでLINE Messaging APIのテキストリンクメッセージを実装する方法

Posted at

SDKを使用せず、JSONObject`を使用してテキストリンクメッセージを作成する方法を説明します。

以下は、JSONObjectを使用してテキストリンクメッセージを作成し、LINEのMessaging APIに送信する例です:

import org.json.JSONObject;
import org.json.JSONArray;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class LineMessageSender {
    private static final String LINE_MESSAGING_API = "https://api.line.me/v2/bot/message/push";
    private static final String CHANNEL_ACCESS_TOKEN = "YOUR_CHANNEL_ACCESS_TOKEN";

    public static void sendTextLinkMessage(String userId) throws Exception {
        URL url = new URL(LINE_MESSAGING_API);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Authorization", "Bearer " + CHANNEL_ACCESS_TOKEN);
        con.setDoOutput(true);

        JSONObject message = createTextLinkMessage();
        JSONObject body = new JSONObject();
        body.put("to", userId);
        body.put("messages", new JSONArray().put(message));

        try(OutputStream os = con.getOutputStream()) {
            byte[] input = body.toString().getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        int responseCode = con.getResponseCode();
        System.out.println("Response Code : " + responseCode);

        con.disconnect();
    }

    private static JSONObject createTextLinkMessage() {
        JSONObject textLink = new JSONObject();
        textLink.put("type", "text");
        textLink.put("text", "ここをタップしてください");

        JSONObject action = new JSONObject();
        action.put("type", "postback");
        action.put("label", "タップ");
        action.put("displayText", "テキストがタップされました");
        action.put("data", "action=tapped");

        textLink.put("action", action);

        JSONObject flexMessage = new JSONObject();
        flexMessage.put("type", "flex");
        flexMessage.put("altText", "テキストリンクの例");

        JSONObject contents = new JSONObject();
        contents.put("type", "bubble");

        JSONObject body = new JSONObject();
        body.put("type", "box");
        body.put("layout", "vertical");
        body.put("contents", new JSONArray().put(textLink));

        contents.put("body", body);
        flexMessage.put("contents", contents);

        return flexMessage;
    }

    public static void main(String[] args) {
        try {
            sendTextLinkMessage("USER_ID");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

この実装では:

  1. createTextLinkMessage()メソッドで、JSONObjectを使用してFlex Messageを作成します。
  2. テキストリンクアクションを含むテキストコンポーネントを作成し、Flex Messageの本文に追加します。
  3. sendTextLinkMessage()メソッドで、作成したメッセージをLINE Messaging APIに送信します。

このコードを使用する際は、CHANNEL_ACCESS_TOKENを自分のチャンネルのアクセストークンに、USER_IDを送信先のユーザーIDに置き換えてください。

この方法により、SDKを使用せずにJSONObjectを使ってテキストリンクメッセージを作成し、LINEのAPIに直接送信することができます[1][3][7]。

Citations:
[1] https://bito.ai/resources/create-json-object-java-json-explained/
[2] https://qiita.com/tatsuya1970/items/f9d03159915d4d009f04
[3] https://ioflood.com/blog/jsonobject-java-class/
[4] https://dev.classmethod.jp/articles/line-messaging-api-flex-message/
[5] https://qiita.com/course_k/items/a5b5392f21e7e8869f83
[6] https://knowledge.littlehelp.co.jp/ja/chatbot/message/flex-message
[7] https://www.freecodecamp.org/news/jsonobject-tostring-how-to-convert-json-to-a-string-in-java/
[8] https://www.oracle.com/technical-resources/articles/java/json.html
[9] https://gist.github.com/jirawatee/be0f0a51ae794fbb3680d49d1c110f34
[10] https://qiita.com/kakakaori830/items/52e52d969800de61ce28


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?