-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (66 loc) · 2.57 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
const Cli = require('matrix-appservice-bridge').Cli;
const Bridge = require('matrix-appservice-bridge').Bridge;
const AppServiceRegistration = require('matrix-appservice-bridge').AppServiceRegistration;
const AppServiceBot = require('matrix-appservice-bridge').AppServiceBot;
const tgApi = require('node-telegram-bot-api');
const runBridge = (port, config) => {
// Maps Telegram group name -> Telegram chat ID
const chatIds = {};
const tg = new tgApi(config.telegram_api_token, {
polling: config.telegram_enable_polling
});
let bridge;
tg.on('message', (e) => {
console.log('got tg message', e);
const name = e.from.username || `${e.from.first_name} ${e.from.last_name}`;
chatIds[e.chat.title] = e.chat.id;
var intent = bridge.getIntent(`@telegram_${name}:${config.matrix_domain}`);
intent.setDisplayName(`${name} (Telegram)`);
intent.sendMessage(config.matrix_room_id, {
msgtype: 'm.text',
body: e.text,
});
});
bridge = new Bridge({
homeserverUrl: config.matrix_homeserver_url,
domain: config.matrix_domain,
registration: "telegram-registration.yaml",
controller: {
onUserQuery: (queriedUser) => {
console.log('onUserQuery:', queriedUser);
return {}; // auto-provision users with no additonal data
},
onEvent: (request, context) => {
var event = request.getData();
if (event.type !== "m.room.message" || !event.content || event.room_id !== config.matrix_room_id) {
console.log('skipping msg', event);
return;
}
if (!chatIds[config.telegram_group_name]) {
console.error(`I haven't learned the chat ID of '${config.telegram_group_name}' yet!`);
console.error(`Invite me to '${config.telegram_group_name}' and greet me in the room so I can learn the chat ID!`);
return;
}
const text = `<${event.user_id}>: ${event.content.body}`;
tg.sendMessage(chatIds[config.telegram_group_name], text);
}
}
});
console.log("Matrix-side listening on port %s", port);
bridge.run(port, config);
};
new Cli({
registrationPath: 'telegram-registration.yaml',
generateRegistration: (reg, callback) => {
reg.setId(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setSenderLocalpart('telegram');
reg.addRegexPattern('users', '@telegram_.*', true);
callback(reg);
},
bridgeConfig: {
schema: 'telegram-config-schema.yaml'
},
run: runBridge
}).run();