-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivity_monitor.js
More file actions
270 lines (235 loc) · 8.41 KB
/
activity_monitor.js
File metadata and controls
270 lines (235 loc) · 8.41 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
require('@dotenvx/dotenvx').config()
const { Api, TelegramClient } = require('telegram')
const { StringSession } = require('telegram/sessions')
const { DateTime } = require('luxon')
const { User, Event, sequelize } = require('./db')
const config = require('./config')
// Configuration
const apiId = Number(process.env.TG_API_ID)
const apiHash = process.env.TG_API_HASH
const stringSession = new StringSession(process.env.TG_STRING_SESSION || '')
const targetChat = process.env.TG_TARGET_CHAT || config.public_channel_id // Group chat ID
const ACTIVITY_WINDOW_DAYS = Number(
process.env.ACTIVITY_WINDOW_DAYS ||
config.activity_monitoring?.window_days ||
30
)
if (!apiId || !apiHash || !stringSession || !targetChat) {
console.error(
'Missing required env vars (TG_API_ID, TG_API_HASH, TG_STRING_SESSION, TG_TARGET_CHAT)'
)
process.exit(1)
}
// Update user's last message date
async function updateUserLastMessage(telegramId, messageDate) {
try {
await User.update(
{ last_message_date: messageDate },
{ where: { telegram_id: telegramId.toString() } }
)
} catch (error) {
console.error(
`Error updating last message date for user ${telegramId}:`,
error
)
}
}
// Update user's last reaction date
async function updateUserLastReaction(telegramId, reactionDate) {
try {
await User.update(
{ last_reaction_date: reactionDate },
{ where: { telegram_id: telegramId.toString() } }
)
} catch (error) {
console.error(
`Error updating last reaction date for user ${telegramId}:`,
error
)
}
}
// Update user's last bot activity date
async function updateUserLastBotActivity(telegramId, activityDate) {
try {
await User.update(
{ last_bot_activity_date: activityDate },
{ where: { telegram_id: telegramId.toString() } }
)
} catch (error) {
console.error(
`Error updating last bot activity date for user ${telegramId}:`,
error
)
}
}
// Resolve entity helper with numeric-ID support
function normalizeChatIdentifier(chat) {
// 1. If it's already a number (or numeric string) coming from an env/config,
// convert it to telegram's `-100` prefixed BigInt (required by gramJS).
// 2. Otherwise just pass the original value (@username, invite link, etc.) through.
if (typeof chat === 'string') {
const trimmed = chat.trim()
if (/^\d+$/.test(trimmed)) {
// Channel/group ID without -100 prefix, add it.
// The resulting value must be BigInt for gramJS.
return BigInt(`-100${trimmed}`)
}
} else if (typeof chat === 'number') {
// Numeric literal provided directly
return BigInt(`-100${chat}`)
}
return chat
}
async function resolveEntity(client, chat) {
const normalized = normalizeChatIdentifier(chat)
try {
return await client.getEntity(normalized)
} catch (err) {
console.error('Failed to resolve target chat entity:', err)
throw err
}
}
// Helper to reliably extract numeric Telegram user ID from a message
function getSenderIdFromMessage(msg) {
// For standard messages in groups/supergroups "fromId" is present.
if (msg.fromId) {
const id = extractUserId(msg.fromId)
if (id) return id
}
// Some API versions expose "senderId" instead of "fromId"
if (msg.senderId) {
const id = extractUserId(msg.senderId)
if (id) return id
}
// Fallback: nothing suitable found
return null
}
// Get recent message senders
async function collectRecentSenders(client, entity, cutoffDate) {
const userLastMsg = new Map()
let offsetId = 0
const limit = 100
while (true) {
const history = await client.invoke(
new Api.messages.GetHistory({
peer: entity,
offsetId,
offsetDate: 0,
addOffset: 0,
limit,
maxId: 0,
minId: 0,
hash: BigInt(0),
})
)
const msgs = history.messages
if (!msgs.length) break
let foundOldMessage = false
for (const m of msgs) {
if (m instanceof Api.Message) {
const dt = DateTime.fromSeconds(m.date, { zone: 'utc' })
// Luxon DateTime objects should be compared via their epoch milliseconds
if (dt.toMillis() < cutoffDate.toMillis()) {
foundOldMessage = true
continue
}
const senderId = getSenderIdFromMessage(m)
if (senderId) {
const existing = userLastMsg.get(senderId)
if (!existing || dt.toMillis() > existing.toMillis()) {
userLastMsg.set(senderId, dt)
}
}
}
}
if (foundOldMessage) break
const lastMsg = msgs[msgs.length - 1]
if (lastMsg && lastMsg.id) {
offsetId = lastMsg.id
} else {
break
}
}
return userLastMsg
}
// Extract user ID from peer
function extractUserId(fromId) {
if (!fromId) return null
if (fromId instanceof Api.PeerUser) return Number(fromId.userId)
return null
}
// Mark users as active/inactive based on all activity types
async function updateUserActivityStatus() {
const cutoffDate = DateTime.utc().minus({ days: ACTIVITY_WINDOW_DAYS })
const cutoffMillis = cutoffDate.toMillis()
// Helper to check if a JS Date lies within the activity window
const withinWindow = (jsDate) =>
jsDate && DateTime.fromJSDate(jsDate).toMillis() >= cutoffMillis
const users = await User.findAll({
where: { is_exempt_from_activity_check: false },
})
for (const user of users) {
let isActive = false
// Check any activity type within window
if (withinWindow(user.last_message_date)) isActive = true
if (withinWindow(user.last_reaction_date)) isActive = true
if (withinWindow(user.last_bot_activity_date)) isActive = true
await user.update({ is_active: isActive })
}
}
// Main function
async function main() {
console.log('Starting activity monitor...')
const client = new TelegramClient(stringSession, apiId, apiHash, {
connectionRetries: 5,
})
try {
console.log('Connecting to Telegram...')
await client.start()
console.log('Telegram connected.')
const entity = await resolveEntity(client, targetChat)
console.log('Resolved chat entity:', targetChat)
const cutoffDate = DateTime.utc().minus({ days: ACTIVITY_WINDOW_DAYS })
console.log(`Activity cutoff UTC: ${cutoffDate.toISO()}`)
// Collect recent message senders
console.log('Collecting recent message senders...')
const userLastMsgMap = await collectRecentSenders(
client,
entity,
cutoffDate
)
console.log(`Found ${userLastMsgMap.size} users with recent messages`)
// Update message activity in database
for (const [userId, lastMessageDate] of userLastMsgMap) {
await updateUserLastMessage(userId, lastMessageDate.toJSDate())
}
// Update activity status for all users
console.log('Updating user activity status...')
await updateUserActivityStatus()
// Generate summary
const activeCount = await User.count({
where: { is_active: true, is_exempt_from_activity_check: false },
})
const inactiveCount = await User.count({
where: { is_active: false, is_exempt_from_activity_check: false },
})
const exemptCount = await User.count({
where: { is_exempt_from_activity_check: true },
})
console.log(`Activity summary:`)
console.log(`Active users: ${activeCount}`)
console.log(`Inactive users: ${inactiveCount}`)
console.log(`Exempt users: ${exemptCount}`)
} catch (error) {
console.error('Error in activity monitor:', error)
process.exit(1)
} finally {
await client.disconnect()
console.log('Activity monitor completed.')
}
}
// Run if called directly
if (require.main === module) {
main().catch(console.error)
}
module.exports = { updateUserLastBotActivity }