-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
64 lines (48 loc) · 1.42 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
const init = require('./init');
const { login, register, logout } = require('./auth');
const getConfig = require('./get-config')
const { getChat, getNewMessages, forwardMessages } = require('./chat-history')
const myStorage = require('./storage')
async function wait(timeout = 3000) {
return new Promise((resolve) => {
setTimeout(resolve, timeout)
})
}
async function boot() {
try {
const telegram = await init()
// const out = await logout(telegram)
// console.log('logout', out)
// const user = await login(telegram)
// console.log('user', user)
const source = await getChat(telegram, 'source')
console.log('Source Chat:', source.title)
const destination = await getChat(telegram, 'destination')
console.log('Destination Chat:', source.title)
run(telegram, source, destination)
} catch (e) {
console.error(e)
process.exit(1)
}
}
async function run(telegram, source, destination) {
try {
do {
await forwardNew(telegram, source, destination)
await wait()
} while (true)
} catch (e) {
console.error(e)
await wait()
}
}
async function forwardNew(telegram, source, destination) {
const storage = await myStorage()
const messages = await getNewMessages(telegram, source)
if (messages.length === 0) {
return
}
await forwardMessages(telegram, messages, destination, source)
storage.set('messages:last', messages[0].id)
}
boot()