iT邦幫忙

2025 iThome 鐵人賽

DAY 17
0

前面我們做了測試訊息並發布,接下來我們要來推播每日單字。

程式碼

// 每日英文單字與例句
function dailyEnglish() {
  var words = [
  {word: "serendipity", sentence: "Finding a $20 bill on the ground was pure serendipity."},
  {word: "benevolent", sentence: "She was a benevolent leader who always cared about her team."},
  {word: "ephemeral", sentence: "The sunset was beautiful but ephemeral."},
  {word: "meticulous", sentence: "He kept meticulous records of every expense."}
];

  // 隨機挑一個
  var today = words[Math.floor(Math.random() * words.length)];

  var message = "📚 今日單字:" + today.word + "\n\n📝 例句:" + today.sentence;
  if (!today.word || !today.sentence) {
  Logger.log("單字或例句為空,無法發送");
  return;
}

  pushToLine(message);
}

words 是一個陣列,裡面放了多個物件,每個物件有兩個欄位:
word:單字
sentence:例句

把單字跟例句組合成一個字串,要傳給 LINE。
\n\n 代表換兩行,排版更清楚。

設有防呆機制:
確保 today.word 和 today.sentence 不是空值。
如果其中一個欄位空了,就不送訊息,避免 LINE API 回錯誤(例如 400)。
最後執行dailyEnglish會得到:
https://ithelp.ithome.com.tw/upload/images/20250827/20169466W71qrKe3OY.jpg


上一篇
D16 Line Notify 替代
下一篇
D18 Line Notify 替代
系列文
Google App Script雲端自動化與動態網頁實戰20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言