forked from abdhass/telegram-channel-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-history.js
executable file
·120 lines (103 loc) · 3.21 KB
/
chat-history.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
const { pluck } = require('ramda')
const { inputField } = require('./utils/fixtures')
const config = require('./config')
const fetch = require("node-fetch")
const db = require('./utils/db');
const telegram = require('./utils/init')
const getChat = async () => {
const dialogs = await telegram('messages.getDialogs', {
limit: parseInt(config.telegram.getChat.limit,10),
})
const { chats } = dialogs
const selectedChat = await selectChat(chats)
console.log(selectedChat);
return selectedChat
}
const chatHistory = async (chat) => {
let lastIdofMsgs = await db.getLastMsgId();
const max = config.telegram.msgHistory.maxMsg
const limit = config.telegram.msgHistory.limit || 99
let offsetId = 0
let full = [],
messages = [];
do {
let history = await telegram('messages.getHistory', {
peer: {
_: 'inputPeerChannel',
channel_id: chat.id,
access_hash: chat.access_hash
},
max_id: -offsetId,
offset: -full.length,
limit
})
messages = history.messages.filter(filterLastDay);
full = full.concat(messages);
messages.length > 0 && (offsetId = messages[0].id);
if (messages.length > 0) {
await db.updateLastMsgId(messages[0].id)
}
history = null;
} while (messages.length === limit && full.length < max)
const showNew = full.filter(({ id }) => id > lastIdofMsgs)
const noRepeats = uniqueArray(showNew, 'id')
const usersMsg = noRepeats.filter(filterUsersMessages)
if (usersMsg.length>0){
const done = await sendToServer(usersMsg)
printMessages(usersMsg)
console.log("saved to server: ",done)
console.log("Last msg id ", messages[0].id)
}
lastIdofMsgs = await db.getLastMsgId();
const dt = new Date()
const hours = dt.getHours()
const mins = dt.getMinutes()
console.log( `${hours}:${mins} - [${lastIdofMsgs}]`)
}
let sent = []
const sendToServer = async (messages) => {
let toPush = messages.filter(m => {
return sent.indexOf( m ) < 0;
})
messages.forEach(m => {
sent.push(m.id)
});
const response = await fetch(config.server,
{
method: 'POST',
body: JSON.stringify(toPush),
headers: { "Content-Type": "application/json" }
})
const json = await response.json();
return json
}
const printMessages = (messages) => {
const formatted = messages.map(formatMessage)
formatted.forEach(e => console.log(e))
}
const uniqueArray = function(myArr, prop) {
return myArr.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
});
}
const filterLastDay = ({ date }) => new Date(date * 1e3) > dayRange()
const dayRange = () => Date.now() - new Date(86400000 * 4)
const filterUsersMessages = ({ _ }) => _ === 'message'
const formatMessage = ({ message, date, id }) => {
const dt = new Date(date * 1e3)
const hours = dt.getHours()
const mins = dt.getMinutes()
return `${hours}:${mins} [${id}] ${message}`
}
const selectChat = async (chats) => {
const chatNames = pluck('title', chats)
console.log('Your chat list')
chatNames.map((name, id) => console.log(`${id} ${name}`))
console.log('Select chat by index')
const chatIndex = await inputField('index')
return chats[+chatIndex]
}
module.exports = {
getChat,
chatHistory,
}