forked from TONresistor/teleton-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (87 loc) · 3.11 KB
/
index.js
File metadata and controls
99 lines (87 loc) · 3.11 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
/**
* Vid plugin -- search and send YouTube videos via the @vid inline bot
*
* Uses GramJS MTProto to query @vid inline results and send them directly in chat.
* Messages appear "via @vid" just like typing @vid in the Telegram input field.
*/
import { createRequire } from "node:module";
import { realpathSync } from "node:fs";
// Resolve "telegram" from teleton's own node_modules (not the plugin directory).
// realpathSync follows the symlink so createRequire looks in the right node_modules.
const _require = createRequire(realpathSync(process.argv[1]));
const { Api } = _require("telegram");
// ---------------------------------------------------------------------------
// Export
// ---------------------------------------------------------------------------
export const tools = (sdk) => [
{
name: "vid",
description:
"Search and send a YouTube video in the current chat using Telegram's @vid inline bot (YouTube Search). " +
"Provide a search query and optionally pick a result by index. The video is sent directly into the chat via @vid.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "YouTube video search query (e.g. 'funny cat', 'TON blockchain', 'cooking tutorial')",
},
index: {
type: "integer",
description: "Which result to send (0 = first, 1 = second, etc.). Defaults to 0.",
minimum: 0,
maximum: 49,
},
},
required: ["query"],
},
execute: async (params, context) => {
try {
const client = sdk.telegram.getRawClient();
const vidBot = await client.getEntity("vid");
const peer = await client.getInputEntity(context.chatId);
const results = await client.invoke(
new Api.messages.GetInlineBotResults({
bot: vidBot,
peer,
query: params.query,
offset: "",
})
);
if (!results.results || results.results.length === 0) {
return { success: false, error: `No YouTube videos found for "${params.query}"` };
}
const index = params.index ?? 0;
if (index >= results.results.length) {
return {
success: false,
error: `Only ${results.results.length} results available, index ${index} is out of range`,
};
}
const chosen = results.results[index];
await client.invoke(
new Api.messages.SendInlineBotResult({
peer,
queryId: results.queryId,
id: chosen.id,
randomId: BigInt(Math.floor(Math.random() * 2 ** 62)),
})
);
return {
success: true,
data: {
query: params.query,
sent_index: index,
total_results: results.results.length,
title: chosen.title || null,
description: chosen.description || null,
type: chosen.type || null,
},
};
} catch (err) {
return { success: false, error: err.message };
}
},
},
];