-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
173 lines (157 loc) · 4.69 KB
/
bot.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { serve } from "https://deno.land/[email protected]/http/mod.ts";
import {
Bot,
InlineKeyboard,
webhookCallback,
} from "https://deno.land/x/[email protected]/mod.ts";
const bot = new Bot(Deno.env.get("BOT_TOKEN")!);
bot.catch(console.error);
bot.command("start", async (ctx) => {
if (!ctx.match) {
return await ctx.reply(
"Use an available language command followed by the song number, and I can get you \
the lyrics of the songs. \n\
You can use me like this: \n\
/mal <song-number>\n\
Example: /mal 1\n\
Available language commands: /mal /eng /hin /kan /tam",
);
}
const split = ctx.match.split("_");
const res = await fetch(
`https://sg7.deno.dev/${split[0]}/${split[1]}.json`,
);
if (!res.ok) return await ctx.reply("Couldn't find the requested song data");
const data = await res.json();
return await ctx.reply(
`<b>${data.number}</b>${data.old_number ? ` (${data.old_number})` : ""}
\n${sanitize(data.text)}\n
Source: https://sg7.deno.dev/${split[0]}/${split[1]}.json
Share: https://t.me/${ctx.me.username}?start=${split[0]}_${split[1]}`,
{ parse_mode: "HTML", disable_web_page_preview: true },
);
});
// deno-fmt-ignore
const cmd = [
"ml", "en", "hi", "kn", "ta",
"mal", "eng", "hin", "kan", "tam"
];
// const count = {
// mal: 1260,
// eng: 221,
// hin: 48,
// kan: 15,
// tam: 15,
// };
bot.command(cmd, async (ctx) => {
if (!ctx.match) {
return await ctx.reply("Enter a song number with the command");
}
const cmd = ctx.msg.entities[0];
const category = getLang(ctx.msg.text.substring(1, cmd.length));
const res = await fetch(
`https://sg7.deno.dev/${category}/${ctx.match}.json`,
);
if (!res.ok) return await ctx.reply("Couldn't find the requested song data");
const data = await res.json();
return await ctx.reply(
`<b>${data.number}</b>${data.old_number ? ` (${data.old_number})` : ""}
\n${sanitize(data.text)}\n
Source: https://sg7.deno.dev/${category}/${ctx.match}.json
Share: https://t.me/${ctx.me.username}?start=${category}_${ctx.match}`,
{ parse_mode: "HTML", disable_web_page_preview: true },
);
});
bot.inlineQuery(/(mal|ml|eng|en|hin|hi|kan|kn|tam|ta)( \d+|)/, async (ctx) => {
const queryParams = ctx.inlineQuery.query.split(" ");
const lang = queryParams[0];
const number = queryParams[1];
const res = await fetch(`https://sg7.deno.dev/${lang}/${number}.json`);
if (!res.ok) {
return await ctx.answerInlineQuery([
{
type: "article",
id: "1234",
title:
"Enter a valid song number. Couldn't find the requested song data.",
input_message_content: { message_text: "Oops :(" },
},
]);
}
const s = await res.json();
const share = `https://t.me/${ctx.me.username}?start=${
getLang(lang)
}_${s.number}`;
await ctx.answerInlineQuery([
{
type: "article",
id: `${lang}_${number}`,
title: `${number}${s.old_number ? ` (${s.old_number})` : ""} — ${s.text}`,
input_message_content: {
message_text: `<b>${s.number}</b>${
s.old_number ? ` (${s.old_number})` : ""
}
\n${sanitize(s.text)}\n
Source: https://sg7.deno.dev/${getLang(lang)}/${s.number}.json
Share: ${share}`,
parse_mode: "HTML",
disable_web_page_preview: true,
},
reply_markup: new InlineKeyboard().url("Open", `${share}`),
},
]);
});
bot.on("inline_query", async (ctx) => {
await ctx.answerInlineQuery([
{
type: "article",
id: "1234",
title: "Invalid query, enter a language abbr followed by song number.",
input_message_content: { message_text: "Oops :(" },
},
]);
});
// bot.start();
const handleUpdate = webhookCallback(bot, "std/http");
await bot.init();
serve(async (req) => {
if (req.method === "POST") {
try {
return await handleUpdate(req);
} catch (err) {
console.error(err);
return new Response();
}
}
// redirect any other requests to the bot profile
return Response.redirect(`https://telegram.me/${bot.botInfo.username}`);
});
/** https://github.com/grammyjs/storages/blob/main/packages/README.md#grammy-storages2 https://github.com/grammyjs/storages/tree/main/packages#grammy-storages */
export function sanitize(str: string) {
return str
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/&/g, "&");
}
function getLang(lang: string) {
return lang[0] === "m"
? "mal"
: lang[0] === "e"
? "eng"
: lang[0] === "h"
? "hin"
: lang[0] === "k"
? "kan"
: "tam";
}
// function getFullLang(lang: string) {
// return lang[0] === "m"
// ? "Malayalam"
// : lang[0] === "e"
// ? "English"
// : lang[0] === "h"
// ? "Hindi"
// : lang[0] === "k"
// ? "Kannada"
// : "Tamil";
// }