MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / ensureBotReady

Function ensureBotReady

parsehub/parsehub.ts:122–197  ·  view source on GitHub ↗
(msg: Api.Message)

Source from the content-addressed store, hash-verified

120 if (!text) return [];
121 const matches = text.match(/(?:https?:\/\/|www\.)\S+/gi) || [];
122 const sanitized = matches.map((raw) => {
123 const cleaned = raw.replace(
124 /[)\]\}\u3002\uff1a\uff01\uff1f\u3001\uff0c>]+$/u,
125 "",
126 );
127 return cleaned.startsWith("http") ? cleaned : `https://${cleaned}`;
128 });
129 return Array.from(new Set(sanitized.map((link) => link.trim()))).filter(
130 Boolean,
131 );
132}
133
134async function ensureBotReady(msg: Api.Message) {
135 const client = msg.client;
136 if (!client) return;
137
138 try {
139 await client.invoke(new Api.contacts.Unblock({ id: BOT_USERNAME }));
140 } catch {}
141
142 try {
143 const inputPeer = await client.getInputEntity(BOT_USERNAME);
144 await client.invoke(
145 new Api.account.UpdateNotifySettings({
146 peer: new Api.InputNotifyPeer({ peer: inputPeer }),
147 settings: new Api.InputPeerNotifySettings({
148 silent: true,
149 muteUntil: 2147483647,
150 }),
151 }),
152 );
153 } catch {}
154
155 if (hasStartedBot) {
156 return;
157 }
158
159 try {
160 const history = await safeGetMessages(client, BOT_USERNAME, { limit: 1 });
161 if (history.length > 0) {
162 hasStartedBot = true;
163 return;
164 }
165 } catch {}
166
167 try {
168 if (!initState.initialized) {
169 firstRunPreStartLastId = await getLatestBotMessageId(client);
170 shouldIgnoreNextBotMessage = true;
171 }
172 await client.invoke(
173 new Api.messages.StartBot({
174 bot: BOT_USERNAME,
175 peer: BOT_USERNAME,
176 startParam: "",
177 }),
178 );
179 hasStartedBot = true;

Callers 1

ParseHubPluginClass · 0.70

Calls 3

getLatestBotMessageIdFunction · 0.85
writeStateFunction · 0.85
sleepFunction · 0.50

Tested by

no test coverage detected