Skip to content
Snippets Groups Projects
Verified Commit 62764523 authored by Adrian Paschkowski's avatar Adrian Paschkowski :thinking:
Browse files

Improve Telegram UX

parent 68907529
No related branches found
No related tags found
No related merge requests found
......@@ -27,7 +27,7 @@ export class TelegramService {
this.telegram.onText(/^\/profile/, this.profile.bind(this));
this.telegram.onText(/^\/start/, this.start.bind(this));
this.telegram.onText(/^\/entry (\w+)/, this.entry.bind(this));
this.telegram.onText(/^\/entry( (\w+))?/, this.entry.bind(this));
this.telegram.on('message', this.supplyValue.bind(this));
}
......@@ -35,11 +35,11 @@ export class TelegramService {
async start(msg: Message) {
await this.telegram.sendMessage(
msg.chat.id,
'/register [Name]\n - Registers you as a new Agent\n' +
'/unregister\n - Deletes all your Entries and Profile\n\n' +
'/entry [public / private]\n - Create new Public/Private Entry from your previous message cache\n' +
'/entry [clear]\n - Clears your current message cache\n' +
'/profile\n - Shows your current profile',
'/register <agentId> <name>\n - Registers you as a new Agent. The agentId 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' +
'/entry <public|private>\n - Create new Public/Private Entry from your previous message cache.\n' +
'/entry <clear>\n - Clears your current message cache.\n' +
'/profile\n - Shows your current profile.',
);
}
......@@ -53,10 +53,7 @@ export class TelegramService {
}
async entry(msg: Message, match: RegExpMatchArray) {
if (!match[1])
return this.sendError(msg, '/entry [private/public/clear]');
switch (match[1].toLowerCase()) {
switch (match[2]?.toLowerCase()) {
case 'private':
return await this.commitMessage(msg.from.id, true, msg);
case 'public':
......@@ -64,9 +61,14 @@ export class TelegramService {
case 'clear':
this.messageCache.delete(msg.from.id);
await this.telegram.sendMessage(
return void (await this.telegram.sendMessage(
msg.chat.id,
'♻ Message Queue cleared',
));
default:
return this.sendError(
msg,
'Usage: /entry <private|public|clear>',
);
}
}
......@@ -76,10 +78,7 @@ export class TelegramService {
return await this.sendError(msg, 'Not registered');
if (!this.messageCache.has(id))
return void (await this.telegram.sendMessage(
msg.chat.id,
'No queued Messages!',
));
return await this.sendError(msg, 'No queued Messages');
let entry: Partial<Entry> = this.messageCache.get(msg.from.id);
......@@ -91,9 +90,9 @@ export class TelegramService {
data: entry as any,
});
await this.telegram.sendMessage(
msg.chat.id,
`Created new ${isPrivate ? 'private' : 'public'} Entry!`,
await this.sendSuccess(
msg,
`Created new ${isPrivate ? 'private' : 'public'} Entry`,
);
// Send a notification to subscriptions
......@@ -121,20 +120,17 @@ export class TelegramService {
if (!match[1] && !match[2]) return;
if (await this.isRegistered(msg.from.id))
return void (await this.telegram.sendMessage(
msg.chat.id,
'Already registered',
));
return await this.sendError(msg, 'Already registered');
const existsCheck = await this.prismaService.agent.count({
where: { id: match[1], uid: '' },
});
if (existsCheck == 0)
return void (await this.telegram.sendMessage(
msg.chat.id,
'AgentId not found or already taken!',
));
return await this.sendError(
msg,
'agentId not found or already taken',
);
const agent = await this.prismaService.agent.update({
where: {
......@@ -151,9 +147,12 @@ export class TelegramService {
},
});
await this.telegram.sendMessage(
msg.chat.id,
`Id: ${agent.id}\nName: ${agent.name}\nCode: ${agent.slug}`,
await this.sendSuccess(
msg,
`Successfully registered!\n` +
`\\- Id: \`${agent.id}\`\n` +
`\\- Name: \`${agent.name}\`\n` +
`\\- Code: \`${agent.slug}\``,
);
}
......@@ -181,10 +180,7 @@ export class TelegramService {
where: { id: agent.id },
});
await this.telegram.sendMessage(
msg.chat.id,
'Successfully deleted all Entries',
);
await this.sendSuccess(msg, 'Deleted all Entries');
}
async profile(msg: Message) {
......@@ -206,11 +202,12 @@ export class TelegramService {
await this.telegram.sendMessage(
msg.chat.id,
`📂 Agent Information\n` +
`Id: ${agent.id}\n` +
`Name: ${agent.name}\n` +
`Code: ${agent.slug}\n` +
`Public Entries: ${publicEntries}\n` +
`Private Entries: ${privateEntries}`,
`\\- Id: \`${agent.id}\`\n` +
`\\- Name: \`${agent.name}\`\n` +
`\\- Code: \`${agent.slug}\`\n` +
`\\- Public Entries: \`${publicEntries}\`\n` +
`\\- Private Entries: \`${privateEntries}\``,
{ parse_mode: 'MarkdownV2' },
);
}
......@@ -244,6 +241,12 @@ export class TelegramService {
if (!(await this.isRegistered(msg.from.id))) return;
if (metadata.type === 'document')
return await this.sendError(
msg,
"Unsupported DataType\\. Please send pictures as a 'Photo', not as a 'File'\\.",
);
const agent = await this.prismaService.agent.findFirst({
where: { uid: String(msg.from.id) },
});
......@@ -262,37 +265,56 @@ export class TelegramService {
);
}
console.log(msg.text, msg.caption, msg);
entry.agentId = agent.id;
if (metadata.type === 'photo') {
const imageEntry = await this.receiveImage(msg);
switch (metadata.type) {
case 'photo':
const imageEntry = await this.receiveImage(msg);
if (entry.image)
await this.telegram.sendMessage(
msg.chat.id,
'✂ Overwriting previous image',
);
entry.image = imageEntry.image;
await this.telegram.sendMessage(msg.chat.id, '➡ Queued Image');
if (!msg.caption) {
// Allow captions to be handled like text messages
break;
}
case 'text':
if (entry.content)
await this.telegram.sendMessage(
msg.chat.id,
'✂ Overwriting previous text',
);
entry.content = msg.text || msg.caption;
await this.telegram.sendMessage(msg.chat.id, '➡ Queued Text');
break;
case 'location':
if (entry.lat)
await this.telegram.sendMessage(
msg.chat.id,
'✂ Overwriting previous location',
);
entry.lat = msg.location.latitude.toString();
entry.lon = msg.location.longitude.toString();
if (entry.image)
await this.telegram.sendMessage(
msg.chat.id,
'✂ Overwriting previous image',
'➡ Queued Location',
);
break;
entry.image = imageEntry.image;
await this.telegram.sendMessage(msg.chat.id, '➡ Queued Image');
} else if (metadata.type === 'text') {
if (entry.content)
await this.telegram.sendMessage(
msg.chat.id,
'✂ Overwriting previous text',
);
entry.content = msg.text;
await this.telegram.sendMessage(msg.chat.id, '➡ Queued Text');
} else if (metadata.type === 'location') {
entry.lat = msg.location.latitude.toString();
entry.lon = msg.location.longitude.toString();
await this.telegram.sendMessage(msg.chat.id, '➡ Queued Location');
} else {
return await this.sendError(msg, 'Unsupported DataType');
default:
return await this.sendError(msg, 'Unsupported DataType');
}
this.messageCache.set(msg.from.id, entry);
......@@ -307,6 +329,14 @@ export class TelegramService {
}
private async sendError(msg: Message, message: string) {
await this.telegram.sendMessage(msg.chat.id, `❌ ${message}`);
await this.telegram.sendMessage(msg.chat.id, `❌ ${message}`, {
parse_mode: 'MarkdownV2',
});
}
private async sendSuccess(msg: Message, message: string) {
await this.telegram.sendMessage(msg.chat.id, `✅ ${message}`, {
parse_mode: 'MarkdownV2',
});
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment