1
1
'use strict' ;
2
- const Tgfancy = require ( " tgfancy" ) ;
3
- const bot_api_token = process . env . BOT_API_TOKEN ;
2
+ const Tgfancy = require ( ' tgfancy' ) ;
3
+ const botApiToken = process . env . BOT_API_TOKEN ;
4
4
5
- const tgBot = new Tgfancy ( bot_api_token , {
5
+ const tgBot = new Tgfancy ( botApiToken , {
6
6
tgfancy : {
7
7
emojification : true ,
8
8
} ,
9
- polling : true
9
+ polling : true ,
10
10
} ) ;
11
11
const CronJob = require ( 'cron' ) . CronJob ;
12
12
@@ -15,194 +15,195 @@ const parser = new Parser();
15
15
const Subscriptions = require ( './subscriptions' ) ;
16
16
const subscriptions = new Subscriptions ( ) ;
17
17
18
- const weekdays = [ " (Mon(day)?)" , " (Tue(sday)?)" , " (Wed(nesday)?)" , " Thu(rsday)?" , " Fri(day)?" ] ;
19
- const weekdayRegex = new RegExp ( weekdays . join ( " (@\w+)?|" ) , 'i' ) ;
18
+ const weekdays = [ ' (Mon(day)?)' , ' (Tue(sday)?)' , ' (Wed(nesday)?)' , ' Thu(rsday)?' , ' Fri(day)?' ] ;
19
+ const weekdayRegex = new RegExp ( weekdays . join ( ' (@\w+)?|' ) , 'i' ) ;
20
20
21
21
let botUsername = process . env . BOT_USERNAME ;
22
- if ( botUsername === undefined ) {
23
- botUsername = " FiveMoodsBot" ;
22
+ if ( botUsername === undefined ) {
23
+ botUsername = ' FiveMoodsBot' ;
24
24
}
25
25
26
26
/* Daily cronjob to notify subscribers*/
27
27
try {
28
- new CronJob ( '00 10 * * 1-5' , function ( ) {
28
+ new CronJob ( '00 10 * * 1-5' , function ( ) {
29
29
console . log ( 'Cron job started' ) ;
30
30
notifySubscribers ( ) ;
31
- } , null , true , 'Europe/Zurich' ) ;
32
- } catch ( ex ) {
33
- console . log ( " cron pattern not valid" ) ;
31
+ } , null , true , 'Europe/Zurich' ) ;
32
+ } catch ( ex ) {
33
+ console . log ( ' cron pattern not valid' ) ;
34
34
}
35
35
36
36
/* Routes */
37
- tgBot . onText ( / \/ ( g e t ) ? ( T o d a y ) ? ( @ \w + ) ? $ / gmi, getTodayHandler ) ;
38
- tgBot . onText ( / \/ ( g e t ) ? W e e k ( @ \w + ) ? $ / gmi, getWeekHandler ) ;
39
- tgBot . onText ( / \/ ( g e t ) ? D a i l y ( @ \w + ) ? $ / gmi, getDailyHandler ) ;
40
- tgBot . onText ( / \/ ( g e t ) ? P a r t T i m e ( @ \w + ) ? / i, getPartTimeHandler ) ;
41
- tgBot . onText ( / \/ s t a r t ( @ \w + ) ? / i, startHandler ) ;
42
- tgBot . onText ( / \/ s t o p ( @ \w + ) ? / i, cancelSubscriptionsHandler ) ;
43
- tgBot . onText ( / \/ c a n c e l ( @ \w + ) ? / i, cancelSubscriptionsHandler ) ;
44
- tgBot . onText ( / \/ ( g e t ) ? s o u r c e ( @ \w + ) ? / mgi, getSourceHandler ) ;
45
- tgBot . onText ( weekdayRegex , weekdayHandler ) ;
46
- tgBot . onText ( / D o n e .* ( @ \w + ) ? / i, cancelWeekdaySelectionHandler ) ;
37
+ tgBot . onText ( / \/ ( g e t ) ? ( T o d a y ) ? ( @ \w + ) ? $ / gmi, getTodayHandler ) ;
38
+ tgBot . onText ( / \/ ( g e t ) ? W e e k ( @ \w + ) ? $ / gmi, getWeekHandler ) ;
39
+ tgBot . onText ( / \/ ( g e t ) ? D a i l y ( @ \w + ) ? $ / gmi, getDailyHandler ) ;
40
+ tgBot . onText ( / \/ ( g e t ) ? P a r t T i m e ( @ \w + ) ? / i, getPartTimeHandler ) ;
41
+ tgBot . onText ( / \/ s t a r t ( @ \w + ) ? / i, startHandler ) ;
42
+ tgBot . onText ( / \/ s t o p ( @ \w + ) ? / i, cancelSubscriptionsHandler ) ;
43
+ tgBot . onText ( / \/ c a n c e l ( @ \w + ) ? / i, cancelSubscriptionsHandler ) ;
44
+ tgBot . onText ( / \/ ( g e t ) ? s o u r c e ( @ \w + ) ? / mgi, getSourceHandler ) ;
45
+ tgBot . onText ( weekdayRegex , weekdayHandler ) ;
46
+ tgBot . onText ( / D o n e .* ( @ \w + ) ? / i, cancelWeekdaySelectionHandler ) ;
47
47
// tgBot.onText(/notify/gi,notifySubscribers); //to test subscriber notifications
48
48
49
49
/* Handlers */
50
50
function getTodayHandler ( message ) {
51
- let chatId = message . chat . id ;
51
+ const chatId = message . chat . id ;
52
52
sendTodaysMenu ( chatId ) ;
53
53
}
54
54
55
55
function getWeekHandler ( message ) {
56
- let chatId = message . chat . id ;
56
+ const chatId = message . chat . id ;
57
57
sendWeekMenu ( chatId ) ;
58
58
}
59
59
60
60
function getDailyHandler ( message ) {
61
- let chat = message . chat ;
62
- let chatId = message . chat . id ;
61
+ const chat = message . chat ;
62
+ const chatId = message . chat . id ;
63
63
64
- subscriptions . add ( chat , function ( ) {
65
- let markdownText = " *Successfully added you to the daily subscriber list* \n" +
66
- " I will send you the menu at 10:00am from now on. \n" +
64
+ subscriptions . add ( chat , function ( ) {
65
+ const markdownText = ' *Successfully added you to the daily subscriber list* \n' +
66
+ ' I will send you the menu at 10:00am from now on. \n' +
67
67
`You can send me /stop@${ botUsername } to quit that.` ;
68
- tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
68
+ tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
69
69
} ) ;
70
70
}
71
71
72
72
function getPartTimeHandler ( message ) {
73
- let chatId = message . chat . id ;
73
+ const chatId = message . chat . id ;
74
74
75
75
// remove current subscriptions before selecting new ones.
76
76
subscriptions . remove ( chatId ) ;
77
77
78
78
const weekdaysKeyboard = [
79
- [ { text : " Monday" } , { text : " Tuesday" } , ] ,
80
- [ { text : " Wednesday" } , { text : " Thursday" } ] ,
81
- [ { text : " Friday" } ] , [ { text : " Done!" } ]
79
+ [ { text : ' Monday' } , { text : ' Tuesday' } ] ,
80
+ [ { text : ' Wednesday' } , { text : ' Thursday' } ] ,
81
+ [ { text : ' Friday' } ] , [ { text : ' Done!' } ] ,
82
82
] ;
83
83
84
- let markdownText = " Please select all weekdays you want to be notified." ;
84
+ const markdownText = ' Please select all weekdays you want to be notified.' ;
85
85
86
- let options = {
87
- " parse_mode" : " Markdown" ,
88
- " reply_markup" : {
89
- " keyboard" : weekdaysKeyboard ,
90
- " selective" : true ,
91
- }
86
+ const options = {
87
+ ' parse_mode' : ' Markdown' ,
88
+ ' reply_markup' : {
89
+ ' keyboard' : weekdaysKeyboard ,
90
+ ' selective' : true ,
91
+ } ,
92
92
} ;
93
93
tgBot . sendMessage ( chatId , markdownText , options ) ;
94
94
}
95
95
96
- function weekdayHandler ( message , match ) {
97
- let chat = message . chat ;
98
- let chatId = message . chat . id ;
99
- let markdownText = " Okay I will send you updates each " + match [ 0 ] ;
100
- let weekdayIndex = + weekdays . regexIndexOf ( match [ 0 ] ) + 1 ; //+1 since dates/days start on Sunday .
96
+ function weekdayHandler ( message , match ) {
97
+ const chat = message . chat ;
98
+ const chatId = message . chat . id ;
99
+ const markdownText = ' Okay I will send you updates each ' + match [ 0 ] ;
100
+ const weekdayIndex = + weekdays . regexIndexOf ( match [ 0 ] ) + 1 ; // +1 since dates/days start on Sunday .
101
101
102
- subscriptions . addWeekday ( chat , weekdayIndex , function ( ) {
102
+ subscriptions . addWeekday ( chat , weekdayIndex , function ( ) {
103
103
tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
104
104
weekdays . regexIndexOf ( match [ 0 ] ) ;
105
105
} ) ;
106
-
107
106
}
108
107
109
108
function cancelWeekdaySelectionHandler ( message ) {
110
- let chat = message . chat ;
111
- let chatId = message . chat . id ;
109
+ const chat = message . chat ;
110
+ const chatId = message . chat . id ;
112
111
113
- subscriptions . getWeekdays ( chat , function ( err , weekdaysData ) {
114
- let markdownText = "" ;
115
- if ( err ) {
116
- console . log ( " error getting weekdays from mongodb" ) ;
112
+ subscriptions . getWeekdays ( chat , function ( err , weekdaysData ) {
113
+ let markdownText = '' ;
114
+ if ( err ) {
115
+ console . log ( ' error getting weekdays from mongodb' ) ;
117
116
return ;
118
117
}
119
118
120
- if ( weekdaysData === null ) {
121
- markdownText = " You did not select any Weekdays :confused: \nI will not send you updates" ;
122
- } else {
123
- markdownText = " :thumbsup: Alright. Here are the days I will send you the menu:" ;
124
- for ( let weekday of weekdaysData . weekdays ) {
125
- let weekdayName = weekdays [ weekday - 1 ] . replace ( / [ ( ) ? ] / gi, "" ) ;
119
+ if ( weekdaysData === null ) {
120
+ markdownText = ' You did not select any Weekdays :confused: \nI will not send you updates' ;
121
+ } else {
122
+ markdownText = ' :thumbsup: Alright. Here are the days I will send you the menu:' ;
123
+ for ( const weekday of weekdaysData . weekdays ) {
124
+ const weekdayName = weekdays [ weekday - 1 ] . replace ( / [ ( ) ? ] / gi, '' ) ;
126
125
markdownText += `\n- ${ weekdayName } ` ;
127
126
}
128
127
}
129
128
130
- let options = {
131
- " parse_mode" : " HTML" ,
132
- " reply_markup" : {
133
- " remove_keyboard" : true ,
134
- " selective" : true
135
- }
129
+ const options = {
130
+ ' parse_mode' : ' HTML' ,
131
+ ' reply_markup' : {
132
+ ' remove_keyboard' : true ,
133
+ ' selective' : true ,
134
+ } ,
136
135
} ;
137
136
tgBot . sendMessage ( chatId , markdownText , options ) ;
138
137
} ) ;
139
138
}
140
139
function cancelSubscriptionsHandler ( message ) {
141
- let chatId = message . chat . id ;
140
+ const chatId = message . chat . id ;
142
141
143
- subscriptions . remove ( chatId , function ( ) {
144
- let markdownText = " *Successfully removed you from the daily subscriber list* \n" +
145
- " I will no longer send you updates. :wave:" ;
146
- tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
142
+ subscriptions . remove ( chatId , function ( ) {
143
+ const markdownText = ' *Successfully removed you from the daily subscriber list* \n' +
144
+ ' I will no longer send you updates. :wave:' ;
145
+ tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
147
146
} ) ;
148
147
}
149
148
150
149
function startHandler ( message ) {
151
- let chatId = message . chat . id ;
150
+ const chatId = message . chat . id ;
152
151
153
- let markdownText = 'Hello! :smile: \n' +
154
- " I can send you the menu for the SV restaurant in Zug. \n" +
152
+ const markdownText = 'Hello! :smile: \n' +
153
+ ' I can send you the menu for the SV restaurant in Zug. \n' +
155
154
`- Try /get@${ botUsername } to receive today's menu. \n` +
156
155
`- For regular notifications use /daily@${ botUsername } or /partTime@${ botUsername } ` ;
157
156
158
- tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
157
+ tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
159
158
}
160
159
161
- function getSourceHandler ( message ) {
162
- let chatId = message . chat . id ;
160
+ function getSourceHandler ( message ) {
161
+ const chatId = message . chat . id ;
163
162
164
- let markdownText = 'This bot is written by Fabio Zuber utilizing Node.js.\n' +
163
+ const markdownText = 'This bot is written by Fabio Zuber utilizing Node.js.\n' +
165
164
'The code is open source. Feel free to check it out on [GitHub](https://github.com/Sirius-A/sv-restaurant-zug-bot).' ;
166
- tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
165
+ tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
167
166
}
168
167
169
168
/* Menu Send Functions */
170
169
function sendTodaysMenu ( chatId ) {
171
- parser . parseToday ( function ( markdownText ) {
170
+ parser . parseToday ( function ( markdownText ) {
172
171
tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
173
172
} ) ;
174
173
}
175
174
176
175
function sendWeekMenu ( chatId ) {
177
- parser . parseWeek ( function ( markdownText ) {
176
+ parser . parseWeek ( function ( markdownText ) {
178
177
tgBot . sendMessage ( chatId , markdownText , { parse_mode : 'Markdown' } ) ;
179
178
} ) ;
180
179
}
181
180
182
181
function notifySubscribers ( ) {
183
- console . log ( " notify Subscribers" ) ;
184
- subscriptions . forAllDailly ( function ( subscriber ) {
182
+ console . log ( ' notify Subscribers' ) ;
183
+ subscriptions . forAllDailly ( function ( subscriber ) {
185
184
sendTodaysMenu ( subscriber . id ) ;
186
185
} ) ;
187
186
188
- let now = new Date ( ) ;
189
- subscriptions . forAllParttime ( now . getDay ( ) , function ( subscriber ) {
187
+ const now = new Date ( ) ;
188
+ subscriptions . forAllParttime ( now . getDay ( ) , function ( subscriber ) {
190
189
sendTodaysMenu ( subscriber . id ) ;
191
190
} )
192
- ; }
191
+ ;
192
+ }
193
193
194
194
/**
195
195
* Regular Expression IndexOf for Arrays
196
196
* This little addition to the Array prototype will iterate over array
197
197
* and return the index of the first element which matches the provided
198
198
* regular expression.
199
199
* Note: This will not match on objects.
200
- * @param {RegExp } rx The regular expression to test with. E.g. /-ba/gim
200
+ * @param {RegExp } RegExp The regular expression to test with. E.g. /-ba/gim
201
201
* @return {Number } -1 means not found
202
202
*/
203
203
if ( typeof Array . prototype . regexIndexOf === 'undefined' ) {
204
- Array . prototype . regexIndexOf = function ( RegEx ) {
205
- for ( let i in this ) {
204
+ // eslint-disable-next-line no-extend-native
205
+ Array . prototype . regexIndexOf = function ( RegEx ) {
206
+ for ( const i in this ) {
206
207
if ( RegEx . match ( this [ i ] . toString ( ) ) ) {
207
208
return i ;
208
209
}
0 commit comments