Tự Động Gửi Cảnh Báo Zalo/Telegram Từ Google Sheets
14 tháng 5, 2026
5 phút đọc
Gửi Cảnh Báo Ngay Khi Cần
Email không đủ real-time cho các cảnh báo khẩn. Telegram và Zalo gửi thông báo ngay lập tức đến điện thoại, giúp bạn phản ứng kịp thời khi tồn kho thấp, doanh thu bất thường, hay lỗi hệ thống phát sinh.
Gửi Tin Nhắn Telegram Từ Apps Script
Bước 1: Tạo Telegram Bot
- Mở Telegram, tìm @BotFather
- Gửi lệnh
/newbot, đặt tên và username bot - Copy Bot Token (dạng
1234567890:ABCdef...) - Gửi tin nhắn cho bot → Gọi
https://api.telegram.org/bot{TOKEN}/getUpdatesđể lấy chat_id
Bước 2: Script Gửi Telegram
function sendTelegramAlert(message) {
try {
const props = PropertiesService.getScriptProperties();
const token = props.getProperty('TELEGRAM_BOT_TOKEN');
const chatId = props.getProperty('TELEGRAM_CHAT_ID');
if (!token || !chatId) throw new Error('Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID');
const url = 'https://api.telegram.org/bot' + token + '/sendMessage';
const payload = {
chat_id: chatId,
text: message,
parse_mode: 'HTML'
};
const response = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
const result = JSON.parse(response.getContentText());
if (!result.ok) throw new Error('Telegram API error: ' + result.description);
console.log('Telegram alert sent: ' + message.substring(0, 50));
return true;
} catch (err) {
console.error('sendTelegramAlert error: ' + err.toString());
return false;
}
}
// Setup credentials (chạy một lần)
function setupTelegramCredentials() {
const props = PropertiesService.getScriptProperties();
props.setProperty('TELEGRAM_BOT_TOKEN', 'YOUR_BOT_TOKEN');
props.setProperty('TELEGRAM_CHAT_ID', 'YOUR_CHAT_ID');
console.log('Credentials saved.');
}
Cảnh Báo Khi Tồn Kho Thấp
function checkInventoryAlerts() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Tồn Kho');
if (!sheet) return;
const data = sheet.getDataRange().getValues();
const headers = data[0];
const colName = headers.indexOf('Sản phẩm');
const colQty = headers.indexOf('Tồn kho');
const colMin = headers.indexOf('Mức tối thiểu');
const alerts = [];
for (let i = 1; i < data.length; i++) {
const qty = Number(data[i][colQty]);
const min = Number(data[i][colMin]);
if (qty <= min) {
alerts.push('⚠️ ' + data[i][colName] + ': còn ' + qty + ' (tối thiểu ' + min + ')');
}
}
if (alerts.length > 0) {
const msg = '🚨 CẢNH BÁO TỒN KHO
' + new Date().toLocaleString('vi-VN') + '
' + alerts.join('
');
sendTelegramAlert(msg);
}
}
Gửi Tin Nhắn Zalo OA
/**
* Gửi tin nhắn qua Zalo OA API
* Yêu cầu: Zalo OA account + access_token từ Zalo Developer Console
*/
function sendZaloAlert(userId, message) {
try {
const accessToken = PropertiesService.getScriptProperties().getProperty('ZALO_ACCESS_TOKEN');
if (!accessToken) throw new Error('Missing ZALO_ACCESS_TOKEN');
const response = UrlFetchApp.fetch('https://openapi.zalo.me/v3.0/oa/message/cs', {
method: 'post',
headers: {
'access_token': accessToken,
'Content-Type': 'application/json'
},
payload: JSON.stringify({
recipient: { user_id: userId },
message: { text: message }
}),
muteHttpExceptions: true
});
const result = JSON.parse(response.getContentText());
if (result.error !== 0) throw new Error('Zalo API error code: ' + result.error + ' — ' + result.message);
console.log('Zalo message sent to user: ' + userId);
return true;
} catch (err) {
console.error('sendZaloAlert error: ' + err.toString());
return false;
}
}
Trigger Kiểm Tra Theo Lịch
// Cài time-based trigger: chạy mỗi 30 phút
function setupAlertTrigger() {
ScriptApp.newTrigger('checkInventoryAlerts')
.timeBased()
.everyMinutes(30)
.create();
console.log('Alert trigger created — runs every 30 minutes.');
}
Use Cases
- Tồn kho thấp: Khi sản phẩm dưới ngưỡng → nhắn Telegram cho bộ phận mua hàng.
- Thanh toán quá hạn: Hàng ngày check hóa đơn chưa thanh toán → gửi Zalo nhắc nhở.
- Doanh thu bất thường: Khi doanh thu ngày < 50% trung bình → cảnh báo ngay cho sales.
- Lỗi script: Wrap try/catch → gửi Telegram khi script gặp lỗi nghiêm trọng.
Chia sẻ bài viết:
Tuân Hoang
Đội ngũ SheetStore
Google SheetsGoogle Apps ScriptCRMAutomationPhần mềm quản lý doanh nghiệp
Google Workspace Certified, 5+ years experience
Bạn thấy bài viết hữu ích?
Đăng ký nhận thông báo khi có bài viết mới.