前面我們做了測試訊息並發布,接下來我們要來推播每日單字。
// 每日英文單字與例句
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會得到: