Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] kakaoalarm #49

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {

// json parsing
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
implementation 'com.google.code.gson:gson:2.8.7'
}

tasks.named('test') {
Expand Down
74 changes: 56 additions & 18 deletions src/main/java/snowcare/backend/WeatherAPI.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package snowcare.backend;

import io.swagger.v3.core.util.Json;
import lombok.RequiredArgsConstructor;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import snowcare.backend.domain.User;
import snowcare.backend.repository.UserRepository;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
Expand All @@ -25,6 +27,9 @@ public class WeatherAPI {
@Value("${api.key}")
private String apiKey;

@Value("${access_token")
private String accessToken;

// ๋งค์ผ ์•„์นจ 9์‹œ์— ์‹คํ–‰
@Scheduled(cron = "0 0 9 * * ?")
public void callWeather() throws IOException {
Expand All @@ -33,35 +38,68 @@ public void callWeather() throws IOException {
for (User user : users) {
try {
// OpenWeatherMap API connection
URL url = new URL("https://api.openweathermap.org/data/2.5/weather?lat="+user.getLatitude()+"&lon="+user.getLongitude()+"&appid="+ apiKey);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
conn.setDoOutput(true);
URL weatherUrl = new URL("https://api.openweathermap.org/data/2.5/weather?lat="+user.getLatitude()+"&lon="+user.getLongitude()+"&appid="+ apiKey);
HttpURLConnection weatherConn = (HttpURLConnection) weatherUrl.openConnection();
weatherConn.setRequestMethod("GET");
weatherConn.setRequestProperty("Content-type", "application/json");
weatherConn.setDoOutput(true);

// ์นด์นด์˜คํ†ก ๋ฉ”์„ธ์ง€ api connection
URL kakaoUrl = new URL("https://kapi.kakao.com/v2/api/talk/memo/default/send");
HttpURLConnection kakaoConn = (HttpURLConnection) kakaoUrl.openConnection();
kakaoConn.setRequestMethod("POST");
kakaoConn.setRequestProperty("Authorization", "Bearer "+ accessToken);
kakaoConn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
kakaoConn.setDoOutput(true);

// ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
// ๋‚ ์”จ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
try {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(weatherConn.getInputStream(), "UTF-8"));
while(br.ready()) {
sb.append(br.readLine());
}

// ๊ฐ€์ ธ์˜จ ์ •๋ณด ํŒŒ์‹ฑ
// ๊ฐ€์ ธ์˜จ ๋‚ ์”จ ์ •๋ณด ํŒŒ์‹ฑ
JSONParser parser = new JSONParser();
JSONObject data = (JSONObject)parser.parse(sb.toString());
JSONArray weather = (JSONArray) data.get("weather");
JSONObject weather_info = (JSONObject) weather.get(0);

// weather main ์ข…๋ฅ˜ : Thunderstorm, Drizzle, Rain, Snow, Atmosphere, Clear, Clouds
String todaysWeather = (String) weather_info.get("main");
String weatherDescription = (String) weather_info.get("description");
JSONArray weather = (JSONArray) data.get("weather");
JSONObject weatherInfo = (JSONObject) weather.get(0);
String todaysWeather = (String) weatherInfo.get("main");
String weatherDescription = (String) weatherInfo.get("description");

// JSONArray temperature = (JSONArray) data.get("main");
// JSONObject tempInfo = (JSONObject) temperature.get(0);
// Double tempNow = ((Double) tempInfo.get("temp")) - 273.15;
// Double tempMax = ((Double) tempInfo.get("temp_max")) - 273.15;
// Double tempMin = ((Double) tempInfo.get("temp_min")) - 273.15;
// int humidity = (int) tempInfo.get("humidity");

// ๋ˆˆ์ด ์˜ฌ ๋•Œ๋งŒ ์•Œ๋ฆผ ์ฃผ๊ธฐ
if (todaysWeather.equals("Snow")) {
System.out.println("weather : " + todaysWeather);
System.out.println("description : " + weatherDescription);
System.out.println("๋ˆˆ์น˜์šฐ๊ธฐ ๋ด‰์‚ฌํ™œ๋™์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ ์ด์—์š”");

// ์นด์นด์˜คํ†ก ๋ฉ”์„ธ์ง€ ์ „์†ก ๋ฐ์ดํ„ฐ ์„ค์ •
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(kakaoConn.getOutputStream()));
StringBuilder stringBuilder = new StringBuilder();
JsonObject kakaoJson = new JsonObject();
kakaoJson.addProperty("object_type", "text");
kakaoJson.addProperty("text", "ํ˜„์žฌ ๊ธฐ์˜จ : -13\n์ตœ๋Œ€ ๊ธฐ์˜จ : -9\n์ตœ์ € ๊ธฐ์˜จ : -17\n์Šต๋„ : 67\n๋ˆˆ์ด ์˜ค๋Š” ๋‚ ์ด์—์š”! ๋ˆˆ์น˜์šฐ๊ธฐ ๋ด‰์‚ฌํ™œ๋™์„ ํ•˜๋Ÿฌ ๊ฐ€๋ณผ๊นŒ์š”?");

JsonObject link = new JsonObject();
link.addProperty("web_url", "http://localhost:3000");

kakaoJson.add("link", link.getAsJsonObject());

stringBuilder.append("template_object="+kakaoJson);
System.out.println(stringBuilder.toString());

bw.write(stringBuilder.toString());
bw.flush();

bw.close();
br.close();
}
} catch (Exception e) {
e.printStackTrace();
Expand Down