-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (109 loc) · 4.21 KB
/
index.js
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
import {execa} from 'execa';
import fs from 'fs';
import path from 'path';
import { Telegraf } from 'telegraf';
import { message } from 'telegraf/filters';
import { Client, GatewayIntentBits, Partials } from 'discord.js';
// set up Discord.js with message content intent
intents = [GatewayIntentBits.Guilds, GatewayIntentBits.DirectMessages, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages];
if (process.env.DISCORD_MESSAGE_CONTENT_INTENT == 'true') {
intents.push(GatewayIntentBits.MessageContent);
}
const Discord = new Client({ partials: [Partials.GuildMember, Partials.User, Partials.Message, Partials.Channel], intents: intents });
if (process.env.DISCORD_TOKEN) {
Discord.login(process.env.DISCORD_TOKEN);
} else {
console.error("DISCORD_TOKEN is not set, skipping Discord Setup.");
}
Discord.on('ready', () => {
console.log(`Logged in as ${Discord.user.tag}!`);
});
Discord.on('messageCreate', async (message) => {
// if message has a voice message
let attentionMessage
if (message.attachments.size > 0 && message.attachments.first().waveform) {
attentionMessage = message
} else if (message.messageSnapshots.first()) {
let snapshot = message.messageSnapshots.first()
if (snapshot.attachments && snapshot.attachments.first()) {
if (snapshot.attachments.first().waveform) {
attentionMessage = message.messageSnapshots.first()
}
}
} else {
return;
}
if (!attentionMessage || !attentionMessage.attachments) {
return;
}
const attachment = attentionMessage.attachments.first();
if (attachment && attachment.waveform) {
message.channel.sendTyping();
const transcript = await DiscordVoiceHandler(attachment.url);
message.reply({ content: `\`\`\`\n${transcript}\`\`\`\n` });
}
});
async function DiscordVoiceHandler(link) {
try {
const fileResponse = await fetch(link)
if (!fs.existsSync('/tmp/tldl')) {
fs.mkdirSync('/tmp/tldl')
}
// generate a unique file name
const file_id = `${Date.now()}-${Math.random().toString(36)}.ogg`
const buffer = Buffer.from(await fileResponse.arrayBuffer())
fs.writeFileSync(`/tmp/tldl/${file_id}.ogg`, buffer)
const transcript = await QueryWhisper(`/tmp/tldl/${file_id}.ogg`)
fs.unlinkSync(`/tmp/tldl/${file_id}.ogg`)
fs.unlinkSync(`/tmp/tldl/${file_id}.text`)
return transcript;
} catch (error) {
console.error('Error processing voice message:', error);
return null;
}
}
var Telegram
if (process.env.TELEGRAM_TOKEN) {
Telegram = new Telegraf(process.env.TELEGRAM_TOKEN)
Telegram.start((ctx) => ctx.reply('Welcome! Forward me a Voice Message to get an audio transcript.'))
Telegram.on(message('voice'), async (ctx) => {
ctx.sendChatAction('typing');
const transcript = await TGVoiceHandler(ctx.message.voice.file_id);
ctx.reply(`${transcript}`, {
reply_to_message_id: ctx.message.message_id
}).catch((err) => {
ctx.reply(`${transcript}`)
})
});
Telegram.launch()
} else {
console.error('TELEGRAM_TOKEN is not set')
}
async function TGVoiceHandler(file_id) {
try {
const link = await Telegram.telegram.getFileLink(file_id);
const fileResponse = await fetch(link)
if (!fs.existsSync('/tmp/tldl')) {
fs.mkdirSync('/tmp/tldl')
}
const buffer = Buffer.from(await fileResponse.arrayBuffer())
fs.writeFileSync(`/tmp/tldl/${file_id}.ogg`, buffer)
const transcript = await QueryWhisper(`/tmp/tldl/${file_id}.ogg`)
fs.unlinkSync(`/tmp/tldl/${file_id}.ogg`)
fs.unlinkSync(`/tmp/tldl/${file_id}.text`)
return transcript;
} catch (error) {
console.error('Error processing voice message:', error);
return null;
}
}
async function QueryWhisper(fileName) {
// execute ./whisper-faster-xxl in "Whisper-Faster-XXL" folder
await execa`./Whisper-Faster-XXL/whisper-faster-xxl ${fileName} ${process.env.DEVICE !== 'auto' ? `--device ${process.env.DEVICE}` : ''} --output_format text --output_dir /tmp/tldl/ --without_timestamps true --word_timestamps false --model ${process.env.WHISPER_MODEL || 'medium'}`
let strippedFileName = path.basename(fileName, path.extname(fileName));
const filePath = path.join('/tmp/tldl/', `${strippedFileName}.text`);
const fileContent = fs.readFileSync(filePath, 'utf-8');
return fileContent
}
process.once('SIGINT', () => Telegram.stop('SIGINT'))
process.once('SIGTERM', () => Telegram.stop('SIGTERM'))