@@ -17,27 +17,36 @@ class EggCart {
17
17
constructor ( ) {
18
18
this . listController = new EggoListController ( ) ;
19
19
this . bot = new Telegraf ( config . telegram . token ) ;
20
+
21
+ this . bot . telegram . getMe ( ) . then ( ( botInfo ) => {
22
+ this . botName = botInfo . username ;
23
+ } ) ;
20
24
}
21
25
22
26
/**
23
27
* Add an item to the shopping list via the bot command.
24
28
*/
25
29
addItem ( ) {
26
30
this . bot . command ( 'add' , async ( ctx ) => {
27
- let messageText = ctx . update . message . text ;
28
- let itemsToAdd = messageText . slice ( messageText . indexOf ( " " ) + 1 ) . split ( "," ) ;
29
- let response = 'Okay! \n' ;
31
+ const messageText = ctx . update . message . text ;
32
+ const chatType = ctx . update . message . chat . type ;
30
33
31
- for ( let itemText of itemsToAdd ) {
32
- try {
33
- await this . listController . addItem ( itemText . trim ( ) ) ;
34
- response += `${ itemText . trim ( ) } , ` ;
35
- } catch ( error ) {
36
- console . error ( error ) ;
34
+ if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
35
+ let itemsToAdd = messageText . slice ( messageText . indexOf ( " " ) + 1 ) . split ( "," ) ;
36
+ let response = 'Okay! \n' ;
37
+
38
+
39
+ for ( let itemText of itemsToAdd ) {
40
+ try {
41
+ await this . listController . addItem ( itemText . trim ( ) ) ;
42
+ response += `${ itemText . trim ( ) } , ` ;
43
+ } catch ( error ) {
44
+ console . error ( error ) ;
45
+ }
37
46
}
47
+ response = response . slice ( 0 , - 2 ) + ' are on the shopping list!' ;
48
+ ctx . reply ( response ) ;
38
49
}
39
- response = response . slice ( 0 , - 2 ) + ' are on the shopping list!' ;
40
- ctx . reply ( response ) ;
41
50
} ) ;
42
51
}
43
52
@@ -46,31 +55,35 @@ class EggCart {
46
55
*/
47
56
deleteItem ( ) {
48
57
this . bot . command ( 'remove' , async ( ctx ) => {
49
- let messageText = ctx . update . message . text ;
50
- let itemsToRemove = messageText . slice ( messageText . indexOf ( " " ) + 1 ) . split ( "," ) ;
51
- let response = '' ;
58
+ const messageText = ctx . update . message . text ;
59
+ const chatType = ctx . update . message . chat . type ;
52
60
53
- for ( let itemName of itemsToRemove ) {
54
- let escapedItemName = escapeMarkdownV2Characters ( itemName . trim ( ) ) ;
55
- try {
56
- const item = await this . listController . findItemByName ( itemName . trim ( ) ) ;
57
- if ( item ) {
58
- await this . listController . removeItem ( item . id ) ;
59
- response += `Okay\\!\n*${ escapedItemName } * removed from the shopping list\\.\n` ;
60
- } else {
61
- response += `Oh\\!\n*${ escapedItemName } * not found in the shopping list\\.\n` ;
61
+ if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
62
+ let itemsToRemove = messageText . slice ( messageText . indexOf ( " " ) + 1 ) . split ( "," ) ;
63
+ let response = '' ;
64
+
65
+ for ( let itemName of itemsToRemove ) {
66
+ let escapedItemName = escapeMarkdownV2Characters ( itemName . trim ( ) ) ;
67
+ try {
68
+ const item = await this . listController . findItemByName ( itemName . trim ( ) ) ;
69
+ if ( item ) {
70
+ await this . listController . removeItem ( item . id ) ;
71
+ response += `Okay\\!\n*${ escapedItemName } * removed from the shopping list\\.\n` ;
72
+ } else {
73
+ response += `Oh\\!\n*${ escapedItemName } * not found in the shopping list\\.\n` ;
74
+ }
75
+ } catch ( error ) {
76
+ console . error ( error ) ;
77
+ response += `Oh\\!\nError removing *${ escapedItemName } *\\.\n` ;
62
78
}
63
- } catch ( error ) {
64
- console . error ( error ) ;
65
- response += `Oh\\!\nError removing *${ escapedItemName } *\\.\n` ;
66
79
}
67
- }
68
-
69
- if ( response === '' ) {
70
- response = "No items specified for removal\\." ;
71
- }
72
-
73
- ctx . replyWithMarkdownV2 ( response ) ;
80
+
81
+ if ( response === '' ) {
82
+ response = "No items specified for removal\\." ;
83
+ }
84
+
85
+ ctx . replyWithMarkdownV2 ( response ) ;
86
+ }
74
87
} ) ;
75
88
}
76
89
@@ -79,19 +92,24 @@ class EggCart {
79
92
*/
80
93
getList ( ) {
81
94
this . bot . command ( 'list' , async ( ctx ) => {
82
- try {
83
- let items = await this . listController . getItems ( ) ;
84
- let response = 'Grocery List\n' ;
85
- items . forEach ( ( item , index ) => {
86
- response += `${ index + 1 } . ${ item . item } \n` ;
87
- } ) ;
88
- if ( items . length === 0 ) {
89
- response = "Nothing to shop for :o - try adding eggs" ;
95
+ const messageText = ctx . update . message . text ;
96
+ const chatType = ctx . update . message . chat . type ;
97
+
98
+ if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
99
+ try {
100
+ let items = await this . listController . getItems ( ) ;
101
+ let response = 'Grocery List\n' ;
102
+ items . forEach ( ( item , index ) => {
103
+ response += `${ index + 1 } . ${ item . item } \n` ;
104
+ } ) ;
105
+ if ( items . length === 0 ) {
106
+ response = "Nothing to shop for :o - try adding eggs" ;
107
+ }
108
+ ctx . reply ( response ) ;
109
+ } catch ( error ) {
110
+ console . error ( error ) ;
111
+ ctx . reply ( "An error occurred while getting the list." ) ;
90
112
}
91
- ctx . reply ( response ) ;
92
- } catch ( error ) {
93
- console . error ( error ) ;
94
- ctx . reply ( "An error occurred while getting the list." ) ;
95
113
}
96
114
} ) ;
97
115
}
@@ -101,15 +119,20 @@ class EggCart {
101
119
*/
102
120
clearList ( ) {
103
121
this . bot . command ( 'clear' , async ( ctx ) => {
104
- try {
105
- let items = await this . listController . getItems ( ) ;
106
- for ( const item of items ) {
107
- await this . listController . removeItem ( item . id ) ;
122
+ const messageText = ctx . update . message . text ;
123
+ const chatType = ctx . update . message . chat . type ;
124
+
125
+ if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
126
+ try {
127
+ let items = await this . listController . getItems ( ) ;
128
+ for ( const item of items ) {
129
+ await this . listController . removeItem ( item . id ) ;
130
+ }
131
+ ctx . reply ( "The shopping list has been cleared!" ) ;
132
+ } catch ( error ) {
133
+ console . error ( error ) ;
134
+ ctx . reply ( "An error occurred while clearing the list." ) ;
108
135
}
109
- ctx . reply ( "The shopping list has been cleared!" ) ;
110
- } catch ( error ) {
111
- console . error ( error ) ;
112
- ctx . reply ( "An error occurred while clearing the list." ) ;
113
136
}
114
137
} ) ;
115
138
}
@@ -119,13 +142,17 @@ class EggCart {
119
142
*/
120
143
help ( ) {
121
144
this . bot . help ( ( ctx ) => {
122
- ctx . reply (
123
- "Add an item: /add eggs, milk\n" +
124
- "Remove an item: /remove eggs, milk\n" +
125
- "Show the list: /list\n" +
126
- "Clear the list: /clear"
145
+ const messageText = ctx . update . message . text ;
146
+ const chatType = ctx . update . message . chat . type ;
127
147
128
- ) ;
148
+ if ( messageText . includes ( `@${ this . botName } ` ) || chatType === 'private' || chatType === 'group' ) {
149
+ ctx . reply (
150
+ "Add an item: /add eggs, milk\n" +
151
+ "Remove an item: /remove eggs, milk\n" +
152
+ "Show the list: /list\n" +
153
+ "Clear the list: /clear"
154
+ ) ;
155
+ }
129
156
} ) ;
130
157
}
131
158
0 commit comments