-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
310 lines (289 loc) · 11.7 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'use strict'
const { Telegraf } = require('telegraf')
//const Composer = require('telegraf/composer')
const Markup = require('telegraf/markup')
const session = require('telegraf/session')
const commandParts = require('telegraf-command-parts')
const Stage = require('telegraf/stage')
const Scene = require('telegraf/scenes/base')
const WizardScene = require('telegraf/scenes/wizard')
//const { leave } = Stage
const userInfo = require("./middlewares/user-info")
const api = require('sg-node-api')
const greeter = new Scene('greeter')
greeter.enter((ctx) => {
ctx.reply('Main menu', Markup.keyboard(['/commit', '/contract', '/newgoal', '/mygoals', '/deletegoal']).resize().extra())
})
/**
* Commit scene
*/
const commit = new WizardScene('commit',
async (ctx) => {
if (ctx.state.skip) {
ctx.wizard.next()
return ctx.wizard.steps[ctx.wizard.cursor](ctx)
}
let buttons = [ ctx.session.currentgoal.get('contract').get('duration') + 'm' ]
if (ctx.state.command.splitArgs[1] && ctx.state.command.splitArgs[1] !== buttons[0] &&
ctx.session.commit.validateDuration(ctx.state.command.splitArgs[1])) {
buttons.unshift(ctx.state.command.splitArgs[1])
}
ctx.reply('What is the duration of your current commit in minutes or hours (1h)? Specify or just press the button',
Markup.keyboard(buttons.concat('/cancel')).resize().extra())
return ctx.wizard.next()
},
(ctx) => {
if (!ctx.state.skip && ctx.session.commit.validateDuration(ctx.message.text)) {
ctx.session.commit.set({ duration: ctx.message.text })
}
ctx.reply('What was done?',
Markup.keyboard(['/skip','/cancel']).resize().extra())
if (ctx.session.currentgoal.get('contract').get('whats_next')) {
ctx.reply('You can choose from your What is next? plans:\n' + ctx.session.currentgoal.get('contract').get('whats_next'))
}
return ctx.wizard.next()
},
(ctx) => {
if (ctx.message.text && ctx.message.text !== '/skip' ) {
ctx.session.commit.set({ whats_done: ctx.message.text })
}
ctx.reply('What\'s next?',
Markup.keyboard(['/skip','/cancel']).resize().extra())
return ctx.wizard.next()
},
(ctx) => {
if (ctx.message.text && ctx.message.text !== '/skip') {
ctx.session.commit.set({ whats_next: ctx.message.text })
}
ctx.reply('Your commit:\n' +
`Goal: ${ctx.session.currentgoal.get('owner')}/${ctx.session.currentgoal.get('key')}\n` + // replace with contract's key attr
`Duration: ${ctx.session.commit.get('duration')}m\n` +
(ctx.session.commit.get('whats_done') !== null ? `What was done: ${ctx.session.commit.get('whats_done')}\n` : '') +
(ctx.session.commit.get('whats_next') !== null ? `What is next: ${ctx.session.commit.get('whats_next')}\n` : '') +
'\nFinish?',
Markup.keyboard(['/finish','/cancel']).resize().extra()) // consider button with full text of the command instead /finish
return ctx.wizard.next()
},
async (ctx) => {
if (ctx.message.text !== '/finish') {
ctx.reply('Ok... Leaving!')
return ctx.scene.enter('greeter')
}
ctx.session.commit.save(ctx)
ctx.reply('Commit is committed :)')
return ctx.scene.enter('greeter')
}
)
/**
* Contract scene
*/
const contract = new WizardScene('contract',
async (ctx) => {
if (!(await findGoalFromArg(ctx))) return ctx.scene.enter('greeter')
ctx.session.contract = new api.contract()
},
(ctx) => {
if (ctx.message.text && ctx.message.text !== '/skip' ) {
ctx.session.commitattr.whats_next = ctx.message.text
}
ctx.reply('Your new contract:\n' +
`Goal: ${ctx.session.currentgoal.get('owner').username}/${ctx.session.currentgoal.get('code')}\n` + // replace with universtal id method
`Duration: ${ctx.session.commitattr.duration}\n` +
ctx.session.commitattr.whats_done ? `What was done: ${ctx.session.commitattr.whats_done}\n` : '' +
ctx.session.commitattr.whats_next ? `What is next: ${ctx.session.commitattr.whats_next}\n` : '' +
'\nFinish?',
Markup.keyboard(['/finish','/cancel']).resize().extra()) // consider button with full text of the command instead /finish
return ctx.wizard.next()
},
async (ctx) => {
if (ctx.message.text !== '/finish') {
ctx.reply('Ok... Leaving!')
return ctx.scene.enter('greeter')
}
ctx.session.contract.save(ctx)
ctx.reply('Congrats! New contract was signed!')
return ctx.scene.enter('greeter')
}
)
/**
* New goal scene
*/
const newGoal = new WizardScene('newgoal',
(ctx) => {
ctx.reply('Please give a name for your Shared Goal.\nEnglish, meaningful title is preferred, but you are free to choose.',
Markup.keyboard(['/cancel']).resize().extra())
return ctx.wizard.next()
},
(ctx) => {
ctx.session.currentgoal = new api.goal
ctx.session.currentgoal.set({ title: ctx.message.text }) // Use regexp for Text without specials from the start
ctx.reply('Ok. Please enter the short key for your Goal or use default with the button.',
Markup.keyboard([ctx.message.text.toLowerCase().split(' ').join('_')].concat('/cancel')).resize().extra()) // Transliterate
return ctx.wizard.next()
},
(ctx) => {
if (!/^[a-zA-Z0-9-_]+$/.test(ctx.message.text)) { // Use regexp for Key without / and specials
ctx.reply('Please use short string for key without spaces and special letters')
return
}
ctx.session.currentgoal.set({ key: ctx.message.text })
ctx.replyWithMarkdown('Please specify the Contract for your Goal.\nThe most common choice is `1h every day`.\n' +
'Other examples are `30m every monday, thursday` or `2h every 10, 25`', // remove every
Markup.keyboard(['1h every day', '30m every monday, thursday', '2h every 10, 25'].concat('/cancel')).resize().extra())
return ctx.wizard.next()
},
async (ctx) => {
const contract = ctx.session.currentgoal.get('contract')
const contractData = await contract.validateFormat(ctx, ctx.message.text)
if (!contractData) {
ctx.reply('Wrong format for contract')
return
}
contract.set(contractData)
const saved = await ctx.session.currentgoal.save(ctx)
contract.set({goal: saved.get('id')}) // Ambiguous line
contract.save(ctx)
const goalUrl = `${ctx.session.user.get('username')}/${saved.get('key')}` // Replace with Contract's Key
ctx.reply('You goal is created! You can share it with others\n' +
'For editing your goals use /mygoals\nYou already can forward this command to your friends')
ctx.reply(`/contract ${goalUrl} ${contract.toString()}`, Markup.keyboard([]).extra()) // Remove keyboard?
return ctx.scene.enter('greeter')
}
)
/**
* Delete goal scene
*/
const deleteGoal = new WizardScene('deletegoal',
async (ctx) => {
if (!(await findGoalFromArg(ctx))) return ctx.scene.enter('greeter')
ctx.replyWithMarkdown('Please send `I\'m sure!` to delete.', Markup.keyboard(['/cancel']).resize().extra())
return ctx.wizard.next()
},
(ctx) => {
if (ctx.message.text !== 'I\'m sure!') {
ctx.reply('Text isn\'t identical. Returning to main menu.')
return ctx.scene.enter('greeter')
}
ctx.session.currentgoal.set({archived: true}) // replace with Delete goal
ctx.session.currentgoal.save(ctx)
ctx.reply('Goal is gone...')
return ctx.scene.enter('greeter')
}
)
const stage = new Stage()
stage.register(greeter, contract, newGoal, commit, deleteGoal)
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.use(session())
bot.use(stage.middleware())
bot.use(commandParts())
bot.use(async (ctx, next) => {
if (!ctx.session.user) {
await userInfo.getUserInfo(ctx)
}
if (!ctx.scene || !ctx.scene.current) {
ctx.scene.enter('greeter')
}
return next()
})
// Find a way to use one
contract.command('/cancel', (ctx) => {
ctx.scene.enter('greeter')
})
newGoal.command('/cancel', (ctx) => {
ctx.scene.enter('greeter')
})
commit.command('/cancel', (ctx) => {
ctx.scene.enter('greeter')
})
deleteGoal.command('/cancel', (ctx) => {
ctx.scene.enter('greeter')
})
/**
* /commit sg 90min WhatDone WhatsNext
*/
bot.command('/commit', async (ctx) => {
if (!ctx.state.command.splitArgs[0]) {
const contracts = await api.contract().findByUser(ctx)
const buttons = contracts.filter((c) => (c.get('today') === true || c.get('overdue') === true)).map((c) => { // - overdue, active and sort duration desc
return `/commit ${c.get('goal').key} ${c.get('duration')}m "Was done"` // remove Was Done
})
ctx.reply('Select a contract to commit:',
Markup.keyboard(buttons.concat('/cancel')).resize().extra())
} else {
ctx.session.commit = new api.commit
if (!(await findGoalFromArg(ctx))) return // replace with contract.find
ctx.session.commit.set({ contract: ctx.session.currentgoal.get('contract') })
// const re = new RegExp(/((?<owner>[^/\s]+)\/)?(?<key>[^\s]+)\s+((?<hours>\d+)\s*(h|hr)\s+)?((?<minutes>\d+)\s*(m|min)\s+)?$/)
const sub_matches = ctx.message.text.match(ctx.session.commit.re) // replace with ctx.session.commit.re without mandatory whats_done
if (!sub_matches || !sub_matches.groups) return
ctx.session.commit.set({
duration: sub_matches.groups.minutes ? sub_matches.groups.minutes : sub_matches.groups.hours * 60,
whats_done: 'Was done',
whats_next: 'Some next'
})
ctx.scene.enter('commit')
ctx.state.skip = true
}
})
/**
* /contract Adidas/RunEveryDay 45min everyday
* /contract bongiozzo/sg 1h everyday
*/
bot.command('/contract', async (ctx) => {
ctx.scene.enter('contract')
if (ctx.state.command.splitArgs[0]) {
if (!(await findGoalFromArg(ctx))) return ctx.scene.leave()
ctx.wizard.next()
}
})
/**
* /newgoal RunEveryDay 1h everyday
* /newgoal MoveToHouse 3h every saturday,sunday
*/
bot.command('/newgoal', async (ctx) => {
if (ctx.state.command.splitArgs[0]) {
// from command line
} else {
ctx.scene.enter('newgoal')
}
})
/**
* /mygoals - List my goals
* Return Goal menu
*/
bot.command('/mygoals', async (ctx) => {
})
/**
* /deletegoal - Delete goal
* Return Goal menu
*/
bot.command('/deletegoal', async (ctx) => {
if (!ctx.state.command.splitArgs[0]) {
const goals = await api.goal().findAll(ctx) // Error
const buttons = goals.map((c) => {
return `/deletegoal ${c.get('key')}`
})
ctx.reply('Choose a goal to delete:',
Markup.keyboard(buttons.concat('/cancel')).resize().extra())
} else {
ctx.scene.enter('deletegoal')
}
})
bot.launch()
// Move the logic of this function to sg-node-api
function getIdFromArg (ctx) {
let id = ctx.state.command.splitArgs[0]
const re = new RegExp('^(?<owner>[^/\\s]+)/(?<key>.+)$')
const sub_matches = id.match(re)
if (!sub_matches || !sub_matches.groups) {
id = ctx.session.user.get('username') + '/' + id
}
return id
}
async function findGoalFromArg (ctx) {
ctx.session.currentgoal = await api.goal().find(ctx, getIdFromArg(ctx))
if (!ctx.session.currentgoal) {
ctx.reply('Goal was not found')
}
return ctx.session.currentgoal
}