Post

Quickly Transfer Line Notify to Telegram Bot Notifications in 10 Minutes

A step-by-step guide to migrating the Line Notify personal notification service to the equally free and more powerful Telegram Bot.

Quickly Transfer Line Notify to Telegram Bot Notifications in 10 Minutes

ℹ️ℹ️ℹ️ The following content is translated by OpenAI.

Click here to view the original Chinese version. | 點此查看本文中文版


Quickly Transfer Line Notify to Telegram Bot Notifications in 10 Minutes

A step-by-step guide to migrating the Line Notify personal notification service to the equally free and more powerful Telegram Bot.

Photo by [Lana Codes](https://unsplash.com/@lanacodes?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash){:target="_blank"}

Photo by Lana Codes

LINE Notify Service Termination Announcement

Thank you for using LINE Notify for a long time.

Since its launch in September 2016, LINE Notify has been dedicated to providing services for developers. To offer better services and focus resources on subsequent similar products, we have decided to terminate this service on March 31, 2025. We sincerely thank all users who have supported and used LINE as a notification service.

If you still need to send notifications to users via LINE, we recommend switching to the more feature-rich Messaging API.

Excerpt from Line Notify Official Website, where Line announced on October 8, 2024, that Line Notify will be completely shut down on April 1, 2025. If you wish to continue using Line for message notifications, you can only use the paid Message API.

The advantage of Line Notify is that it is very easy to integrate, making it very convenient for personal notification bots. Some Line Bots or third-party services also use Line Notify for notifications (e.g., Louisa, order notification features); however, it has many drawbacks, such as limited message content, inability to group messages (all sent to the Line Notify Bot), and limited message length, among others.

With the announcement of Line Notify’s termination, it also provides me with an opportunity to migrate to other communication and notification services:

  • Slack: The free version retains messages for only 30 days, and since my notifications are more personal, using Slack feels a bit excessive. (For sending messages via Slack, refer to my previous article: Slack & ChatGPT Integration)
  • Discord: Again, my notifications are personal, making it feel a bit excessive.
  • Telegram: Free and nearly unlimited usage.

For me, Telegram’s communication service better meets my original needs for Line Notify. I need a channel that can receive notifications, ideally with different channels for different needs, and the more diverse the content and format, the better. Additionally, it should be quick and easy to integrate; Telegram meets all these requirements and also allows for interaction with bots.

Results

First, here’s the final effect image (using Github Star notifications and Repo Stats notifications as examples):

  • ✅ When someone stars the Repo, it triggers a Webhook -> Google Apps Script -> Telegram Bot sends a notification to Telegram — Github Stats Group
  • ✅ Google Apps Script runs daily -> fetches Github Repo Stats status -> Telegram Bot sends a notification to Telegram — Github Stats Group
  • ✅ Using the /update Telegram Bot Command triggers fetching Github Repo Stats status -> Telegram Bot sends a notification to Telegram — Github Stats Group

Comparison with Original Line Notify

  • ❌ All messages cannot be categorized; they are all sent to Line Notify
  • ❌ Cannot set special settings for individual messages (e.g., notification sound, mute, etc.)
  • ❌ Cannot interact with messages

Table of Contents

  • Setting Up Telegram Bot
  • Migrating Line Notify Messages to Telegram Bot (Google Apps Script)
  • Interacting with Telegram Bot (Command) x Using Google Apps Script

(1/2) Setting Up Telegram Bot

Applying for a Telegram Bot is very simple; you don’t even need to open a webpage, just interact with the official BotFather bot.

Step 1. Apply for a Telegram Bot

After installing and registering for Telegram services, click to add the BotFather bot as a friend.

  1. Open and join the BotFather bot.
  2. After joining, send the message “/newbot” to create your bot.
  3. Enter your bot’s name.
  4. Enter your bot’s username (must be unique and end with bot, e.g., my zhgchgli_bot).
  5. Your Bot link, click to start using it (e.g., t.me/harrytest56_bot).
  6. Obtain your YOUR_BOT_API_Token, please keep it safe ⚠️⚠️⚠️.

Click on the link obtained in step 4 to start using the Bot:

Currently, there are no functions; you can click the info icon in the upper right corner to edit the name or upload a profile picture.

Step 2. Create a Telegram Notification Group & Add the Bot Account

I want different types of personal notifications to go to different Groups, so for the demo, I only created one My Notify Group.

You can create different Groups based on your actual needs and follow the steps to add and set up the bot.

  1. New Group
  2. Search for your bot account & click to join.
  3. Set the Group name and profile picture.

Step 3. Obtain Group Chat ID

The Telegram Bot API does not have a direct endpoint to obtain the list of Groups or Group Chat IDs; you can only get the bot’s message list through /getUpdates and find the Group Chat ID from there:

Request:

1
curl 'https://api.telegram.org/botYOUR_BOT_API_Token/getUpdates'
  • The Telegram API format is https://api.telegram.org/bot YOUR_BOT_API_Token /getUpdates, and the BOT API Token string must be prefixed with bot.
  • Example: curl 'https://api.telegram.org/bot7814194578:AAEWpPJvKn06ID7D9FjV65aDKQLkGkz8cc8/getUpdates'

Response:

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
99
100
101
102
103
104
105
106
{
    "ok": true,
    "result": [
        {
            "update_id": 706454235,
            "my_chat_member": {
                "chat": {
                    "id": -4532420331,
                    "title": "My Notify",
                    "type": "group",
                    "all_members_are_administrators": false
                },
                "from": {
                    "id": 986128250,
                    "is_bot": false,
                    "first_name": "Harry",
                    "last_name": "Li",
                    "username": "zhgchgli"
                },
                "date": 1728726861,
                "old_chat_member": {
                    "user": {
                        "id": 7814194578,
                        "is_bot": true,
                        "first_name": "Harry Test",
                        "username": "harrytest56_bot"
                    },
                    "status": "left"
                },
                "new_chat_member": {
                    "user": {
                        "id": 7814194578,
                        "is_bot": true,
                        "first_name": "Harry Test",
                        "username": "harrytest56_bot"
                    },
                    "status": "member"
                }
            }
        },
        {
            "update_id": 706454236,
            "message": {
                "message_id": 1,
                "from": {
                    "id": 986128250,
                    "is_bot": false,
                    "first_name": "Harry",
                    "last_name": "Li",
                    "username": "zhgchgli"
                },
                "chat": {
                    "id": -4532420331,
                    "title": "My Notify",
                    "type": "group",
                    "all_members_are_administrators": true
                },
                "date": 1728726861,
                "group_chat_created": true
            }
        },
        {
            "update_id": 706454237,
            "message": {
                "message_id": 2,
                "from": {
                    "id": 986128250,
                    "is_bot": false,
                    "first_name": "Harry",
                    "last_name": "Li",
                    "username": "zhgchgli"
                },
                "chat": {
                    "id": -4532420331,
                    "title": "My Notify",
                    "type": "group",
                    "all_members_are_administrators": true
                },
                "date": 1728726864,
                "new_chat_photo": [
                    {
                        "file_id": "AgACAgUAAxkBAAMCZwpHUEaLZSvFFYu8GiO-8qI_jVYAApfAMRu0B1BUJP-4u2wF6scBAAMCAANhAAM2BA",
                        "file_unique_id": "AQADl8AxG7QHUFQAAQ",
                        "file_size": 5922,
                        "width": 160,
                        "height": 160
                    },
                    {
                        "file_id": "AgACAgUAAxkBAAMCZwpHUEaLZSvFFYu8GiO-8qI_jVYAApfAMRu0B1BUJP-4u2wF6scBAAMCAANiAAM2BA",
                        "file_unique_id": "AQADl8AxG7QHUFRn",
                        "file_size": 15097,
                        "width": 320,
                        "height": 320
                    },
                    {
                        "file_id": "AgACAgUAAxkBAAMCZwpHUEaLZSvFFYu8GiO-8qI_jVYAApfAMRu0B1BUJP-4u2wF6scBAAMCAANjAAM2BA",
                        "file_unique_id": "AQADl8AxG7QHUFQB",
                        "file_size": 37988,
                        "width": 640,
                        "height": 640
                    }
                ]
            }
        }
    ]
}

You can find the corresponding Group Name + type=group nested JSON data in the response, where the id is the Group Chat ID:

1
2
3
4
5
6
"chat": {
  "id": -4532420331,
  "title": "My Notify",
  "type": "group",
  "all_members_are_administrators": false
}
  • Group Chat Id = -4532420331

⚠️⚠️⚠️️ If your response is empty { "ok": true, "result": [] }, please try sending a message in your Group (e.g., Hello) and then call the API again; it should work.

Step 4. Send a Message

We can use /sendMessage to send a message to the Group.

Request:

1
2
3
curl 'https://api.telegram.org/botYOUR_BOT_API_Token/sendMessage' \
--form 'chat_id="Group Chat Id"' \
--form 'text="Message content"'

Example:

1
2
3
curl 'https://api.telegram.org/bot7814194578:AAEWpPJvKn06ID7D9FjV65aDKQLkGkz8cc8/sendMessage' \
--form 'chat_id="-4532420331"' \
--form 'text="test"'

Response & Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "ok": true,
  "result": {
    "message_id": 5,
    "from": {
      "id": 7814194578,
      "is_bot": true,
      "first_name": "Harry Test",
      "username": "harrytest56_bot"
    },
    "chat": {
      "id": -4532420331,
      "title": "My Notify",
      "type": "group",
      "all_members_are_administrators": true
    },
    "date": 1728727847,
    "text": "test"
  }
}
  • Successfully sent, receiving the above response.

  • The message content just sent will appear in the Telegram Group.

