-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (105 loc) · 3.31 KB
/
index.js
File metadata and controls
118 lines (105 loc) · 3.31 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
import { createRequire } from "node:module";
import { realpathSync } from "node:fs";
const _require = createRequire(realpathSync(process.argv[1]));
const { Api } = _require("telegram");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export const manifest = {
name: "voice-notes",
version: "1.0.0",
sdkVersion: ">=1.0.0",
description: "Transcribe voice messages and video notes using Telegram Premium speech-to-text.",
};
export const tools = (sdk) => [
{
name: "voice_transcribe",
description:
"Transcribe a voice message or video note using Telegram Premium speech-to-text. " +
"ALWAYS use this when you see [🎤 voice msg_id=...] or [🎬 video_note msg_id=...]. " +
"Pass the msg_id number from the tag as message_id.",
category: "data-bearing",
scope: "always",
parameters: {
type: "object",
properties: {
message_id: {
type: "number",
description: "The msg_id number from the [🎤 voice msg_id=...] tag",
},
},
required: ["message_id"],
},
execute: async (params, context) => {
try {
const { message_id } = params;
const chatId = context.chatId;
const gramJsClient = sdk.telegram.getRawClient();
const result = await gramJsClient.invoke(
new Api.messages.TranscribeAudio({
peer: chatId,
msgId: message_id,
})
);
if (!result.pending && result.text) {
return {
success: true,
data: {
text: result.text,
message: `User said: ${result.text}`,
},
};
}
if (result.pending) {
for (let attempt = 0; attempt < 10; attempt++) {
await sleep(1500);
try {
const check = await gramJsClient.invoke(
new Api.messages.TranscribeAudio({
peer: chatId,
msgId: message_id,
})
);
if (!check.pending && check.text) {
return {
success: true,
data: {
text: check.text,
message: `User said: ${check.text}`,
},
};
}
} catch {
// Ignore poll errors, keep trying
}
}
return {
success: false,
error:
"Transcription is still processing after 15 seconds. " +
(result.text ? `Partial: ${result.text}` : "Try again later."),
};
}
return {
success: false,
error: "Transcription returned empty result. The message may not contain audio.",
};
} catch (err) {
const msg = String(err.message || err).slice(0, 500);
if (msg.includes("PREMIUM_ACCOUNT_REQUIRED")) {
return {
success: false,
error: "Telegram Premium is required for voice transcription.",
};
}
if (msg.includes("MSG_ID_INVALID")) {
return {
success: false,
error: "Invalid message ID. Make sure the message exists and contains audio.",
};
}
return { success: false, error: msg };
}
},
},
];