-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.ts
251 lines (196 loc) · 6.46 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import 'https://deno.land/x/[email protected]/load.ts';
import {
Api,
Bot,
CallbackQueryContext,
Context,
InlineKeyboard,
} from 'https://deno.land/x/[email protected]/mod.ts';
import { Message } from 'https://deno.land/x/[email protected]/types.deno.ts';
import {
FileApiFlavor,
FileFlavor,
hydrateFiles,
} from 'https://deno.land/x/[email protected]/mod.ts';
import { autoRetry } from 'https://esm.sh/@grammyjs/[email protected]';
import { prisma } from './database.ts';
import { transcribe, writeX, writeXThread } from './openai_calls.ts';
import {
accessMiddleware,
downloadFile,
isLongForm,
threadButton,
tweetButton,
zwnj,
} from './utils.ts';
export const isDev = Deno.env.get('NODE_ENV') === 'development';
export type MyContext = FileFlavor<Context>;
export type MyApi = FileApiFlavor<Api>;
const token = Deno.env.get('BOT_TOKEN')!;
const bot = new Bot<MyContext, MyApi>(token);
bot.api.config.use(hydrateFiles(bot.token));
bot.api.config.use(autoRetry());
const botDescription =
'Telegram bot that converts voice messages to text, and text to Twitter tweets or threads.';
bot.api.setMyDescription(botDescription);
bot.api.setMyShortDescription(botDescription);
//#region Commands
bot.api.setMyCommands([
{ command: 'help', description: 'Show help message' },
...(isDev ? [{ command: 'test', description: 'Test command' }] : []),
]);
bot.command('start', async (ctx) => {
const message = `Hi 👋
I'm VoiceToX bot, and I can help you generate tweets and threads from your messages\\.
Now send me a *text* or *voice* message\\.`;
await ctx.reply(message, { parse_mode: 'MarkdownV2' });
});
bot.command('help', (ctx) =>
ctx.reply(
`My name is VoiceToX bot 🤖
I can help you transcribe voice messages and generate tweets and threads from text\\.
I generate threads only from messages that are longer than 150 characters\\.
\\[[GitHub repository](https://github.com/SashaKryzh/tg-voice-gpt)\\]
Please send me a *text* or *voice* message\\.`,
{
parse_mode: 'MarkdownV2',
disable_web_page_preview: true,
}
)
);
bot.command('requestaccess', async (ctx) => {
const from = ctx.from;
if (!from) {
return ctx.reply('Something went wrong...');
}
const dbUser = await prisma.user.findUnique({ where: { id: from.id } });
if (dbUser?.hasAccess) {
return ctx.reply('You already have access. Enjoy!');
}
if (!dbUser) {
const name = `${from.first_name} ${from.last_name ?? ''}`.trim();
await prisma.user.upsert({
where: { id: from.id },
update: {},
create: { id: from.id, username: from.username, name: name },
});
}
await ctx.reply(
"Your request has been sent. Wait.\n\nAlso, notify @SashaKryzh to review it if you haven't done it yet."
);
});
if (isDev) {
bot.command('test', async (ctx) => {
await ctx.reply('This is a test command. Why did you run it?');
});
}
//#endregion
//#region Messages
bot.on('message:text', accessMiddleware, async (ctx) => {
const keyboard = InlineKeyboard.from([
[tweetButton, ...(isLongForm(ctx.message.text) ? [threadButton] : [])],
]);
await ctx.reply('What do you want me to do?' + zwnj, {
reply_to_message_id: ctx.message.message_id,
reply_markup: keyboard,
});
});
bot.on('message:voice', accessMiddleware, async (ctx) => {
const transcribeButton = InlineKeyboard.text('Transcribe ✍️', `transcribe`);
const keyboard = InlineKeyboard.from([[transcribeButton]]);
await ctx.reply(
`Got a voice message! I will process it if you use the button below.`,
{
reply_to_message_id: ctx.message.message_id,
reply_markup: keyboard,
}
);
});
bot.on('message', async (ctx) => {
await ctx.reply("I don't understand 🤡\nSend me a text or voice message 🗣️");
});
//#endregion
//#region Callbacks
bot.callbackQuery('transcribe', accessMiddleware, async (ctx) => {
const callbackMessage = ctx.callbackQuery.message;
const messageWithFile = callbackMessage?.reply_to_message;
const fileId = messageWithFile?.voice?.file_id;
if (!fileId) {
return ctx.answerCallbackQuery({
text: 'Something went wrong... Try sending file again.',
});
}
await ctx.answerCallbackQuery();
const transcribedMessage = await ctx.reply(`Transcribing...`, {
reply_to_message_id: messageWithFile?.message_id,
});
let text = '';
try {
const path = await downloadFile(ctx, fileId);
text = await transcribe(path);
Deno.removeSync(path);
} catch (e) {
await ctx.reply('Something went wrong...');
console.error(e);
return;
}
const keyboard = InlineKeyboard.from([
[tweetButton, ...(isLongForm(text) ? [threadButton] : [])],
]);
const chatId = ctx.chat?.id ?? '';
await ctx.api.editMessageText(chatId, transcribedMessage.message_id, text, {
reply_markup: keyboard,
});
if (!isDev) {
await ctx.api.deleteMessage(chatId, callbackMessage?.message_id ?? 0);
}
});
bot.callbackQuery('tweet', accessMiddleware, (ctx) =>
genXCallbackHandler(ctx, 'tweet')
);
bot.callbackQuery('thread', accessMiddleware, (ctx) =>
genXCallbackHandler(ctx, 'thread')
);
const genXCallbackHandler = async (
ctx: CallbackQueryContext<MyContext>,
type: 'tweet' | 'thread'
) => {
const callbackMessage = ctx.callbackQuery.message;
if (!callbackMessage) {
return ctx.answerCallbackQuery({ text: 'Something went wrong...' });
}
let messageWithText: Message | undefined;
if (callbackMessage.text?.endsWith(zwnj)) {
messageWithText = callbackMessage.reply_to_message;
} else {
messageWithText = callbackMessage;
}
const text = messageWithText?.text;
if (!text) {
return ctx.answerCallbackQuery({ text: 'Something went wrong...' });
}
await ctx.answerCallbackQuery();
const xMessage = await ctx.reply(`Generating ${type}...`, {
reply_to_message_id: messageWithText?.message_id,
});
const x = await (type === 'tweet' ? writeX(text) : writeXThread(text));
return ctx.api.editMessageText(
ctx.chat?.id ?? '',
xMessage.message_id,
x ?? 'Something went wrong...'
);
};
//#endregion
bot.on('callback_query:data', async (ctx) => {
console.log('Unknown button event with payload', ctx.callbackQuery.data);
await ctx.answerCallbackQuery();
});
bot.catch((err) => {
const ctx = err.ctx;
console.error('Error while handling update ', ctx.update.update_id);
console.error(err.error);
ctx.reply('Some unhandled error occurred. Go away!');
});
Deno.addSignalListener('SIGINT', () => bot.stop());
Deno.addSignalListener('SIGTERM', () => bot.stop());
bot.start();