@gonzih/cc-tg 0.9.17 → 0.9.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/notifier.js +55 -0
- package/package.json +1 -1
package/dist/notifier.js
CHANGED
|
@@ -73,6 +73,61 @@ export function startNotifier(bot, chatId, namespace, redis, handleUserMessage,
|
|
|
73
73
|
log("info", `subscribed to cca:chat:incoming:${namespace}`);
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
|
+
// Poll the cca:notify:{namespace} LIST every 5 seconds.
|
|
77
|
+
// Jobs push to this list via RPUSH; pub/sub alone won't deliver those messages.
|
|
78
|
+
const notifyListKey = `cca:notify:${namespace}`;
|
|
79
|
+
const MAX_PER_CYCLE = 20;
|
|
80
|
+
const pollNotifyList = async () => {
|
|
81
|
+
const targetId = chatId ?? getActiveChatId?.();
|
|
82
|
+
if (targetId == null)
|
|
83
|
+
return;
|
|
84
|
+
const items = [];
|
|
85
|
+
try {
|
|
86
|
+
for (let i = 0; i < MAX_PER_CYCLE; i++) {
|
|
87
|
+
const item = await redis.rpop(notifyListKey);
|
|
88
|
+
if (item === null)
|
|
89
|
+
break;
|
|
90
|
+
items.push(item);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
log("warn", "notify list rpop failed:", err.message);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (items.length === 0)
|
|
98
|
+
return;
|
|
99
|
+
let remaining = 0;
|
|
100
|
+
if (items.length === MAX_PER_CYCLE) {
|
|
101
|
+
try {
|
|
102
|
+
remaining = await redis.llen(notifyListKey);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
log("warn", "notify list llen failed:", err.message);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const raw of items) {
|
|
109
|
+
let text = raw;
|
|
110
|
+
try {
|
|
111
|
+
const parsed = JSON.parse(raw);
|
|
112
|
+
if (parsed.text)
|
|
113
|
+
text = parsed.text;
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// not JSON — use raw string as-is
|
|
117
|
+
}
|
|
118
|
+
bot.sendMessage(targetId, text).catch((err) => {
|
|
119
|
+
log("warn", "notify list sendMessage failed:", err.message);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (remaining > 0) {
|
|
123
|
+
bot.sendMessage(targetId, `...and ${remaining} more notifications`).catch((err) => {
|
|
124
|
+
log("warn", "notify list summary sendMessage failed:", err.message);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
setInterval(() => {
|
|
129
|
+
void pollNotifyList();
|
|
130
|
+
}, 5_000);
|
|
76
131
|
sub.on("message", (channel, message) => {
|
|
77
132
|
const notifyChannel = `cca:notify:${namespace}`;
|
|
78
133
|
const incomingChannel = `cca:chat:incoming:${namespace}`;
|