0
0

LINE Notify for Java 自分用メモ

Posted at

Line Notify

【事前準備】
Line Notify公式サイトでTOKENを取得
②Line Notifyをグループに招待

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.net.URL;
import java.util.Scanner;

public class LineNotify {

    // LINE Notify トークン
    private static final String TOKEN = "YOUR_TOKEN";
    // LINE Notify APIエンドポイント
    private static final String URL = "https://notify-api.line.me/api/notify";

    private static String urlencode(String message) throws Exception {
        return URLEncoder.encode(message, "UTF-8");
    }

    public static void main(String[] args) {

        String message = "テスト";

        try {
            String encodedMessage = urlencode(message);
            sendPostRequest(encodedMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendPostRequest(String message) throws Exception {
        URL url = new URL(URL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + TOKEN);
        conn.setDoOutput(true);

        // "message="は必須
        String postData = "message=" + message;

        try (OutputStream os = conn.getOutputStream()) {
            os.write(postData.getBytes("UTF-8"));
        } 

        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("メッセージが送信されました。");
        } else {
            System.out.println("メッセージの送信に失敗しました。HTTP応答コード: " + responseCode);
        }

        conn.disconnect();
    }
}

※画像も送れる

パラメータ名 画像形式
imageFile File png、jpeg
0
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
0
0