-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActivityIndicator.js
More file actions
71 lines (60 loc) · 2.02 KB
/
Copy pathActivityIndicator.js
File metadata and controls
71 lines (60 loc) · 2.02 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
/**
* Simple Typing Indicator for Telegram Bot
* Shows typing action during Claude processing
*/
class ActivityIndicator {
constructor(bot) {
this.bot = bot;
this.activeIndicators = new Map();
}
async start(chatId) {
try {
// Start typing indicator immediately
await this.bot.sendChatAction(chatId, 'typing');
// Continue typing indicator every 4 seconds to stay within 5s limit
const typingInterval = setInterval(async () => {
try {
await this.bot.sendChatAction(chatId, 'typing');
} catch (error) {
console.error(`[ActivityIndicator] Typing error for chat ${chatId}:`, error.message);
}
}, 4000);
// Store for cleanup
this.activeIndicators.set(chatId, {
typingInterval,
startTime: Date.now()
});
console.log(`[ActivityIndicator] Started typing for chat ${chatId}`);
} catch (error) {
console.error(`[ActivityIndicator] Failed to start typing for chat ${chatId}:`, error.message);
}
}
async stop(chatId) {
const indicator = this.activeIndicators.get(chatId);
if (!indicator) {
return; // Already stopped or never started
}
// Clear typing interval
clearInterval(indicator.typingInterval);
// Calculate processing time
const processingTime = Date.now() - indicator.startTime;
console.log(`[ActivityIndicator] Stopped typing for chat ${chatId}, duration: ${processingTime}ms`);
this.activeIndicators.delete(chatId);
}
// Emergency cleanup - stops all indicators
cleanup() {
console.log(`[ActivityIndicator] Emergency cleanup - stopping ${this.activeIndicators.size} typing indicators`);
for (const [, indicator] of this.activeIndicators) {
clearInterval(indicator.typingInterval);
}
this.activeIndicators.clear();
}
// Get stats for debugging
getStats() {
return {
activeIndicators: this.activeIndicators.size,
indicators: Array.from(this.activeIndicators.keys())
};
}
}
module.exports = ActivityIndicator;