-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
matrix-googlevoice-bot.js
349 lines (321 loc) · 14.7 KB
/
matrix-googlevoice-bot.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const config = require('./config.js')
const botNotifyRoom = `${config.matrixBotId.split(':')[0]}-${config.aliasSuffix}`
const [Black, Red, Green, Yellow, Blue, Magenta, Cyan, White] = ["\x1b[30m", "\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m", "\x1b[37m"]
const JP = (text) => JSON.stringify(text, null, 2) // JSON prettify
const Log = (text, color = White) => {
if (config.logging && (config.imapNoopLogging || !text.includes('NOOP'))) {
let timestamp = new Date((datetime = new Date()).getTime() - datetime.getTimezoneOffset() * 60000).toISOString()
.replace("T", " ").split('.')[0]
console.log(`${timestamp} ${color}${text}${White}`);
}
}
Log(JP(config)) /// added
//! Replies via gmail-send
const gVoiceReply = async (room, text) => {
let alias = await matrixClient.getPublishedAlias(room);
let subject = await matrixClient.getRoomStateEvent(room, 'm.room.topic')
if (typeof alias != 'undefined' && alias.includes('@txt.voice.google.com')) {
let data = {
user: config.gmailId, pass: config.gmailPw,
to: alias.split(/[#:]+/)[1],
subject: subject, text: text
};
Log(`GMAIL (OUT): "${text}" to ${data.to.split('.')[0]}`, Magenta);
require('gmail-send')(data)(() => { });
}
}
//! Matrix via matrix-bot-sdk
const { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } = require("matrix-bot-sdk")
const storage = new SimpleFsStorageProvider("bot.json")
const matrixSendMessage = async (from, data) => {
from.address += config.aliasSuffix /// added
Log(`matrixSendMessage:\nfrom:${JP(from)}\ndata:${JP(data)}`, Cyan) /// added
const getRoom = (alias) => { return matrixClient.resolveRoom(alias).catch((e) => { return e.statusCode }) }
const createRoom = (name, alias) => {
Log(`createRoom:\nname: ${name}\nalias: ${alias}`, Cyan) /// added
return matrixClient.createRoom({
name: name.replace(/$| \(SMS\)/, ' (GV)'),
invite: config.matrixYourIds,
is_direct: true,
room_alias_name: alias,
topic: `Google Voice bridge with ${name}`,
preset: "trusted_private_chat"
//, power_level_content_override: { users_default: 100 }
}).catch((e) => { return e.statusCode })
}
Log(`getRoom(#${from.address}:${config.matrixDomain})`, Cyan)
var room = await getRoom(`#${from.address}:${config.matrixDomain}`)
Log(`got room=${room}`, Cyan) /// added
if (room > 0) { // create room if doesn't already exist (because got status code so room > 0)
room = await createRoom(from.name, from.address);
await matrixClient.sendStateEvent(room, 'm.room.member', config.matrixBotId,
{
displayname: from.name,
membership: 'join'
})
if (config.roomAvatarURL) {
await matrixClient.sendStateEvent(room, 'm.room.avatar', '', {
url: config.roomAvatarURL //set room avatar google voice
});
}
}
Log(`MATRIX (OUT): ${JP({ room, data })}`, Blue)
matrixClient.sendMessage(room, data);
}
const matrixNotify = async (text, color, emoji = '🤖') => {
try {
await matrixSendMessage({ address: `${botNotifyRoom}`, name: config.matrixBotName },
{
body: body = `${emoji} <code>${text}</code>`,
formatted_body: body, msgtype: 'm.notice', format: "org.matrix.custom.html"
});
Log(text, color);
} catch (error) {
console.error('Error in matrixNotify:', error);
}
}
const getAvatarUrl = async (url) => {
let mxcURL = '';
if (url.startsWith('mxc://')) { mxcURL = url }
else if (url.startsWith('http')) {
mxcURL = await matrixClient.uploadContentFromUrl(url)
}
return mxcURL
}
const setRoomAvatar = async (roomId, url) => {
let mxcURL = await getAvatarUrl(url);
if (mxcURL) {
await matrixClient.sendStateEvent(roomId, 'm.room.avatar', '', { url: mxcURL }); // room avatar
await matrixClient.sendStateEvent(roomId, 'm.room.member', config.matrixBotId, { avatar_url: mxcURL, membership: "join" }); // bot room avatar
}
}
var matrixClient;
const startNewMatrixClient = () => {
matrixClient = new MatrixClient(config.matrixServerUrl, config.matrixBotAccessToken, storage);
AutojoinRoomsMixin.setupOnClient(matrixClient);
matrixClient.start().then(() => { matrixNotify("MATRIX: connected.", Green, '🟢'); });
matrixClient.on("room.message", async (room, event) => {
Log(`matrixClient room.message:\nroom:${JP(room)}\nevent:${JP(event)}`, Cyan) /// added
if (!event.content) return;
let sender = event.sender;
let body = event.content.body;
if (sender != config.matrixBotId && event.type == 'm.room.message') {
Log(`MATRIX (IN): ${JP(event)}`, Blue);
if (body.startsWith('!')) {
let [cmd, arg = ''] = body.split(/ (.*)/g)
if (cmd == '!help') {
await matrixClient.sendMessage(room, {
body: msg =
'<code>!name <string></code> Set room name<br>' +
'<code>!botname <string></code> Set bot name (in all rooms)<br>' +
'<code>!botnick <string></code> Set bot nickname (in current room)<br>' +
'<code>!avatar <public URL></code> Set room & bot room avatar<br>' +
'<code>!show <MXC URL></code> Display a given MXC URL<br>' +
'<code>!echo <text></code> Check if alive<br>' +
'<code>!restart</code> Restart IMAP & Matrix connections<br>' +
'<code>!help</code> Show this list<br>',
msgtype: "m.text", format: "org.matrix.custom.html",
formatted_body: msg
});
}
else if (cmd == '!avatar' && arg) {
setRoomAvatar(room, arg)
}
else if (cmd == '!botname' && arg) {
await matrixClient.setDisplayName(arg)
}
else if (cmd == '!botnick' && arg) {
await matrixClient.sendStateEvent(room, 'm.room.member', config.matrixBotId, { displayname: arg, membership: 'join' })
}
else if (cmd == '!name' && arg) {
await matrixClient.sendStateEvent(room, 'm.room.name', '', { name: arg })
}
else if (cmd == '!show' && arg) {
await matrixClient.sendMessage(room, {
msgtype: "m.image",
url: `mxc://${config.matrixDomain}/${arg}`,
body: 'image'
})
}
else if (cmd == "!echo") {
await matrixClient.sendMessage(room, {
"msgtype": "m.notice",
"body": arg ? arg : 'Hi!',
});
}
else if (cmd == "!restart") {
await mailClient.stop(); //startNewMailClient();
await matrixClient.stop(); startNewMatrixClient();
}
} else if (await matrixClient.getPublishedAlias(room) != `#${botNotifyRoom}:${config.matrixDomain}`) {
gVoiceReply(room, body)
}
}
});
}
//! INCOMING via Gmail IMAP watch from https://github.com/chirag04/mail-listener2/blob/master/index.js
const Imap = require('imap')
const EventEmitter = require('events').EventEmitter
const simpleParser = require('mailparser').simpleParser
const async = require('async')
const { auth } = require("googleapis/build/src/apis/abusiveexperiencereport")
class MailListener extends EventEmitter {
constructor(options) {
super()
this.markSeen = !!options.markSeen
this.mailbox = options.mailbox || 'INBOX'
if ('string' === typeof options.searchFilter) { this.searchFilter = [options.searchFilter] }
else { this.searchFilter = options.searchFilter || ['UNSEEN']; }
this.fetchUnreadOnStart = !!options.fetchUnreadOnStart
this.mailParserOptions = options.mailParserOptions || {}
if (options.attachments && options.attachmentOptions && options.attachmentOptions.stream) {
this.mailParserOptions.streamAttachments = true
}
this.attachmentOptions = options.attachmentOptions || {}
this.attachments = options.attachments || false
this.attachmentOptions.directory = (this.attachmentOptions.directory ? this.attachmentOptions.directory : '')
this.imap = new Imap({
keepalive: config.imapKeepalive,
xoauth2: options.xoauth2, user: options.username, password: options.password, host: options.host,
port: options.port, tls: options.tls, tlsOptions: options.tlsOptions || {},
connTimeout: options.connTimeout || null, authTimeout: options.authTimeout || null,
debug: options.debug || null
})
this.imap.once('ready', this.imapReady.bind(this))
this.imap.once('close', this.imapClose.bind(this))
this.imap.on('error', this.imapError.bind(this))
}
start() { this.imap.connect() }
stop() { this.imap.end() }
imapReady() {
this.imap.openBox(this.mailbox, false, (err, mailbox) => {
if (err) { this.emit('error', err); }
else {
this.emit('server', 'connected')
this.emit('mailbox', mailbox)
if (this.fetchUnreadOnStart) { this.parseUnread.call(this); }
let listener = this.imapMail.bind(this)
this.imap.on('mail', listener)
this.imap.on('update', listener)
}
})
}
imapClose() { this.emit('server', 'disconnected'); }
imapError(err) { this.emit('error', err); }
imapMail() { this.parseUnread.call(this); }
parseUnread() {
let self = this
self.imap.search(self.searchFilter, (err, results) => {
if (results.length === 0) {
self.emit('no_results');
}
if (err) { self.emit('error', err); }
else if (results.length > 0) {
async.each(results, (result, callback) => {
let f = self.imap.fetch(result, { bodies: '', markSeen: self.markSeen })
f.on('message', (msg, seqno) => {
msg.on('body', async (stream, info) => {
let parsed = await simpleParser(stream)
Log(`mail: ${JP(parsed)}`)
let from = parsed.from.value[0]
self.emit('mail', from, parsed.text, parsed.subject)
if (parsed.attachments.length > 0) {
for (let att of parsed.attachments) {
if (self.attachments) { self.emit('attachment', from, att); }
else { self.emit('attachment', from, null); }
}
}
})
})
f.once('error', (err) => { self.emit('error', err); })
}, (err) => { if (err) { self.emit('error', err); } })
}
})
}
}
var mailClient
const startNewMailClient = () => {
mailClient = new MailListener({
username: config.gmailId, password: config.gmailPw, host: 'imap.gmail.com',
port: 993, tls: true, tlsOptions: { servername: 'imap.gmail.com' },
connTimeout: 10000, authTimeout: 5000,
mailbox: config.imapSearchFolder || "INBOX",
searchFilter: [
['UNSEEN'],
['or', ['FROM', 'txt.voice.google.com'], ['FROM', 'voice-noreply@google.com']],
//from:(txt.voice.google.com OR voice-noreply@google.com)
["SINCE", new Date().getTime() - 86400000 * config.backDays]
],
// searchFilter: [['FROM', 'txt.voice.google.com'], ["SINCE", new Date().getTime()-24*60*60*1000*10]], // for testing
fetchUnreadOnStart: true, markSeen: true,
attachments: true, attachmentOptions: { directory: "attachments/" },
debug: config.imapLogging ? Log : false
});
mailClient.start();
mailClient.on("no_results", () => {
Log("GMAIL: No new emails found.", Yellow);
});
mailClient.on("mail", async (from, text, subject) => {
Log(`GMAIL (in): ${JP({ text, from, subject })}`, Red);
let data = { msgtype: 'm.text' };
let bodytxt = text.replace(/[\s\S]*\n<https:\/\/voice\.google\.com>\n([\s\S]*?)(?:\nTo respond to this[\s\S]*?\.)?\nYOUR ACCOUNT <https:\/\/voice\.google\.com>[\s\S]*/i, '$1').trim();
if (from.address.startsWith('voice-noreply@google.com')) {
if (subject.startsWith("New text message from")) {
// Extract the number from the subject line
let numberMatch = subject.match(/(\d+)$/);
if (numberMatch) {
let number = numberMatch[1];
from = {
name: number,
address: `${botNotifyRoom}_${number}`
};
} else {
// Handle the case when the number is not found in the subject line
from = {
name: config.matrixBotName,
address: `${botNotifyRoom}`
};
data.formatted_body = `<h5>${subject}</h5>` + bodytxt.replace('\n\n', '<br>')
.replace(/^(.*)\n<(http.*)>/gm, '<br>🔗 <code><a href="$2">$1</a></code>').trim();
data.format = "org.matrix.custom.html";
}
} else {
from = {
name: config.matrixBotName,
address: `${botNotifyRoom}`
};
data.formatted_body = `<h5>${subject}</h5>` + bodytxt.replace('\n\n', '<br>')
.replace(/^(.*)\n<(http.*)>/gm, '<br>🔗 <code><a href="$2">$1</a></code>').trim();
data.format = "org.matrix.custom.html";
}
}
data.body = bodytxt.replace(/([a-z])\n/g, '$1 ');
Log(`MSG: ${JP(data)}`, Magenta);
matrixSendMessage(from, data);
});
mailClient.on("server", async (status) => {
if (status == 'connected') {
matrixNotify(`GMAIL: ${status}`, Magenta, '🟢');
} else if (status == 'disconnected') {
matrixNotify('GMAIL: disconnected, attempting reconnection in 10s...', Red, '🔴');
setTimeout(startNewMailClient, 1000 * 10);
} else { matrixNotify(`GMAIL: ${status}`, Magenta) }
});
mailClient.on("error", (err) => {
matrixNotify(`GMAIL ${err}\nAttempting reconnection in 10s...`, Yellow, '⚠️');
mailClient.stop();
setTimeout(startNewMailClient, 1000 * 10);
});
mailClient.on("attachment", async (from, att) => {
Log(`GMAIL (IN) Attachment: ${JP({ size: att.size, contentType: att.contentType })}`, Red)
if (att) {
let name = `attachment.${att.contentType.split('/')[1]}`;
let url = await matrixClient.uploadContent(Buffer.from(att.content, 'base64'), att.contentType, name);
matrixSendMessage(from, {
msgtype: "m.image", url: url, body: name
});
}
});
}
startNewMatrixClient();
startNewMailClient();