forked from FruitieX/teleirc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg.js
223 lines (196 loc) · 8.53 KB
/
tg.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var Telegram = require('node-telegram-bot-api');
var fs = require('fs');
var path = require('path');
var irc = require('./irc');
var nodeStatic = require('node-static');
var mkdirp = require('mkdirp');
var crypto = require('crypto');
// tries to read chat ids from a file
var readChatIds = function(arr) {
console.log('\n');
console.log('NOTE!');
console.log('=====');
var idMissing = false;
try {
var json = JSON.parse(fs.readFileSync(process.env.HOME + '/.teleirc/chat_ids'));
for (var i = 0; i < arr.length; i++) {
var key = arr[i].tgGroup;
if (key in json) {
arr[i].tgChatId = json[key];
console.log('id found for:', key, ':', json[key]);
} else {
console.log('id not found:', key);
idMissing = true;
}
}
} catch (e) {
console.log('~/.teleirc/chat_ids file not found!');
idMissing = true;
}
if (idMissing) {
console.log(
'\nPlease add your Telegram bot to a Telegram group and have' +
'\nsomeone send a message to that group.' +
'\nteleirc will then automatically store your group chat_id.');
}
console.log('\n');
};
var writeChatIds = function(config) {
var json = {};
for (var i = 0; i < config.channels.length; i++) {
if (config.channels[i].tgChatId) {
json[config.channels[i].tgGroup] = config.channels[i].tgChatId;
}
}
json = JSON.stringify(json);
fs.writeFile(process.env.HOME + '/.teleirc/chat_ids', json, function(err) {
if (err) {
console.log('error while storing chat ID:');
console.log(err);
} else {
console.log('successfully stored chat ID in ~/.teleirc/chat_ids');
}
});
};
var getName = function(user, config) {
var name = config.nameFormat;
if (user.username) {
name = name.replace('%username%', user.username, 'g');
} else {
// if user lacks username, use fallback format string instead
name = name.replace('%username%', config.usernameFallbackFormat, 'g');
}
name = name.replace('%firstName%', user.first_name || '', 'g');
name = name.replace('%lastName%', user.last_name || '', 'g');
// get rid of leading and trailing whitespace
name = name.replace(/(^\s*)|(\s*$)/g, '');
return name;
};
function randomValueBase64(len) {
return crypto.randomBytes(Math.ceil(len * 3 / 4))
.toString('base64')
.slice(0, len)
.replace(/\+/g, '0')
.replace(/\//g, '0');
}
var serveFile = function(fileId, config, tg, callback) {
var randomString = randomValueBase64(config.mediaRandomLenght);
mkdirp(process.env.HOME + '/.teleirc/files/' + randomString);
tg.downloadFile(fileId, process.env.HOME + '/.teleirc/files/' +
randomString).then(function(filePath) {
callback(config.httpLocation + '/' + randomString + '/' + path.basename(filePath));
});
};
module.exports = function(config, sendTo) {
// start HTTP server for media files if configured to do so
if (config.showMedia) {
var fileServer = new nodeStatic.Server(process.env.HOME + '/.teleirc/files');
mkdirp(process.env.HOME + '/.teleirc/files');
require('http').createServer(function(req, res) {
req.addListener('end', function() {
fileServer.serve(req, res);
}).resume();
}).listen(config.httpPort);
}
var tg = new Telegram(config.tgToken, {polling: true});
readChatIds(config.channels);
tg.on('message', function(msg) {
var channel = config.channels.filter(function(channel) {
return channel.tgGroup === msg.chat.title;
})[0];
if (!channel) {
return;
}
if (!channel.tgChatId) {
console.log('storing chat ID: ' + msg.chat.id);
channel.tgChatId = msg.chat.id;
writeChatIds(config);
}
if (msg.text && !msg.text.indexOf('/names')) {
var names = sendTo.ircNames(channel);
names.sort();
names = 'Users on ' + (channel.chanAlias || channel.ircChan) + ':\n\n' +
names.join(', ');
return tg.sendMessage(channel.tgChatId, names);
}
// skip posts containing media if it's configured off
if ((msg.audio || msg.document || msg.photo || msg.sticker || msg.video ||
msg.voice || msg.contact || msg.location) && !config.showMedia) {
return;
}
var text;
if (msg.reply_to_message && msg.text) {
text = msg.text.replace(/\n/g , '\n<' + getName(msg.from, config) + '>: ');
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'@' + getName(msg.reply_to_message.from, config) + ', ' + text);
} else if (msg.audio) {
serveFile(msg.audio.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Audio, ' + msg.audio.duration + 's)' + url);
});
} else if (msg.document) {
serveFile(msg.document.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Document) ' + url);
});
} else if (msg.photo) {
// pick the highest quality photo
var photo = msg.photo[msg.photo.length - 1];
serveFile(photo.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Photo, ' + photo.width + 'x' + photo.height + ') ' + url);
});
} else if (msg.new_chat_photo) {
// pick the highest quality photo
var chatPhoto = msg.new_chat_photo[msg.new_chat_photo.length - 1];
serveFile(chatPhoto.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(New chat photo, ' + chatPhoto.width + 'x' + chatPhoto.height + ') ' + url);
});
} else if (msg.sticker) {
serveFile(msg.sticker.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Sticker, ' + msg.sticker.width + 'x' + msg.sticker.height + ') ' + url);
});
} else if (msg.video) {
serveFile(msg.video.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Video, ' + msg.video.duration + 's)' + url);
});
} else if (msg.voice) {
serveFile(msg.voice.file_id, config, tg, function(url) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Voice, ' + msg.voice.duration + 's)' + url);
});
} else if (msg.contact) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Contact, ' + '"' + msg.contact.first_name + ' ' +
msg.contact.last_name + '", ' +
msg.contact.phone_number + ')');
} else if (msg.location) {
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' +
'(Location, ' + 'lon: ' + msg.location.longitude +
', lat: ' + msg.location.latitude + ')');
} else if (msg.new_chat_participant) {
sendTo.irc(channel.ircChan, getName(msg.new_chat_participant, config) +
' was added by: ' + getName(msg.from, config));
} else if (msg.left_chat_participant) {
sendTo.irc(channel.ircChan, getName(msg.left_chat_participant, config) +
' was removed by: ' + getName(msg.from, config));
} else {
text = msg.text.replace(/\n/g , '\n<' + getName(msg.from, config) + '>: ');
sendTo.irc(channel.ircChan, '<' + getName(msg.from, config) + '>: ' + text);
}
});
sendTo.tg = function(channel, msg) {
console.log(' >> relaying to TG: ' + msg);
if (!channel.tgChatId) {
var err = 'ERROR: No chat_id set! Add me to a Telegram group ' +
'and say hi so I can find your group\'s chat_id!';
sendTo.irc(channel.ircChan, err);
console.error(err);
return;
}
tg.sendMessage(channel.tgChatId, msg);
};
};