Skip to content

Commit e6c79f9

Browse files
committed
Merge branch 'feat/send-messages-as-markdown' into development
2 parents d9a0278 + b1d4d64 commit e6c79f9

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/modules/Eggcart.js

+40-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ function escapeMarkdownV2Characters(text) {
1313
return text.replace(/([_()*~`>#+-=|{}[\].!\\])/g, '\\$1');
1414
}
1515

16+
function beautifyText(text) {
17+
// Remove leading and trailing whitespace
18+
let trimmedText = text.trim();
19+
20+
// Remove a trailing period if present
21+
if (trimmedText.endsWith('.')) {
22+
trimmedText = trimmedText.slice(0, -1);
23+
}
24+
25+
// Capitalize the first letter
26+
return trimmedText.replace(/^\w/, c => c.toUpperCase());
27+
}
28+
29+
1630
class EggCart {
1731
constructor() {
1832
this.listController = new EggoListController();
@@ -33,19 +47,20 @@ class EggCart {
3347

3448
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
3549
let itemsToAdd = messageText.slice(messageText.indexOf(" ") + 1).split(",");
36-
let response = 'Okay! \n';
37-
50+
let response = 'Okay\\! \n';
3851

3952
for (let itemText of itemsToAdd) {
4053
try {
41-
await this.listController.addItem(itemText.trim());
42-
response += `${itemText.trim()}, `;
54+
await this.listController.addItem(beautifyText(itemText.trim()));
55+
response += `*${escapeMarkdownV2Characters(beautifyText(itemText.trim()))}*, `;
56+
4357
} catch (error) {
4458
console.error(error);
4559
}
4660
}
47-
response = response.slice(0, -2) + ' are on the shopping list!';
48-
ctx.reply(response);
61+
62+
response = response.slice(0, -2) + ' is \\(are\\) on the shopping list\\.';
63+
ctx.replyWithMarkdownV2(response);
4964
}
5065
});
5166
}
@@ -63,18 +78,21 @@ class EggCart {
6378
let response = '';
6479

6580
for (let itemName of itemsToRemove) {
66-
let escapedItemName = escapeMarkdownV2Characters(itemName.trim());
81+
let escapedItemName = escapeMarkdownV2Characters(beautifyText(itemName.trim()));
82+
6783
try {
68-
const item = await this.listController.findItemByName(itemName.trim());
84+
const item = await this.listController.findItemByName(beautifyText(itemName.trim()));
6985
if (item) {
7086
await this.listController.removeItem(item.id);
7187
response += `Okay\\!\n*${escapedItemName}* removed from the shopping list\\.\n`;
88+
7289
} else {
7390
response += `Oh\\!\n*${escapedItemName}* not found in the shopping list\\.\n`;
7491
}
92+
7593
} catch (error) {
7694
console.error(error);
77-
response += `Oh\\!\nError removing *${escapedItemName}*\\.\n`;
95+
response += `Oh\\!\nError removing *${escapedItemName}* from the shopping list\\.\n`;
7896
}
7997
}
8098

@@ -98,17 +116,19 @@ class EggCart {
98116
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
99117
try {
100118
let items = await this.listController.getItems();
101-
let response = 'Grocery List\n';
119+
let response = '*Grocery List*\n';
120+
102121
items.forEach((item, index) => {
103-
response += `${index + 1}. ${item.item}\n`;
122+
response += `${index + 1}\\. ${escapeMarkdownV2Characters(item.item)}\n`;
104123
});
124+
105125
if (items.length === 0) {
106-
response = "Nothing to shop for :o - try adding eggs";
126+
response = "Nothing to shop for\\. \nTry adding eggs\\.";
107127
}
108-
ctx.reply(response);
128+
ctx.replyWithMarkdownV2(response);
109129
} catch (error) {
110130
console.error(error);
111-
ctx.reply("An error occurred while getting the list.");
131+
ctx.replyWithMarkdownV2("An error occurred while getting the list\\.");
112132
}
113133
}
114134
});
@@ -128,15 +148,15 @@ class EggCart {
128148
for (const item of items) {
129149
await this.listController.removeItem(item.id);
130150
}
131-
ctx.reply("The shopping list has been cleared!");
151+
ctx.replyWithMarkdownV2("The shopping list has been cleared\\.");
132152
} catch (error) {
133153
console.error(error);
134-
ctx.reply("An error occurred while clearing the list.");
154+
ctx.replyWithMarkdownV2("An error occurred while clearing the list\\.");
135155
}
136156
}
137157
});
138158
}
139-
159+
140160
/**
141161
* Provide help information via the bot command.
142162
*/
@@ -147,8 +167,8 @@ class EggCart {
147167

148168
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
149169
ctx.reply(
150-
"Add an item: /add eggs, milk\n" +
151-
"Remove an item: /remove eggs, milk\n" +
170+
"Add an item: /add Eggs, Milk\n" +
171+
"Remove an item: /remove Eggs, Milk\n" +
152172
"Show the list: /list\n" +
153173
"Clear the list: /clear"
154174
);
@@ -162,6 +182,7 @@ class EggCart {
162182
connect() {
163183
this.bot.launch().then(() => {
164184
console.log('Bot launched successfully');
185+
165186
}).catch(error => {
166187
console.error('Error launching bot:', error);
167188
});

0 commit comments

Comments
 (0)