(2/2) Migrating Line Notify Messages to Telegram Bot (Google Apps Script)

My personal notification bot service is achieved using Google Apps Script, so I will use Google Apps Script for the conversion example (similar to JavaScript).

Original Line Notify Sending Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const lineToken = "XXXX";

function sendLineNotifyMessage(message) {
  var url = 'https://notify-api.line.me/api/notify';
  
  var options = {
    method: 'post',
    headers: {
      'Authorization': 'Bearer '+lineToken
    },
    payload: {
      'message': message
    }
  }; 
  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

As you can see, it is very simple and convenient to use…

Migrating to Telegram Bot Sending Code:

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
const telegramToken = "YOUR_BOT_API_Token";
const TelegramChatId = {
  GA: -123456,
  GITHUB: -123457,
  MEDIUM: -123458
};

function sendNotifyMessage(message, chatId) {
  var url = "https://api.telegram.org/bot"+telegramToken+"/sendMessage";
  
  const payload = {
    "chat_id": chatId,
    "text": message,
    "parse_mode": "Markdown"
  } 
  const options = {
    'method': 'post',
    'contentType': 'application/json',
    'muteHttpExceptions': true,
    'payload': JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

Using the information obtained from the previous Telegram Bot setup steps:

  • telegramToken = YOUR_BOT_API_Token
  • TelegramChatId is a structure I defined myself because, in practice, I want different notifications to be sent to different Groups, so I defined a structure to manage the target Groups and their Group Chat Id.
1
2
3
4
5
6
7
8
9
10
11
/sendMessage [**API parameters, for more parameters and details, please refer to the official documentation**](https://core.telegram.org/bots/api#sendmessage){:target="_blank"} Here are the parameters I personally use:
- text: Message content \(required\)
- chat\_id: Target Group Chat Id \(required\)
- parse\_mode: Message content parsing method, here I specify `Markdown`
- disable\_web\_page\_preview: Whether to disable link previews in the message content, set to `true` to turn it off for a cleaner message display.


**Usage:**
```scss
sendNotifyMessage("Hello", TelegramChatId.MEDIUM) // Send Hello message to MEDIUM Group Chat Id
sendNotifyMessage("Hello", -1234) // Send Hello message to -1234 Group Chat Id

Results

Taking my Github Repo Star Notifier Bot as an example:

Setting Special Sounds or Mute

What’s even better than Line Notify is that we can set different notification sounds or mute for different Groups.

Interacting with Telegram Bot (Command) x Using Google Apps Script

In addition to replacing the Notify function, the Telegram Bot can easily implement user interaction features — Telegram Bot Commands.

Returning to my use case, my bot sends notification messages to me at scheduled times or triggered by Webhooks; however, sometimes I want to manually trigger the bot to get the current results immediately. Previously, Line Notify did not have this feature, and with Google Apps Script, I could only set a URL that would trigger the action when opened, which was not very user-friendly.

Telegram Bot Commands allow me to directly input command messages, instructing the bot to execute what I want immediately.

This article uses Google Apps Script as an example. For a detailed introduction to Google Apps Script, you can refer to my previous article “Implementing Google Service RPA Automation with Google Apps Script“.

Step 1. Implement Command Handling Logic Using Google Apps Script

  • Go to the Google Apps Script homepage
  • Click “Create New Project” in the top left
  • Click on “Untitled Project” to enter the project name e.g. Telegram
  • Paste the basic code:

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
const telegramToken = "your_BOT_API_Token";

function doPost(e) {
  const content = JSON.parse(e.postData.contents);
  if (content.message && content.message.text) {
    const command = content.message.text.split(' ')[0];
    const chatId = content.message.chat.id;

    if (command.startsWith("/update")) { 
      // Received /update command
      // Handle what you want to do here... then respond...
      sendNotifyMessage("Hello.....\nCommand:"+command, chatId);
    }
  }

  return HtmlService.createHtmlOutput("OK!");
}

function sendNotifyMessage(message, chatId) {
  var url = "https://api.telegram.org/bot"+telegramToken+"/sendMessage";
  
  const payload = {
    "chat_id": chatId,
    "text": message,
    "disable_web_page_preview": true,
    "parse_mode": "Markdown"
  } 
  const options = {
    'method': 'post',
    'contentType': 'application/json',
    'muteHttpExceptions': true,
    'payload': JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}
  • telegramToken = your_BOT_API_Token
  • In the above demo code, when we receive a Post request and the Command parameter equals /update, we respond with Hello… simulating the scenario of processing after receiving the command.

Step 2. Complete Google Apps Script Web Deployment

  • Click “Deploy” in the top right -> “New Deployment”
  • In the top left, click “Settings” -> “Web App”
  • For who can access, select “Anyone”

  • Add a deployment task, select “Grant Access”
  • A window will pop up for account selection, choose your Google login account
  • A warning window will pop up, select “Advanced” -> “Go to Project Name (unsafe)”
  • Select “Allow”

  • Web app URL: your Webhook URL . Copy it. e.g. https://script.google.com/macros/s/AKfycbx2oFv-eB4LezdOk3P3aoEZVhx_PI6n_YnTNP7WVVQSaiRU52di5bKNThsvIZxus3Si/exec

For Google Apps Script web app deployment, updates, usage, and debugging, you can refer to my previous article “Implementing Google Service RPA Automation with Google Apps Script“.

⚠️⚠️⚠️ Please note, if there are changes to the Google Apps Script code, you must click Deploy -> Manage Deployments -> Select Create New Version for it to take effect. For details, please refer to the article above.

⚠️⚠️⚠️ Please note, if there are changes to the Google Apps Script code, you must click Deploy -> Manage Deployments -> Select Create New Version for it to take effect. For details, please refer to the article above.

⚠️⚠️⚠️ Please note, if there are changes to the Google Apps Script code, you must click Deploy -> Manage Deployments -> Select Create New Version for it to take effect. For details, please refer to the article above.

Step 3. Register Webhook

Use the Telegram API /setWebhook to register your Webhook URL.

Request:

1
2
curl --location 'https://api.telegram.org/your_BOT_API_Token/setWebhook' \
--form 'url="your Webhook URL"'

Response:

1
2
3
4
5
{
    "ok": true,
    "result": true,
    "description": "Webhook was set"
}

Testing

  • We will respond based on different Chat Ids, so whether it’s a 1:1 interaction with the bot or in a Group where the bot is present, we can respond.
  • Success 🎉🎉🎉

Next Article:

Other Google Apps Script Automation Articles

Note

This article is also my 100th post on Medium (first published in October 2018 here, it’s been 6 years). I will continue to work hard and share detailed insights and data once I reach 1000 Followers (currently 925 as of October 2024) or a total view count of over 1,000,000 (currently 984,549 as of October 2024).

If you have any questions or suggestions, feel free to contact me.

```


This article was first published on Medium ➡️ Click Here

Automatically converted and synchronized using ZMediumToMarkdown and Medium-to-jekyll-starter.

Improve this page on Github.

Buy me a beer

8,712 Total Views
Last Statistics Date: 2025-03-25 | 5,699 Views on Medium.
This post is licensed under CC BY 4.0 by the author.