Line Notify 快速移转 Telegram Bot|10 分钟完成个人通知服务切换
面对 Line Notify 即将终止服务,教你用 10 分钟快速将通知系统切换到免费且功能更强大的 Telegram Bot,实现多群组分类、丰富讯息格式与互动指令,提升通知管理效率与使用体验。
Click here to view the English version of this article.
點擊這裡查看本文章正體中文版本。
基于 SEO 考量,本文标题与描述经 AI 调整,原始版本请参考内文。
10 分钟快速移转 Line Notify 到 Telegram Bot 通知
手把手将 Line Notify 个人通知服务迁移至同样免费、更强大的 Telegram Bot
Photo by Lana Codes
LINE Notify结束服务公告
感谢您长期以来使用LINE Notify。
LINE Notify自2016年9月推出以来,一直致力于为开发者提供服务。为能提供更优质的服务,并将经营资源集中于后续的类似商品服务,我们决定于2025年3月31日结束本服务。对于长期以来支持并使用LINE作为通知连动服务的所有用户,我们深表感谢。
若您仍需使用LINE向用户传送通知的商品服务,建议可改用功能更丰富的Messaging API。
撷取自 Line Notify 官网 , Line 于 2024/10/08 发文公告 Line Notify 将于 2025/04/01 完全关闭,如需继续使用 Line 做为讯息通知,只能用付费的 Message API 。
Line Notify 的优点是非常容易串接,拿来做个人通知机器人非常方便好用,一些 Line Bot 或第三方服务也会使用 Line Notify 进行通知 (例如:路易莎、你订的订单通知功能);但缺点也蛮多的,例如讯息内容单一、无法分群组(统一都是传送到 Line Notify Bot)、讯息内容长度有限…等等
随著 Line Notify 宣告终止,正好也给我了一个迁移至其他通讯、通知服务的机会:
Slack:免费版讯息只保留 30 天、我的通知比较是个人,用 Slack 有点大材小用。(Slack 传送讯息可参考我之前的文章: Slack & ChatGPT Integration )
Discord:我的通知比较是个人,一样有点大材小用。
Telegram:免费、几乎无限使用。
对我来说 Telegram 的通讯服务比较符合我原本 Line Notify 的使用需求,我需要一个能接收通知的频道,最好是能依照不同需求有不同的频道,可接受的内容、格式越丰富越好、并且能快速简单串接;Telegram 都符合上述需求,还能多实现跟 Bot 的交互功能。
成果
先上最终效果图(以 Github Star 通知、Repo Stats 通知为例 ):
✅ 有人按 Repo 星星时会触发 Webhook -> Google Apps Script -> Telegram Bot 发送通知到 Telegram — Github Stats Group
✅ Google Apps Script 每日定时 -> 捞取 Github Repo Stats 状态 -> Telegram Bot 发送通知到 Telegram — Github Stats Group
✅ 使用
/update
Telegram Bot Command 触发捞取 Github Repo Stats 状态 -> Telegram Bot 发送通知到 Telegram — Github Stats Group
对比原本 Line Notify
❌ 所有讯息无法分类、分群组都传到 Line Notify
❌ 无法针对个别讯息做特殊设定 (如通知声、静音…)
❌ 无法输入讯息互动
本文目录
设定 Telegram Bot
移转 Line Notify 发送讯息到 Telegram Bot (Google Apps Script)
与 Telegram Bot 交互 (Command) x 使用 Google Apps Script
(1/2) 设定 Telegram Bot
Telegram Bot 的申请非常简单,连网页都不需要开,只要跟官方的 BotFather 机器人 互动就可以了。
Step 1. 申请 Telegram Bot
安装、注册好 Telegram 服务 之后,点击加「 BotFather 机器人 」为好友。
打开、加入 BotFather 机器人
加入后直接传送讯息「
/newbot
」建立你的机器人。输入你的机器人名称
输入你的机器人帐号 (不可重复、必须以
bot
为结尾,例如我的zhgchgli_bot
)你的 Bot 连结,点进进入开始使用 (e.g. t.me/harrytest56_bot)
取得
你的_BOT_API_Token
, 请妥善保存 ⚠️⚠️⚠️
点击 4. 取得的 Bot 连结,开始使用 Bot:
目前无任何功能,可以点右上角 Info,编辑名称或上传大头贴。
Step 2. 创建 Telegram 通知 Group & 加入机器人帐号
我希望不同的个人通知类型传到不同的 Group,这 边因为 Demo 只创建一个 My Notify Group 。
你可以依据实际需求创建不同的 Group & 依照步骤加入、设定机器人。
New Group
搜寻你的机器人帐号&点击加入
设定 Group 名称、大头贴
Step 3. 取得 Group Chat ID
Telegram Bot API 没有直接取得 Group 列表、Group Chat ID 的 Endpoint,只能透过 /getUpdates
取得机器人讯息列表,并从中找到 Group Chat ID:
Request:
1
curl 'https://api.telegram.org/bot你的_BOT_API_Token/getUpdates'
Telegram API 格式为
https://api.telegram.org/bot
你的_BOT_API_Token/getUpdates
, BOT API Token 字串前要加上bot
字串范例:
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 Nofify",
"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 Nofify",
"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 Nofify",
"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
}
]
}
}
]
}
可以从回应中找到对应 Group Name + type=group 的巢状 JSON 资料,其中的 id 就是 Group Chat ID:
1
2
3
4
5
6
"chat": {
"id": -4532420331,
"title": "My Nofify",
"type": "group",
"all_members_are_administrators": false
}
Group Chat Id
=-4532420331
⚠️⚠️⚠️️ 如果你的 Response 为空
{ "ok": true, "result": [] }
请尝试在你的 Group 发送讯息 (e.g.Hello
) 再重新 Call API 应该就有了。
Step 4. 发送讯息
我们可以使用 /sendMessage
发送讯息到 Group。
Request:
1
2
3
curl 'https://api.telegram.org/bot你的_BOT_API_Token/sendMessage' \
--form 'chat_id="Group Chat Id"' \
--form 'text="讯息内容"'
范例:
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 Nofify",
"type": "group",
"all_members_are_administrators": true
},
"date": 1728727847,
"text": "test"
}
}
- 发送成功取得以上 Response
- 回 Telegram Group 就会出现刚发送的讯息内容。
(2/2) 移转 Line Notify 发送讯息到 Telegram Bot (Google Apps Script)
我的个人通知机器人服务是使用 Google Apps Script 达成的,因此以 Google Apps Script 转换为例 (类 JavaScript)。
原始的 Line Notify 发送程式码:
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());
}
可以看到非常简单方便好用…
移转成 Telegram Bot 发送程式码:
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 = "你的_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());
}
照前面 Telegram Bot 设定步骤取得的资讯。
telegramToken
=你的_BOT_API_Token
TelegramChatId
这边是我自己多定义的方法,因为实务上我希望不同的通知分别传送到不同的 Group,因此定义了一个结构管理目标 Group 与他的Group Chat Id
。
/sendMessage
API 参数,更多参数、细节可参考官方文件 ,以下是我自己会用到的参数:
text: 讯息内容 (必带)
chat_id: 目标 Group Chat Id (必带)
parse_mode: 讯息内容解析方式,这边我指定
Markdown
disable_web_page_preview: 讯息内容的连结是否不要预览,这边设
true
关闭,可以让讯息显示更简洁。
使用方式:
1
2
sendNotifyMessage("Hello", TelegramChatId.MEDIUM) // 发送 Hello 讯息到 MEDIUM Group Chat Id
sendNotifyMessage("Hello", -1234) // 发送 Hello 讯息到 -1234 Group Chat Id
成果
以我的 Github Repo Star Notifier 机器人 为例:
验证成功! 当有人按我的 Repo Star 时能正确改发送通知到 Telegram Group!🎉🎉🎉
制作方式可参考我之前的文章「 使用 Google Apps Script 三步骤免费建立 Github Repo Star Notifier 」
设定特殊声音或静音
比 Line Notify 更棒的事是我们还可以对不同的 Group 设定不同的通知声音或是静音。
与 Telegram Bot 交互 (Command) x 使用 Google Apps Script
除了替代 Notify 功能之外, Telegram Bot 还能轻易地实现与使用者交互的功能 — Telegram Bot Command。
回到我的使用场景,我的机器人会定时或是 Webhook 触发传送通知讯息送给我;但有时候我也想手动触发机器人立刻取得当前结果,以往 Line Notify 没有这个功能,以 Google Apps Script 来说就只能暴力的设定一个网址,只要打开网址就会触发,很不好用。
Telegram Bot Command 就能让我直接输入指令讯息,命令机器人立刻执行我想做的事情。
本文以 Goolge Apps Script 为例,Google Apps Script 详细介绍可参考我之前文章「 使用 Google Apps Script 实现 Google 服务 RPA 自动化 」。
Step 1. 使用 Google Apps Script 实现 Command 处理逻辑
前往 Google Apps Script 首页
左上方「建立新专案」
点击「未命名专案」输入专案名称 e.g.
Telegram
贴上基础程式码:
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 = "你的_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")) {
// 收到 /update 指令
// 在这边处理你想做的事...然后回应...
sendNotifyMessage("你好.....\n指令:"+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
=你的_BOT_API_Token
上述 Demo 程式我们在收到 Post 请求、Command 参数等于
/update
时回应你好…
模拟收到指令后处理后回应的场景。
Step 2. 完成 Google Apps Script Web 部署
右上角「部署」-> 「新增部署作业」
左上角「设定」->「网页应用程式」
谁可以存取选择「所有人」
新增部署作业,选择「授予存取权」
跳出帐号视窗,选择你的 Google 登入帐号
跳出警告视窗,选择「Advanced」->「Go to
专案名称
(unsafe)」选择「Allow」
- 网页应用程式网址:
你的 Webhook 网址
。 复制下来。 e.g.https://script.google.com/macros/s/AKfycbx2oFv-eB4LezdOk3P3aoEZVhx_PI6n_YnTNP7WVVQSaiRU52di5bKNThsvIZxus3Si/exec
Google Apps Script 网页应用程式部署、更新、使用、除错可参考我之前的文章「 使用 Google Apps Script 实现 Google 服务 RPA 自动化 」。
⚠️⚠️⚠️ 请注意,如果 Google Apps Script 程式码有变更要点击部署->管理部署->选择建立新版本才会生效,细节可参考上述文章。
⚠️⚠️⚠️请注意,如果 Google Apps Script 程式码有变更要点击部署->管理部署->选择建立新版本才会生效,细节可参考上述文章。
⚠️⚠️⚠️请注意,如果 Google Apps Script 程式码有变更要点击部署->管理部署->选择建立新版本才会生效,细节可参考上述文章。
Step 3. 注册 Webhook
使用 Telegram API /setWebhook
注册你的 Webhook 网址。
Request:
1
2
curl --location 'https://api.telegram.org/你的_BOT_API_Token/setWebhook' \
--form 'url="你的 Webhook 网址"'
Response:
1
2
3
4
5
{
"ok": true,
"result": true,
"description": "Webhook was set"
}
测试
我们会根据不同的 Chat Id 回应,因此不管是 1:1 对机器人或是有机器人在的 Group 群组都能回应。
成功 🎉🎉🎉
下篇:
其他 Google Apps Script 自动化文章
附注
本文也是我 Medium 的第 100 篇文章(2018/10 发表 第一篇 ,6 年了),坚持不懈、继续努力,详细心得跟数据等到破 1000 Followers (2024/10 目前 925) 或是总浏览数破 1,000,000 (2024/10 目前 984,549) 时再来分享。
有任何问题及指教欢迎 与我联络 。
本文首次发表于 Medium (点击查看原始版本),由 ZMediumToMarkdown 提供自动转换与同步技术。