-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
157 lines (148 loc) · 4.62 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
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
const {
MatrixAppServiceBridge: {
Cli, AppServiceRegistration
},
Puppet,
MatrixPuppetBridgeBase
} = require("matrix-puppet-bridge");
const GroupMeClient = require('./client');
const config = require('./config.json');
const path = require('path');
const puppet = new Puppet(path.join(__dirname, './config.json' ));
const debug = require('debug')('matrix-puppet:keepalive');
const keepalive = ({ ping, reconnect }) => {
const now = () => new Date().getTime();
let lastPingRx = now();
const run = () => {
const ts = now() - lastPingRx;
if (ts > 30000) {
debug('time since last contact', ts, 'pinging');
ping().then(({time}) => {
debug('got reply in', time);
lastPingRx = now();
setTimeout(run, 5000);
}).catch(()=>{
debug('ping timeout! reconnecting', reconnect);
reconnect();
});
} else if (ts > 50000) {
debug('time since last contact', ts, 'reconnecting');
reconnect();
} else {
setTimeout(run, 5000);
}
};
run();
};
class App extends MatrixPuppetBridgeBase {
getServicePrefix() {
return "groupme";
}
createClient() {
return new GroupMeClient(this.config.groupme.accessToken);
}
initThirdPartyClient(client) {
this.userId = null;
this.client = client || this.createClient();
return this.client.connect().then(() => {
return this.client.api.getMe();
}).then(user => {
this.userId = user.id;
return this.client.subscribe(`/user/${user.id}`);
}).then(userSub => {
keepalive({
ping: () => userSub.send({ type: 'ping' }, 30000),
reconnect: () => this.initThirdPartyClient(this.client)
});
console.log('Subscribed to GroupMe user messages');
userSub.on('line.create', (data) => {
const { subject: { group_id, user_id, text, name } } = data;
const isMe = user_id === this.userId;
return this.handleThirdPartyRoomMessage({
roomId: group_id,
senderName: name,
senderId: isMe ? undefined : user_id,
text
}).catch(err => {
console.error(err.stack);
});
});
userSub.on('direct_message.create', (data) => {
const { subject: { chat_id, sender_id, text, name } } = data;
const isMe = sender_id === this.userId;
return this.handleThirdPartyRoomMessage({
roomId: chat_id,
senderName: isMe ? undefined : name,
senderId: isMe ? undefined : sender_id,
text
}).catch(err => {
console.error(err.stack);
});
});
});
}
getThirdPartyRoomDataById(id) {
if (this.isDirectChat(id)) {
return this.client.api.getChats(id).then(chats=>{
return chats.find(c=>c.last_message.conversation_id === id);
}).then(chat=>({
name: chat.other_user.name, topic: 'GroupMe Direct Message'
}));
} else {
return this.client.api.showGroup(id).then(data=>({
name: data.name, topic: data.description
}));
}
}
isDirectChat(id){
return id.split('+').length > 1;
}
getRecipientFromDirectChatId(cid){
return cid.split('+').filter(id => id !== this.userId)[0];
}
sendMessageAsPuppetToThirdPartyRoomWithId(id, text) {
if (this.isDirectChat(id)) {
const rid = this.getRecipientFromDirectChatId(id);
return this.client.api.sendDirectMessage(rid)(text);
} else {
return this.client.api.sendGroupMessage(id)(text);
}
}
sendReadReceiptAsPuppetToThirdPartyRoomWithId() {
// not available for now
}
sendTypingEventAsPuppetToThirdPartyRoomWithId() {
// avoiding UnhandledPromiseRejectionWarning:
}
}
new Cli({
port: config.port,
registrationPath: config.registrationPath,
generateRegistration: function(reg, callback) {
puppet.associate().then(()=>{
reg.setId(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setSenderLocalpart("groupmebot");
reg.addRegexPattern("users", "@groupme_.*", true);
reg.addRegexPattern("aliases", "#groupme_.*", false);
callback(reg);
}).catch(err=>{
console.error(err.message);
process.exit(-1);
});
},
run: function(port) {
const app = new App(config, puppet);
return puppet.startClient().then(()=>{
return app.initThirdPartyClient();
}).then(() => {
return app.bridge.run(port, config);
}).then(()=>{
console.log('Matrix-side listening on port %s', port);
}).catch(err=>{
console.error(err.message);
process.exit(-1);
});
}
}).run();