1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Replace these values with your own
const OPENWEATHERMAP_API_KEY = "註冊的apiKey";
const LINE_NOTIFY_ACCESS_TOKEN = "Line Notify的token";

// Function to get weather data from OpenWeatherMap
function getWeather() {
  const lang = "zh_tw"; //語言
  const apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=24.94702&lon=121.581749&lang=${lang}&units=metric&appid=${OPENWEATHERMAP_API_KEY}`;

  try {
    const response = UrlFetchApp.fetch(apiUrl);
    const weatherData = JSON.parse(response.getContentText());

    return weatherData;
  } catch (error) {
    console.error("Error fetching weather data: " + error);
    return null;
  }
}

// Function to send Line Notify message
function sendLineNotify(message) {
  const lineNotifyUrl = "https://notify-api.line.me/api/notify";
  const formData = {
    "message": message,
  };
  const options = {
    "headers": { "Authorization": "Bearer " + LINE_NOTIFY_ACCESS_TOKEN },
    "method": 'post',
    "payload": formData,
  };

  try {
    const response = UrlFetchApp.fetch(lineNotifyUrl, options);
    console.log("Line Notify message sent successfully");
  } catch (error) {
    console.error("Error sending Line Notify message: " + error);
  }
}

// Function to format temperature to Celsius
function tempToC(fTemp) {
  return round((fTemp - 32) * 5 / 9, 1);
}

// 天氣icon文字傳換成符號
function mapWeatherIcon(iconCode) {
  const iconMapping = {
    "01d": "☀",
    "01n": "☀",
    "02d": "⛅",
    "02n": "⛅",
    "03d": "☁",
    "03n": "☁",
    "04d": "☁",
    "04n": "☁",
    "09d": "🌧",
    "09n": "🌧",
    "10d": "🌦",
    "10n": "🌦",
    "11d": "🌩",
    "11n": "🌩",
    "13d": "❄",
    "13n": "❄",
    "50d": "🌫",
    "50n": "🌫"
  };

  return iconMapping[iconCode] || "?"; // Default to a question mark if the code is not recognized
}

// Function to send weather notification
function sendWeatherNotification() {
  const weatherData = getWeather();
  if (weatherData) {
    const weatherDescription = weatherData.weather[0].description; // 天氣
    const iconCode = weatherData.weather[0].icon; // 天氣icon
    const iconSymbol = mapWeatherIcon(iconCode); // 轉換後的符號
    const feelsLike = weatherData.main.feels_like; // 體感溫度
    const tempMax = weatherData.main.temp_max; // 最高溫度
    const tempMin = weatherData.main.temp_min; // 最低溫度
    const humidity = weatherData.main.humidity; // 濕度
    const windSpeed = weatherData.wind.speed; // 風速
    
    // Format the message
    const message = `\n天氣:${weatherDescription} ${iconSymbol}\n`
                  + `體感溫度:${feelsLike} °C\n`
                  + `最高溫:${tempMax} °C\n`
                  + `最低溫:${tempMin} °C\n`
                  + `濕度:${humidity}%\n`
                  + `風速:${windSpeed} m/s\n`;

    // Send Line Notify message
    sendLineNotify(message);
  } else {
    console.error("Failed to fetch weather data.");
  }
}