-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
109 lines (76 loc) · 3.1 KB
/
index.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
import {Telegraf} from 'telegraf';
import dbus from 'dbus-next';
dbus.setBigIntCompat( true );
class Chat {
constructor( sendFn ) {
this.fns = [];
this.sendFn = sendFn;
}
_received( sender, message ) {
this.fns.forEach( fn=>fn(sender, message) );
}
send( sender, message ) {
this.sendFn( sender, message );
}
onMessage( fn ) {
this.fns.push( fn );
}
}
async function signalInit() {
const bus = dbus.sessionBus();
const obj = await bus.getProxyObject( 'org.asamk.Signal', '/org/asamk/Signal' );
const iface = obj.getInterface( "org.asamk.Signal" );
// console.log( iface.$methods );
// console.log( iface.$signals );
const chat = new Chat( (sender, message)=>{
iface.sendGroupMessage( `${sender}: ${message}`, [], targetGroup );
});
const targetGroup = Buffer.from( process.env.SIGNAL_GROUP, 'base64');
iface.on( 'SyncMessageReceived', async(timestamp, sender, destination, groupID, message, attachments)=>{
const senderName = await iface.getContactName( sender );
console.log( `[sync message] ${senderName} (${sender}): "${message}" in group ${groupID.toString("base64")}` );
if( targetGroup.compare(groupID) || !message ) { return; }
// iface.sendMessage( message, [], [sender] );
// iface.sendGroupMessage( `${senderName} (${sender}) just wrote: "${message}" here`, [], targetGroup );
chat._received( senderName, message );
});
iface.on( 'MessageReceived', async(timestamp, sender, groupID, message, attachments)=>{
const senderName = await iface.getContactName( sender );
console.log( `[message] ${senderName} (${sender}): "${message}" in group ${groupID.toString("base64")}` );
if( targetGroup.compare(groupID) || !message ) { return; }
// iface.sendMessage( message, [], [sender] );
// iface.sendGroupMessage( `${senderName} (${sender}) just wrote: "${message}" here`, [], targetGroup );
chat._received( senderName, message );
});
console.log( `Signal ready!` );
// bus.disconnect();
return chat;
}
async function telegramInit() {
const bot = new Telegraf( process.env.TELEGRAM_TOKEN );
const chat = new Chat( (sender, message)=>{
message = message.replace( /&/g, '&' ).replace( /</g, '<' ).replace( />/g, '>' );
bot.telegram.sendMessage( process.env.TELEGRAM_GROUP, `<b>${sender}</b>: ${message}`, {parse_mode:"HTML"} );
});
bot.on( 'text', ctx=>{
if( ! ctx.message.from ) { return; }
const {from} = ctx.message;
const namePieces = [from.first_name, from.last_name, from.username ? `@${from.username}` : ``];
const name = namePieces.filter( x=>!!x ).join( ` ` );
console.log( `${name}: "${ctx.message.text}" in ${ctx.message.chat.id} (${ctx.message.chat.title})` );
if( `${ctx.message.chat.id}` !== process.env.TELEGRAM_GROUP ) { return; }
chat._received( name, ctx.message.text );
});
bot.launch();
console.log( `Telegram ready!` );
return chat;
}
async function main() {
const telegram = await telegramInit();
const signal = await signalInit();
telegram.onMessage( (sender, message)=>signal.send(sender, message) );
signal.onMessage( (sender, message)=>telegram.send(sender, message) );
}
main().catch( (err)=>{
console.error( err );
});