forked from zerobias/telegram-mtproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-history.js
84 lines (71 loc) · 2.08 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
const { pluck } = require('ramda')
const { inputField } = require('./fixtures')
const telegram = require('./init')
const getChat = async () => {
const dialogs = await telegram('messages.getDialogs', {
limit: 50,
})
const { chats } = dialogs
const selectedChat = await selectChat(chats)
return selectedChat
}
const chatHistory = async chat => {
const max = 400
const limit = 100
let offset = 0
let full = [],
messages = []
do {
const history = await telegram('messages.getHistory', {
peer: {
_ : 'inputPeerChannel',
channel_id : chat.id,
access_hash: chat.access_hash
},
max_id: offset,
offset: -full.length,
limit
})
messages = history.messages.filter(filterLastDay)
full = full.concat(messages)
messages.length > 0 && (offset = messages[0].id)
messages.length > 0 && console.log(offset, messages[0].id)
} while (messages.length === limit && full.length < max)
printMessages(full)
return full
}
const filterLastDay = ({ date }) => new Date(date*1e3) > dayRange()
const dayRange = () => Date.now() - new Date(86400000*4)
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]
}
const filterUsersMessages = ({ _ }) => _ === 'message'
const formatMessage = ({ message, date, from_id }) => {
const dt = new Date(date*1e3)
const hours = dt.getHours()
const mins = dt.getMinutes()
return `${hours}:${mins} [${from_id}] ${message}`
}
const printMessages = messages => {
const filteredMsg = messages.filter(filterUsersMessages)
const formatted = filteredMsg.map(formatMessage)
formatted.forEach(e => console.log(e))
return formatted
}
const searchUsers = async (username) => {
const results = await telegram('contacts.search', {
q : username,
limit: 100,
})
return results
}
module.exports = {
getChat,
chatHistory,
searchUsers
}