Added Telegram bot login (#4650)
Ylian Saint-Hilaire committed
Oct 22, 2022 at 22:05 UTC
b57224996a65d77b9bffc2e015a7413de2d6ca4f
1 file changed
+73
-33
meshmessaging.js
+73
-33
@@ -15,7 +15,7 @@
15
"use strict";
16
17
/*
18
-// For Telegram, add this in config.json
18
+// For Telegram user login, add this in config.json
19
"messaging": {
20
"telegram": {
21
"apiid": 00000000,
@@ -23,6 +23,15 @@
23
"session": "aaaaaaaaaaaaaaaaaaaaaaa"
24
}
25
}
26
+
27
+// For Telegram bot login, add this in config.json
28
+"messaging": {
29
+ "telegram": {
30
+ "apiid": 00000000,
31
+ "apihash": "00000000000000000000000",
32
+ "bottoken": "00000000:aaaaaaaaaaaaaaaaaaaaaaaa"
33
+ }
34
+}
35
*/
36
37
// Construct a messaging server object
@@ -38,7 +47,7 @@ module.exports.CreateServer = function (parent) {
47
var telegramOK = true;
48
if (typeof parent.config.messaging.telegram.apiid != 'number') { console.log('Invalid or missing Telegram apiid.'); telegramOK = false; }
49
if (typeof parent.config.messaging.telegram.apihash != 'string') { console.log('Invalid or missing Telegram apihash.'); telegramOK = false; }
41
- if (typeof parent.config.messaging.telegram.session != 'string') { console.log('Invalid or missing Telegram session.'); telegramOK = false; }
50
+ if ((typeof parent.config.messaging.telegram.session != 'string') && (typeof parent.config.messaging.telegram.bottoken != 'string')) { console.log('Invalid or missing Telegram session or bottoken.'); telegramOK = false; }
51
52
if (telegramOK) {
53
// Setup Telegram
@@ -48,12 +57,24 @@ module.exports.CreateServer = function (parent) {
57
const { Logger } = require('telegram/extensions/Logger');
58
const logger = new Logger({ LogLevel : 'none' });
59
const input = require('input');
51
- const stringSession = new StringSession(parent.config.messaging.telegram.session);
52
- const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
53
- await client.start({ onError: function (err) { console.log('Telegram error', err); } });
54
- obj.telegramClient = client;
55
- obj.providers += 1; // Enable Telegram messaging
56
- console.log("MeshCentral Telegram client is connected.");
60
+ var client;
61
+ if (parent.config.messaging.telegram.bottoken == null) {
62
+ // User login
63
+ var stringSession = new StringSession(parent.config.messaging.telegram.session);
64
+ const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
65
+ await client.start({ onError: function (err) { console.log('Telegram error', err); } });
66
+ obj.telegramClient = client;
67
+ obj.providers += 1; // Enable Telegram messaging
68
+ console.log("MeshCentral Telegram client is user connected.");
69
+ } else {
70
+ // Bot login
71
+ var stringSession = new StringSession('');
72
+ const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
73
+ await client.start({ botAuthToken: parent.config.messaging.telegram.bottoken, onError: function (err) { console.log('Telegram error', err); } });
74
+ obj.telegramClient = client;
75
+ obj.providers += 1; // Enable Telegram messaging
76
+ console.log("MeshCentral Telegram client is bot connected.");
77
+ }
78
}
79
setupTelegram();
80
}
@@ -169,31 +190,50 @@ module.exports.SetupTelegram = async function (parent) {
190
}
191
192
// If the session value is missing, perform the process to get it
172
- if ((parent.config.messaging.telegram.session == null) || (parent.config.messaging.telegram.session == '') || (typeof parent.config.messaging.telegram.session != 'string')) {
173
- const { TelegramClient } = require('telegram');
174
- const { StringSession } = require('telegram/sessions');
175
- const { Logger } = require('telegram/extensions/Logger');
176
- const logger = new Logger({ LogLevel : 'none' });
177
- const input = require('input');
178
- const stringSession = new StringSession('');
179
- const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
180
- await client.start({
181
- phoneNumber: async function () { return await input.text("Please enter your number (+1-111-222-3333): "); },
182
- password: async function () { return await input.text("Please enter your password: "); },
183
- phoneCode: async function () { return await input.text("Please enter the code you received: "); },
184
- onError: function (err) { console.log('Telegram error', err); }
185
- });
186
- console.log('Set this session value in the messaging section of the config.json like this:');
187
- console.log('{');
188
- console.log(' "messaging": {');
189
- console.log(' "telegram": {');
190
- console.log(' "apiid": ' + parent.config.messaging.telegram.apiid + ',');
191
- console.log(' "apihash": "' + parent.config.messaging.telegram.apihash + '",');
192
- console.log(' "session": "' + client.session.save() + '"');
193
- console.log(' }');
194
- console.log(' }');
195
- console.log('}');
196
- process.exit();
193
+ if (((parent.config.messaging.telegram.session == null) || (parent.config.messaging.telegram.session == '') || (typeof parent.config.messaging.telegram.session != 'string')) && ((parent.config.messaging.telegram.bottoken == null) || (parent.config.messaging.telegram.bottoken == '') || (typeof parent.config.messaging.telegram.bottoken != 'string'))) {
194
+ if (parent.args.setuptelegram == 'user') {
195
+ const { TelegramClient } = require('telegram');
196
+ const { StringSession } = require('telegram/sessions');
197
+ const { Logger } = require('telegram/extensions/Logger');
198
+ const logger = new Logger({ LogLevel: 'none' });
199
+ const input = require('input');
200
+ const stringSession = new StringSession('');
201
+ const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
202
+ await client.start({
203
+ phoneNumber: async function () { return await input.text("Please enter your number (+1-111-222-3333): "); },
204
+ password: async function () { return await input.text("Please enter your password: "); },
205
+ phoneCode: async function () { return await input.text("Please enter the code you received: "); },
206
+ onError: function (err) { console.log('Telegram error', err); }
207
+ });
208
+ console.log('Set this session value in the messaging section of the config.json like this:');
209
+ console.log('{');
210
+ console.log(' "messaging": {');
211
+ console.log(' "telegram": {');
212
+ console.log(' "apiid": ' + parent.config.messaging.telegram.apiid + ',');
213
+ console.log(' "apihash": "' + parent.config.messaging.telegram.apihash + '",');
214
+ console.log(' "session": "' + client.session.save() + '"');
215
+ console.log(' }');
216
+ console.log(' }');
217
+ console.log('}');
218
+ process.exit();
219
+ } else if (parent.args.setuptelegram == 'bot') {
220
+ console.log('Login to your Telegram account, search for "BotFather", message him and create a bot.');
221
+ console.log('Once you get the HTTP API token, add it in the config.json as "bottoken" like so:');
222
+ console.log('{');
223
+ console.log(' "messaging": {');
224
+ console.log(' "telegram": {');
225
+ console.log(' "apiid": ' + parent.config.messaging.telegram.apiid + ',');
226
+ console.log(' "apihash": "' + parent.config.messaging.telegram.apihash + '",');
227
+ console.log(' "bottoken": "00000000:aaaaaaaaaaaaaaaaaaaaaaaa"');
228
+ console.log(' }');
229
+ console.log(' }');
230
+ console.log('}');
231
+ process.exit();
232
+ } else {
233
+ console.log('run "--setuptelegram bot" to setup Telegram login as a bot (typical).');
234
+ console.log('run "--setuptelegram user" to setup Telegram login as a user.');
235
+ process.exit();
236
+ }
237
}
238
239
// All Telegram values seem ok