@@ -13,6 +13,20 @@ function escapeMarkdownV2Characters(text) {
13
13
return text . replace ( / ( [ _ ( ) * ~ ` > # + - = | { } [ \] . ! \\ ] ) / g, '\\$1' ) ;
14
14
}
15
15
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
+
16
30
class EggCart {
17
31
constructor ( ) {
18
32
this . listController = new EggoListController ( ) ;
@@ -33,19 +47,20 @@ class EggCart {
33
47
34
48
if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
35
49
let itemsToAdd = messageText . slice ( messageText . indexOf ( " " ) + 1 ) . split ( "," ) ;
36
- let response = 'Okay! \n' ;
37
-
50
+ let response = 'Okay\\! \n' ;
38
51
39
52
for ( let itemText of itemsToAdd ) {
40
53
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
+
43
57
} catch ( error ) {
44
58
console . error ( error ) ;
45
59
}
46
60
}
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 ) ;
49
64
}
50
65
} ) ;
51
66
}
@@ -63,18 +78,21 @@ class EggCart {
63
78
let response = '' ;
64
79
65
80
for ( let itemName of itemsToRemove ) {
66
- let escapedItemName = escapeMarkdownV2Characters ( itemName . trim ( ) ) ;
81
+ let escapedItemName = escapeMarkdownV2Characters ( beautifyText ( itemName . trim ( ) ) ) ;
82
+
67
83
try {
68
- const item = await this . listController . findItemByName ( itemName . trim ( ) ) ;
84
+ const item = await this . listController . findItemByName ( beautifyText ( itemName . trim ( ) ) ) ;
69
85
if ( item ) {
70
86
await this . listController . removeItem ( item . id ) ;
71
87
response += `Okay\\!\n*${ escapedItemName } * removed from the shopping list\\.\n` ;
88
+
72
89
} else {
73
90
response += `Oh\\!\n*${ escapedItemName } * not found in the shopping list\\.\n` ;
74
91
}
92
+
75
93
} catch ( error ) {
76
94
console . error ( error ) ;
77
- response += `Oh\\!\nError removing *${ escapedItemName } *\\.\n` ;
95
+ response += `Oh\\!\nError removing *${ escapedItemName } * from the shopping list \\.\n` ;
78
96
}
79
97
}
80
98
@@ -98,17 +116,19 @@ class EggCart {
98
116
if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
99
117
try {
100
118
let items = await this . listController . getItems ( ) ;
101
- let response = 'Grocery List\n' ;
119
+ let response = '*Grocery List*\n' ;
120
+
102
121
items . forEach ( ( item , index ) => {
103
- response += `${ index + 1 } . ${ item . item } \n` ;
122
+ response += `${ index + 1 } \\ . ${ escapeMarkdownV2Characters ( item . item ) } \n` ;
104
123
} ) ;
124
+
105
125
if ( items . length === 0 ) {
106
- response = "Nothing to shop for :o - try adding eggs" ;
126
+ response = "Nothing to shop for\\. \nTry adding eggs\\. " ;
107
127
}
108
- ctx . reply ( response ) ;
128
+ ctx . replyWithMarkdownV2 ( response ) ;
109
129
} catch ( error ) {
110
130
console . error ( error ) ;
111
- ctx . reply ( "An error occurred while getting the list." ) ;
131
+ ctx . replyWithMarkdownV2 ( "An error occurred while getting the list\\ ." ) ;
112
132
}
113
133
}
114
134
} ) ;
@@ -128,15 +148,15 @@ class EggCart {
128
148
for ( const item of items ) {
129
149
await this . listController . removeItem ( item . id ) ;
130
150
}
131
- ctx . reply ( "The shopping list has been cleared! " ) ;
151
+ ctx . replyWithMarkdownV2 ( "The shopping list has been cleared\\. " ) ;
132
152
} catch ( error ) {
133
153
console . error ( error ) ;
134
- ctx . reply ( "An error occurred while clearing the list." ) ;
154
+ ctx . replyWithMarkdownV2 ( "An error occurred while clearing the list\\ ." ) ;
135
155
}
136
156
}
137
157
} ) ;
138
158
}
139
-
159
+
140
160
/**
141
161
* Provide help information via the bot command.
142
162
*/
@@ -147,8 +167,8 @@ class EggCart {
147
167
148
168
if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
149
169
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" +
152
172
"Show the list: /list\n" +
153
173
"Clear the list: /clear"
154
174
) ;
@@ -162,6 +182,7 @@ class EggCart {
162
182
connect ( ) {
163
183
this . bot . launch ( ) . then ( ( ) => {
164
184
console . log ( 'Bot launched successfully' ) ;
185
+
165
186
} ) . catch ( error => {
166
187
console . error ( 'Error launching bot:' , error ) ;
167
188
} ) ;
0 commit comments