forked from virgaux/USBpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelnotif.js
More file actions
63 lines (57 loc) · 2.41 KB
/
telnotif.js
File metadata and controls
63 lines (57 loc) · 2.41 KB
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
require('dotenv').config()
if (process.env.TELEGRAM_NOTIF === 'true'){
const fs = require('fs')
const chalk = require('chalk');
const TeleBot = require('telebot');
const bot = new TeleBot({
token: process.env.TELEGRAM_TOKEN, // Required. Telegram Bot API token.
polling: {
proxy: ''
}
});
function startTG() {
return bot.start();
}
async function battlesummary(logSummary,tet,sleepingTime){
try {
message = 'Battle result summary: \n' + " " + new Date().toLocaleString() + ' \n' + tet.replace(/\u001b[^m]*?m/g,"") + ' \n';
for (let i = 0; i < logSummary.length; i++) {
message = message + logSummary[i].replace(/\u001b[^m]*?m/g,"") +' \n';
}
message = message + ' \n' + ' Next battle in '+ sleepingTime / 1000 / 60 + ' minutes at ' + new Date(Date.now() +sleepingTime).toLocaleString() + ' \n';
message = message + ' \n' +'Telegram https://t.me/ultimatesplinterlandsbot' + ' \n' + 'Discord https://discord.gg/hwSr7KNGs9'
const max_size = 4096
var messageString = message
var amount_sliced = messageString.length / max_size
var start = 0
var end = max_size
if (amount_sliced>1) {
for (let i = 0; i < amount_sliced; i++) {
message = messageString.slice(start, end)
await bot.sendMessage(process.env.TELEGRAM_CHATID, message)
start = start + max_size
end = end + max_size
}
} else {
await bot.sendMessage(process.env.TELEGRAM_CHATID, message)
}
//notify.send(message);
console.log(chalk.green(' \n' + ' Battle result sent to telegram'));
} catch (e) {
console.log(e);
console.log(chalk.red(' [ERROR] Unable to send battle result to Telegram. Please make sure telegram setting is correct.'));
bot.sendMessage(process.env.TELEGRAM_CHATID, ' [ERROR] Unable to send battle result to Telegram. Please make sure telegram setting is correct.');
return;
}
message = '';
}
function sender (logMessage) {
bot.sendMessage(process.env.TELEGRAM_CHATID, logMessage);
//notify.send(logMessage);
logMessage= '';
return
}
module.exports.startTG = startTG;
exports.sender =sender;
exports.battlesummary = battlesummary;
}