Newer
Older
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Markup, Telegraf } from 'telegraf';
import { TelegrafContext } from 'telegraf/typings/context';
import { TelegramEntry } from './telegram.entry';
export class TelegramService implements OnModuleInit {
private readonly bot: Telegraf<TelegrafContext>;
private readonly logger = new Logger('TelegramService');
private readonly entries = new Map<number, TelegramEntry>();
constructor(
private readonly prisma: PrismaService,
private readonly minio: MinioService,
) {
this.bot = new Telegraf(process.env.TELEGRAM_TOKEN);
this.bot.start(this.start);
this.bot.command('unregister', (ctx) =>
this.unregister.call(this, ctx).catch(console.error),
);
this.bot.command('register', (ctx) =>
this.register.call(this, ctx).catch(console.error),
);
this.bot.command('profile', (ctx) =>
this.profile.call(this, ctx).catch(console.error),
);
this.bot.on('message', (ctx) =>
this.onMessage.call(this, ctx).catch(console.error),
);
this.bot.on('edited_message', (ctx) =>
this.onMessage.call(this, ctx).catch(console.error),
);
this.bot.action('delete', (ctx) =>
this.deleteButton.call(this, ctx).catch(console.error),
);
this.bot.action('public', (ctx) =>
this.publicButton.call(this, ctx).catch(console.error),
);
this.bot.action('private', (ctx) =>
this.privateButton.call(this, ctx).catch(console.error),
);
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
}
async onModuleInit() {
await this.bot
.launch()
.then(() => this.bot.telegram.getMe())
.then((user) => this.logger.log(`Logged in as ${user.username}`));
}
async onMessage(ctx: TelegrafContext) {
if (!(await this.isRegistered(ctx.from.id))) {
await ctx.reply('User not registered');
return;
}
const message = ctx.message ?? ctx.editedMessage;
const content = message.text;
const location = message.location;
const image = message?.photo?.reduce((prev, curr) => {
if (prev.width > curr.width) return prev;
else return curr;
});
const entry: TelegramEntry = { content };
if (image)
entry.photo = {
id: image.file_id,
width: image.width,
height: image.height,
};
if (location)
entry.location = {
lat: location.latitude,
lon: location.longitude,
};
if (!entry.content && !entry.photo && !entry.location) return;
this.entries.set(ctx.from.id, entry);
const msg = await ctx.reply('📌 Message queued', {
reply_markup: {
inline_keyboard: [
[
Markup.callbackButton('Save as public', 'public'),
Markup.callbackButton('Save as private', 'private'),
Markup.callbackButton('Delete', 'delete'),
],
],
one_time_keyboard: true,
resize_keyboard: true,
},
});
try {
for (let i = 1; i <= 2; i++)
await ctx.telegram.editMessageReplyMarkup(
ctx.from.id,
msg.message_id - i,
undefined,
'',
);
} catch (e) {}
}
async start(ctx: TelegrafContext) {
await ctx.reply(`
/register <password> <name>\n - Registers you as a new Agent. The password will be assigned to you beforehand, while the name is what will be displayed to other users.\n
/unregister\n - Deletes all your Entries and Profile.\n\n
/profile\n - Shows your current profile.
`);
}
async unregister(ctx: TelegrafContext) {
const agent = await this.prisma.agent.update({
where: { uid: String(ctx.from.id) },
data: { uid: null },
});
if (agent) {
await ctx.reply('Unregistered');
} else {
await ctx.reply('User not registered');
}
}
async register(ctx: TelegrafContext) {
if (!ctx.message.text) return;
if (await this.isRegistered(ctx.from.id)) {
await ctx.reply('User already registered');
return;
}
const args = ctx.message.text.split(' ');
if (args.length !== 3) return await this.start(ctx);
const exists = await this.prisma.agent.count({
where: { slug: args[1] },
});
if (exists === 0) return await ctx.reply('Slug not found');
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
const agent = await this.prisma.agent.update({
where: { slug: args[1] },
data: {
name: args[2],
uid: String(ctx.from.id),
},
});
if (agent) {
await ctx.reply('Registered');
} else {
await ctx.reply('Password not found');
}
}
async profile(ctx: TelegrafContext) {
const agent = await this.prisma.agent.findFirst({
where: { uid: String(ctx.from.id) },
});
if (!agent) {
await ctx.reply('User not registered');
return;
}
const [publicEntries, privateEntries] = await this.prisma.$transaction([
this.prisma.entry.count({
where: { agentId: agent.id, private: false },
}),
this.prisma.entry.count({
where: { agentId: agent.id, private: true },
}),
]);
await ctx.reply(
`📂 Agent Information\n` +
`- Id: ${agent.id}\n` +
`- Name: ${agent.name}\n` +
`- Code: ${agent.slug}\n` +
`- Public Entries: ${publicEntries}\n` +
`- Private Entries: ${privateEntries}`,
);
}
async deleteButton(ctx: TelegrafContext) {
await ctx.deleteMessage();
}
async privateButton(ctx: TelegrafContext) {
if (!(await this.isRegistered(ctx.from.id))) {
await ctx.reply('User not registered');
return;
}
await this.createEntry(ctx, true);
}
async publicButton(ctx: TelegrafContext) {
if (!(await this.isRegistered(ctx.from.id))) {
await ctx.reply('User not registered');
return;
}
await this.createEntry(ctx, false);
}
async createEntry(ctx: TelegrafContext, isPrivate: boolean) {
const data = this.entries.get(ctx.from.id);
if (!data) return;
if (ctx.update?.callback_query?.message?.message_id)
await ctx.telegram.editMessageReplyMarkup(
ctx.from.id,
ctx.update.callback_query.message.message_id,
undefined,
'',
);
this.entries.delete(ctx.from.id);
let image_id;
if (data.photo) {
image_id = uuid();
const link = await ctx.telegram.getFileLink(data.photo.id);
const res = await fetch(link)
.then((res) => res.arrayBuffer())
.then((buf) => Buffer.from(buf));
await this.minio.saveFile(
`${process.env.MINIO_ENTRY_IMAGE_PATH}/${image_id}.jpg`,
res,
);
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
image_width: data.photo?.width,
image_height: data.photo?.height,
content: data.content,
lat: data.location?.lat,
lon: data.location?.lon,
private: isPrivate,
agent: {
connect: { uid: String(ctx.from.id) },
},
},
});
await ctx.reply(`✅ ${isPrivate ? 'Private' : 'Public'} Entry created`);
}
isRegistered(uid: string | number): Promise<boolean> {
return this.prisma.agent
.count({
where: { uid: String(uid) },
})
.then((count) => count > 0);
}