📲 Integrating Line Notify: Building a Real-Time Monitoring Notification Bot

In the previous chapter, we successfully made Google Sheet automatically fetch the USD exchange rate every morning.
But if the exchange rate suddenly crashes, as an investor, you might not check the spreadsheet daily. What if it could actively send you a LINE message saying, "Boss, the USD crashed today, hop on quick!"?

In this chapter, we’ll guide you through applying for a lifetime-free Line Notify and teach you how to write a function in GAS to push alerts to your phone or workgroup anytime, anywhere!


1. What is Line Notify?

If you want to send LINE messages to others, there are two methods:

  1. LINE Official Account (Bot): Powerful features like graphical menus and two-way chatting. The downside? It’s paid! Exceeding 200 messages per month incurs high subscription fees.
  2. LINE Notify: Only supports "one-way broadcasting." You can bind yourself or a group, and the code can only "send text or images" to you. The advantage? Completely free! No sending limits!

For system monitoring, internal report notifications, or personal investment bots, LINE Notify is undoubtedly the top choice.


2. Applying for a LINE Notify Token

To grant our GAS program permission to send messages to your LINE, we need to apply for a "key" (Token).

  1. Open the LINE Notify official website in a computer browser and log in with your LINE account.
  2. Click your name in the top-right corner -> select "My Page."
  3. Scroll to the bottom and click the "Generate token" button.
  4. Enter a token name: For example, [Finance Bot] (this will appear at the start of each message).
  5. Select the chat to receive notifications:
    • Choose "Receive LINE Notify messages via 1-on-1 chat" (only visible to you).
    • Or select a "group chat" with your team (visible to everyone in the group).
  6. Click "Generate."
  7. A long string of random characters will appear (e.g., abc123DEF456ghi789...).
    🚨 Immediately copy and save it in a text file! Once you close this window, you’ll never see this Token again!

⚠️ If you selected a "group chat," remember to open the LINE group on your phone and invite the official account LINE Notify to join the group! Otherwise, it won’t be able to post messages there!


3. Writing a LINE Notify Sending Function in GAS

Return to our GAS editor (Code.gs) from the previous chapter.
Let’s write a shared function that will send any message you pass to LINE!

// Shared function to send LINE Notify messages
function sendLineNotify(message) {
  // Paste the Token you copied here
  var token = "YOUR_LINE_NOTIFY_TOKEN_HERE"; 
  
  // The official LINE Notify API URL
  var url = "https://notify-api.line.me/api/notify";
  
  // The required transmission format for LINE Notify
  var options = {
    "method": "post",
    "headers": {
      "Authorization": "Bearer " + token
    },
    // The content to send is placed in 'payload', with the key 'message'
    "payload": {
      "message": message
    }
  };
  
  try {
    // Fire!
    var response = UrlFetchApp.fetch(url, options);
    Logger.log("LINE sent successfully: " + response.getContentText());
  } catch (error) {
    Logger.log("LINE failed to send: " + error.toString());
  }
}

// Let’s write a test function to see if your phone buzzes!
function testLine() {
  sendLineNotify("\nGood morning, boss! This is a test message sent from Google Sheet! 🚀");
}

💡 Pro Tip: You’ll notice I added \n at the start of the test message. This is because LINE Notify’s default format is [Token Name] Your Message. Without a line break, it looks cramped. Adding \n moves your content to the next line for better formatting!

Now, select the testLine function and click "Run." Your phone should instantly "ding" with the bot’s greeting!


4. Combining with the Previous Chapter: Building a "Buy-the-Dip Alert"

Now, let’s merge the "exchange rate scraper" from the last chapter with this chapter’s "LINE sender" to create an automated alert with business logic.

Modify our scraper code from the previous chapter:

function fetchDailyExchangeRate() {
  var apiUrl = "https://api.exchangerate-api.com/v4/latest/USD";
  
  try {
    var response = UrlFetchApp.fetch(apiUrl);
    var data = JSON.parse(response.getContentText());
    var usdToTwd = data.rates.TWD;
    
    if (!usdToTwd) return;
    
    // Write to spreadsheet
    var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy/MM/dd");
    appendToSheet(today, usdToTwd);
    
    // 🔔 Alert logic: If USD rate drops below 31, send a "buy alert" to the boss!
    if (usdToTwd < 31.0) {
      var msg = "\n🚨 【USD DIP ALERT】🚨\n" + 
                "The USD to TWD rate has fallen below 31!\n" +
                "Current rate: " + usdToTwd + "\n" +
                "Boss, consider buying in batches now! 💵";
                
      sendLineNotify(msg);
    } else {
      // For normal rates, send a simple daily report
      sendLineNotify("\n📊 Daily Finance Report \nUSD to TWD rate: " + usdToTwd);
    }
    
  } catch (error) {
    // If the system crashes, send a LINE to the engineer (you!)
    sendLineNotify("\n❌ Scraper system error! Check the code ASAP!\nError: " + error.toString());
  }
}

5. More Business Use Cases

Mastering the combo of UrlFetchApp and LINE Notify gives you a Swiss Army knife of automation.
Beyond finance bots, you can implement countless business automations:

  1. Form Monitoring System: When a customer submits your Google Form appointment, set a trigger "on form submit" to instantly send a LINE to your customer service group: "🔔 New appointment! Contact ASAP!"
  2. Payment Reconciliation Bot: When accounting marks a payment as "received" in Google Sheet, automatically notify the salesperson to ship the goods via LINE.
  3. Website Downtime Monitor: Write a script to ping your company’s homepage hourly. If the response isn’t 200 OK (site is down), spam LINE to wake up the engineer and fix the bug.

All these powerful features are completely free in Google Apps Script—no server rentals or cloud databases needed! With this automation mindset, one person can match the efficiency of an entire team!

Unlock Full Tutorial

This chapter is paid content. Join the project to unlock over 5000 words of deep analysis, including 10+ god-tier Prompts and real Source Code examples!