-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
95 lines (71 loc) · 3.39 KB
/
index.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
import { Telegraf } from 'telegraf';
import { transcriber } from './lib/transcriber';
import { refiner } from './lib/refiner';
import type { MessageEntity } from 'telegraf/types';
import { videoNoteTranscriber } from './lib/video-note-transcriber';
if (!process.env.BOT_TOKEN) throw new Error('BOT_TOKEN must be provided!');
if (!process.env.ADMIN_USERS) throw new Error('ADMIN_USERS must be provided!');
if (!process.env.ALLOWED_GROUPS) throw new Error('ALLOWED_GROUPS must be provided!');
function parseIdList(list: string): number[] {
return list.split(',').map(u=>u.trim()).filter(Boolean).map(Number);
}
const adminUsers = parseIdList(process.env.ADMIN_USERS);
const allowedGroups = parseIdList(process.env.ALLOWED_GROUPS);
const idsCombined = [...adminUsers, ...allowedGroups];
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.start((ctx) => ctx.reply('well cum'));
bot.use(async (ctx, next) => {
const start = new Date()
const chat = ctx.chat;
// Ignore old messages
if (ctx.message?.date && (new Date().getTime() / 1000 - ctx.message.date) > 60) {
console.log('Ignoring old message');
return next();
}
if (!idsCombined.includes(chat?.id as number)) {
ctx.reply('You are not allowed to use this bot');
return next();
}
type HasFileId = {
file_id: string;
}
if ((ctx.message as any)?.voice as HasFileId) {
replyWithTranscription('Transcribing voice message...', (ctx.message as any).voice.file_id);
}
if ((ctx.message as any)?.video_note as HasFileId) {
replyWithTranscription('Transcribing video note...', (ctx.message as any).video_note.file_id, true);
}
async function replyWithTranscription(initialMessage: string, fileId: string, isVideoNote = false) {
const reply = await ctx.reply(initialMessage, { reply_parameters: { message_id: ctx.message!.message_id } });
const url = await ctx.telegram.getFileLink(fileId)
const result = isVideoNote ? await videoNoteTranscriber(url.href) : await transcriber(url.href);
ctx.telegram.editMessageText(reply.chat.id, reply.message_id, undefined, result.transcription);
const refined = await refiner(result.transcription);
const transcriptionHeader = 'Transcription:';
const summaryHeader = 'Summary:';
const transcriptionIndex = refined.indexOf(transcriptionHeader);
const summaryIndex = refined.indexOf(summaryHeader);
const transcriptionHeaderEntities: Array<MessageEntity> = [
{ type: 'bold', offset: transcriptionIndex, length: transcriptionHeader.length },
{ type: 'underline', offset: transcriptionIndex, length: transcriptionHeader.length }
];
const summaryHeaderEntities: Array<MessageEntity> = summaryIndex !== -1 ? [
{ type: 'bold', offset: summaryIndex, length: summaryHeader.length },
{ type: 'underline', offset: summaryIndex, length: summaryHeader.length }
] : [];
ctx.telegram.editMessageText(reply.chat.id, reply.message_id, undefined, refined, {
entities: [
...transcriptionHeaderEntities,
...summaryHeaderEntities
]
});
}
return next().then(() => {
const ms = new Date().getTime() - start.getTime()
console.log('Response time: %sms', ms)
})
});
bot.launch()
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))