Newer
Older
import { Injectable } from '@nestjs/common';
import { Message, Metadata } from 'node-telegram-bot-api';
import * as TelegramBot from 'node-telegram-bot-api';
import { PrismaService } from '../prisma/prisma.service';
import * as fs from 'fs';
import * as path from 'path';
import { v4 as uuid } from 'uuid';
@Injectable()
export class TelegramService {
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
private telegram: TelegramBot;
constructor(private prismaService: PrismaService) {
this.telegram = new TelegramBot(process.env.BOT_TOKEN, {
polling: true,
});
this.telegram.onText(/^\/register (.+)/, this.register.bind(this));
this.telegram.onText(/^\/unregister/, this.unregister.bind(this));
this.telegram.onText(/^\/state/, this.state.bind(this));
this.telegram.onText(/^\/start/, this.start.bind(this));
this.telegram.onText(/^\/commit (.+)/, this.commit.bind(this));
this.telegram.on('message', this.supplyValue.bind(this));
}
private lower = parseInt('aaaaaaa', 36);
private upper = parseInt('zzzzzzzz', 36);
private generateSlug() {
return Math.floor(
Math.random() * (this.upper - this.lower) + this.lower,
).toString(36);
}
async start(msg: Message, match: RegExpMatchArray) {
await this.telegram.sendMessage(
msg.chat.id,
`/register [Name]\n/state\n/unregister`,
);
}
async commit(msg: Message, match: RegExpMatchArray) {
if (!match[1])
return void (await this.telegram.sendMessage(
msg.chat.id,
'No message',
));
if (!(await this.isRegistered(msg.from.id)))
return void (await this.telegram.sendMessage(
msg.chat.id,
'Not registered',
));
const agent = await this.prismaService.agent.findFirst({
where: { uid: String(msg.from.id) },
});
await this.prismaService.entry.create({
data: {
agentId: agent.id,
content: match[1],
private: false,
type: 'text',
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
},
});
await this.telegram.sendMessage(msg.chat.id, 'Commit accepted');
}
async register(msg: Message, match: RegExpMatchArray) {
if (!match[1])
return void (await this.telegram.sendMessage(
msg.chat.id,
'Invalid name format',
));
if (await this.isRegistered(msg.from.id))
return void (await this.telegram.sendMessage(
msg.chat.id,
'Already registered',
));
const agent = await this.prismaService.agent.create({
data: {
uid: String(msg.from.id),
name: match[1],
slug: this.generateSlug(),
},
});
await this.telegram.sendMessage(
msg.chat.id,
`Id: ${agent.id}\nName: ${agent.name}\nCode: ${agent.slug}`,
);
}
async unregister(msg: Message, match: RegExpMatchArray) {
if (!(await this.isRegistered(msg.from.id)))
return void (await this.telegram.sendMessage(
msg.chat.id,
'Not registered!',
));
const agent = await this.prismaService.agent.findFirst({
where: { uid: String(msg.from.id) },
});
await this.prismaService.entry.deleteMany({
where: { agentId: agent.id },
});
await this.prismaService.entry.deleteMany({
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
where: { agentId: agent.id },
});
await this.prismaService.agent.delete({
where: { id: agent.id },
});
await this.telegram.sendMessage(
msg.chat.id,
'Successfully deleted all Entries',
);
}
async state(msg: Message, match: RegExpMatchArray) {
if (!(await this.isRegistered(msg.from.id)))
return void (await this.telegram.sendMessage(
msg.chat.id,
'Not registered!',
));
const agent = await this.prismaService.agent.findFirst({
where: { uid: String(msg.from.id) },
});
const entries = await this.prismaService.entry.count({
where: { agentId: agent.id },
});
const unlockEntries = await this.prismaService.entry.count({
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
where: { agentId: agent.id },
});
await this.telegram.sendMessage(
msg.chat.id,
`Id: ${agent.id}\nName: ${agent.name}\nCode: ${agent.slug}\nPublic Entries: ${entries}\nUnlockable Entries: ${unlockEntries}`,
);
}
async supplyValue(msg: Message, metadata: Metadata) {
if (metadata.type === 'text' && msg.text.startsWith('/')) return;
if (!(await this.isRegistered(msg.from.id))) return;
const agent = await this.prismaService.agent.findFirst({
where: { uid: String(msg.from.id) },
});
if (metadata.type === 'photo') {
let file,
size = -1;
for (const p of msg.photo) {
if (p.width > size) {
size = p.width;
file = p.file_id;
}
}
const id = uuid();
const dest = fs.createWriteStream(
path.join(process.cwd(), 'static', 'photos', `${id}.jpg`),
);
const pipe = this.telegram.getFileStream(file).pipe(dest);
await new Promise((resolve) => pipe.on('finish', resolve));
await this.prismaService.entry.create({
image: `${id}.jpg`,
private: true,
},
});
return void (await this.telegram.sendMessage(
msg.chat.id,
`Accepted Photo`,
));
} else if (metadata.type === 'text') {
await this.prismaService.entry.create({
data: {
type: 'text',
agentId: agent.id,
content: msg.text,
},
});
return void (await this.telegram.sendMessage(
msg.chat.id,
'Accepted Text',
));
} else if (metadata.type === 'location') {
await this.prismaService.entry.create({
data: {
agentId: agent.id,
type: 'location',
lat: msg.location.latitude.toString(),
lon: msg.location.longitude.toString(),
},
});
return void (await this.telegram.sendMessage(
msg.chat.id,
'Accepted Location',
));
} else {
return void (await this.telegram.sendMessage(
msg.chat.id,
'Unsupported DataType',
));
private async isRegistered(uid: string | number): Promise<boolean> {
return (
(await this.prismaService.agent.count({
where: { uid: String(uid) },
})) > 0
);
}