forked from backmeupplz/voicy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendout.js
131 lines (120 loc) · 3.9 KB
/
sendout.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* File to sendout messages per languages
*/
// Dependencies
const mongoose = require('mongoose');
// Load env variables
require('dotenv').config({path: `${__dirname}/.env`});
// Connect to mongoose
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URL, {
server: {
socketOptions: {
socketTimeoutMS: 0,
connectTimeoutMS: 0,
},
},
});
mongoose.connection.on('disconnected', () => {
mongoose.connect(config.database, {
server: {
socketOptions: {
connectTimeoutMS: 0,
},
},
});
});
const db = require('./helpers/db');
const Telegram = require('node-telegram-bot-api');
const config = require('./config');
const bot = new Telegram(process.env.TOKEN, {
polling: false,
});
const msg = {
'ru': 'Привет всем! Огромное спасибо за то, что пользуетесь бесплатным @voicybot. !',
};
const languages = {
'wit': {
'Russian': 'ru',
},
'google': {
'ru-RU': 'ru',
},
'yandex': {
'ru-RU': 'ru',
},
};
function getLanguage(chat) {
let lan = 'en';
Object.keys(languages).forEach((engine) => {
if (engine === chat.engine) {
const object = languages[engine];
let chatLanguage;
if (engine === 'wit') {
chatLanguage = chat.witLanguage;
} else if (engine === 'google') {
chatLanguage = chat.googleLanguage;
} else {
chatLanguage = chat.yandexLanguage;
}
const lang = object[chatLanguage] || 'en';
lan = lang;
}
});
return lan;
}
function send(message, total, index, successes) {
if (index > total) {
console.log(`All sent! Successes:\n${JSON.stringify(successes)}`);
return;
}
db.getChats({ $or: [
{ witLanguage: 'Russian' },
{ googleLanguage: 'ru-RU' },
{ yandexLanguage: 'ru-RU' },
]})
.skip(index)
.limit(30)
.then((chats) => {
const promises = [];
chats.forEach((chat) => {
console.log(`(${index}) sent to ${chat.id}`);
const p = new Promise((resolve, reject) => {
bot.sendMessage(chat.id, message)
.then(() => resolve(1))
.catch((err) => {
console.error(`${chat.id}: ${err.message}`);
resolve(0);
});
});
promises.push(p);
});
return Promise.all(promises)
.then((results) => {
return results.reduce((prev, cur) => prev + cur, 0);
})
.then((count) => {
console.log(`${index + 30}/${total}`);
setTimeout(() => {
send(message, total, index + 30, successes + count);
}, 1500);
})
.catch(err => console.error(err));
})
.catch(err => console.error(err));
}
function sendMessages(message) {
console.log('Getting chats count...');
db.countChats({ $or: [
{ witLanguage: 'Russian' },
{ googleLanguage: 'ru-RU' },
{ yandexLanguage: 'ru-RU' },
]})
.then((c) => {
console.log(`Found ${c} chats. Sending the message...`);
send(message, c, 0, 0);
})
.catch(err => console.error(err));
}
console.log('Starting...');
sendMessages('Привет всем! Спасибо за активное использование @voicybot! Более 3.1 миллиона голосовых сообщений было переведено в текст с момента запуска бота — и все это абсолютно бесплатно и всегда будет бесплатным. Поблагодарить авторов бота очень просто: мы начали вести свой канал в Телеграме о путешествиях, политике, критическом мышлении, IT, криптовалютах и проектах, которыми сейчас занимаемся. Будем очень признательны, если вы подпишитесь: @golden_borodutch. Спасибо!');