Removed unused file.
Ylian Saint-Hilaire committed
Oct 22, 2022 at 07:24 UTC
68504f36eddaae3066edaaa3b3bee976afef76df
1 file changed
-136
telegram.js
deleted
-136
@@ -1,136 +0,0 @@
1
-/**
2
-* @description MeshCentral Telegram communication module
3
-* @author Ylian Saint-Hilaire
4
-* @copyright Intel Corporation 2018-2022
5
-* @license Apache-2.0
6
-* @version v0.0.1
7
-*/
8
-
9
-/*xjslint node: true */
10
-/*xjslint plusplus: true */
11
-/*xjslint maxlen: 256 */
12
-/*jshint node: true */
13
-/*jshint strict: false */
14
-/*jshint esversion: 6 */
15
-"use strict";
16
-
17
-/*
18
- "telegram": {
19
- "apiid": 0000000,
20
- "apihash": "hexvalue",
21
- "session": "base64value"
22
- }
23
-*/
24
-
25
-// Construct a Telegram server object
26
-module.exports.CreateServer = function (parent) {
27
- var obj = {};
28
- obj.parent = parent;
29
-
30
- // Check that we have the correct values
31
- if (typeof parent.config.telegram != 'object') return null;
32
- if (typeof parent.config.telegram.apiid != 'number') return null;
33
- if (typeof parent.config.telegram.apihash != 'string') return null;
34
- if (typeof parent.config.telegram.session != 'string') return null;
35
-
36
- // Connect to the telegram server
37
- async function connect() {
38
- const { TelegramClient } = require('telegram');
39
- const { StringSession } = require('telegram/sessions');
40
- const input = require('input');
41
-
42
- const stringSession = new StringSession(parent.config.telegram.session);
43
- const client = new TelegramClient(stringSession, parent.config.telegram.apiid, parent.config.telegram.apihash, { connectionRetries: 5 });
44
- await client.start({
45
- phoneNumber: async function () { await input.text("Please enter your number: "); },
46
- password: async function () { await input.text("Please enter your password: "); },
47
- phoneCode: async function () { await input.text("Please enter the code you received: "); },
48
- onError: function (err) { console.log('Telegram error', err); },
49
- });
50
- console.log("MeshCentral Telegram session is connected.");
51
- obj.client = client;
52
- //console.log(client.session.save()); // Save this string to avoid logging in again
53
- }
54
-
55
- // Send an Telegram message
56
- obj.sendMessage = async function (to, msg, func) {
57
- if (obj.client == null) return;
58
- parent.debug('email', 'Sending Telegram to: ' + to + ': ' + msg);
59
- await client.sendMessage(to, { message: msg });
60
- func(true);
61
- }
62
-
63
- // Get the correct SMS template
64
- function getTemplate(templateNumber, domain, lang) {
65
- parent.debug('email', 'Getting SMS template #' + templateNumber + ', lang: ' + lang);
66
- if (Array.isArray(lang)) { lang = lang[0]; } // TODO: For now, we only use the first language given.
67
-
68
- var r = {}, emailsPath = null;
69
- if ((domain != null) && (domain.webemailspath != null)) { emailsPath = domain.webemailspath; }
70
- else if (obj.parent.webEmailsOverridePath != null) { emailsPath = obj.parent.webEmailsOverridePath; }
71
- else if (obj.parent.webEmailsPath != null) { emailsPath = obj.parent.webEmailsPath; }
72
- if ((emailsPath == null) || (obj.parent.fs.existsSync(emailsPath) == false)) { return null }
73
-
74
- // Get the non-english email if needed
75
- var txtfile = null;
76
- if ((lang != null) && (lang != 'en')) {
77
- var translationsPath = obj.parent.path.join(emailsPath, 'translations');
78
- var translationsPathTxt = obj.parent.path.join(emailsPath, 'translations', 'sms-messages_' + lang + '.txt');
79
- if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathTxt)) {
80
- txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString();
81
- }
82
- }
83
-
84
- // Get the english email
85
- if (txtfile == null) {
86
- var pathTxt = obj.parent.path.join(emailsPath, 'sms-messages.txt');
87
- if (obj.parent.fs.existsSync(pathTxt)) {
88
- txtfile = obj.parent.fs.readFileSync(pathTxt).toString();
89
- }
90
- }
91
-
92
- // No email templates
93
- if (txtfile == null) { return null; }
94
-
95
- // Decode the TXT file
96
- var lines = txtfile.split('\r\n').join('\n').split('\n')
97
- if (lines.length <= templateNumber) return null;
98
-
99
- return lines[templateNumber];
100
- }
101
-
102
- // Send telegram user verification message
103
- obj.sendPhoneCheck = function (domain, to, verificationCode, language, func) {
104
- parent.debug('email', "Sending verification Telegram to " + to);
105
-
106
- var sms = getTemplate(0, domain, language);
107
- if (sms == null) { parent.debug('email', "Error: Failed to get SMS template"); return; } // No SMS template found
108
-
109
- // Setup the template
110
- sms = sms.split('[[0]]').join(domain.title ? domain.title : 'MeshCentral');
111
- sms = sms.split('[[1]]').join(verificationCode);
112
-
113
- // Send the SMS
114
- obj.sendMessage(to, sms, func);
115
- };
116
-
117
- // Send login token verification message
118
- obj.sendToken = function (domain, to, verificationCode, language, func) {
119
- parent.debug('email', "Sending login token Telegram to " + to);
120
-
121
- var sms = getTemplate(1, domain, language);
122
- if (sms == null) { parent.debug('email', "Error: Failed to get SMS template"); return; } // No SMS template found
123
-
124
- // Setup the template
125
- sms = sms.split('[[0]]').join(domain.title ? domain.title : 'MeshCentral');
126
- sms = sms.split('[[1]]').join(verificationCode);
127
-
128
- // Send the SMS
129
- obj.sendMessage(to, sms, func);
130
- };
131
-
132
- // Connect the Telegram session
133
- connect();
134
-
135
- return obj;
136
-};