Skip to content

Commit 61ca3a4

Browse files
committed
feat: ability to set default collection for adding links via telegram
1 parent d6594da commit 61ca3a4

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

app/controllers/telegram.js

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const CMD_START = 'CMD_START',
2626
CMD_ADD_LINK = 'CMD_ADD_LINK',
2727
CMD_CHOOSE_COLLECTION = 'CMD_CHOOSE_COLLECTION',
2828
CMD_UPLOAD_PHOTO = 'CMD_UPLOAD_PHOTO',
29+
CMD_SET_DEFAULT = 'CMD_SET_DEFAULT',
30+
CMD_CLEAR_DEFAULT = 'CMD_CLEAR_DEFAULT',
2931
CMD_HELP = 'CMD_HELP'
3032

3133
const commandMatchers = {
@@ -58,6 +60,14 @@ const commandMatchers = {
5860
const args = m.text.match(/^(\d+)$/)
5961
return args ? args.slice(1) : null
6062
},
63+
[CMD_SET_DEFAULT]: function (m) {
64+
const args = m.text.match(/^\/setdefault\s(\d+)$/)
65+
return args ? args.slice(1) : null
66+
},
67+
[CMD_CLEAR_DEFAULT]: function (m) {
68+
const args = m.text.match(/^\/cleardefault$/)
69+
return args ? args.slice(1) : null
70+
},
6171
[CMD_UPLOAD_PHOTO]: function (m) {
6272
return m.photo ? [] : null
6373
},
@@ -130,20 +140,24 @@ const commandProcessors = {
130140
date: Date.now()
131141
}
132142

133-
fetchCollections(user)
134-
.then(({ data }) => {
135-
// this is not robust to the collections changing while the user does their choice
136-
let text = '👇 Choose a collection to save the link to by typing its number:\n\n'
137-
text += data
138-
.map((c, i) => {
139-
return `**${i + 1}.** ${c.name}`
140-
})
141-
.join('\n')
142-
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: text, parse_mode: 'Markdown' })
143-
})
144-
.catch(() => {
145-
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to add link, sorry.' })
146-
})
143+
if (user.telegramDefaultCollId) {
144+
commandProcessors[CMD_CHOOSE_COLLECTION]([user.telegramDefaultCollId], rawMessage)
145+
} else {
146+
fetchCollections(user)
147+
.then(({ data }) => {
148+
// this is not robust to the collections changing while the user does their choice
149+
let text = '👇 Choose a collection to save the link to by typing its number:\n\n'
150+
text += data
151+
.map((c, i) => {
152+
return `**${i + 1}.** ${c.name}`
153+
})
154+
.join('\n')
155+
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: text, parse_mode: 'Markdown' })
156+
})
157+
.catch(() => {
158+
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to add link, sorry.' })
159+
})
160+
}
147161
})
148162
.catch(unauthenticatedHandler(rawMessage))
149163
},
@@ -174,6 +188,26 @@ const commandProcessors = {
174188
})
175189
.catch(unauthenticatedHandler(rawMessage))
176190
},
191+
[CMD_SET_DEFAULT]: function (args, rawMessage) {
192+
return tgutils.resolveUser(rawMessage.from)
193+
.then((user) => {
194+
User.updateOne({ _id: user._id }, { telegramDefaultCollId: parseInt(args[0]) })
195+
.then(() => {
196+
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '✅ Set default collection.' })
197+
})
198+
})
199+
.catch(unauthenticatedHandler(rawMessage))
200+
},
201+
[CMD_CLEAR_DEFAULT]: function (args, rawMessage) {
202+
return tgutils.resolveUser(rawMessage.from)
203+
.then((user) => {
204+
User.updateOne({ _id: user._id }, { $unset: { telegramDefaultCollId: '' } })
205+
.then(() => {
206+
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '✅ Unset default collection.' })
207+
})
208+
})
209+
.catch(unauthenticatedHandler(rawMessage))
210+
},
177211
[CMD_UPLOAD_PHOTO]: function (args, rawMessage) {
178212
return tgutils.resolveUser(rawMessage.from)
179213
.then((user) => {

app/models/user.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const userSchema = mongoose.Schema({
2323
name : String
2424
},
2525
verificationToken : String,
26-
telegramUserId : String
26+
telegramUserId : String,
27+
telegramDefaultCollId : Number,
2728
}, {
2829
usePushEach: true
2930
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "anchr-multi-webservice",
3-
"version": "3.1.1",
3+
"version": "3.2.0",
44
"description": "⚓️ Anchr provides you with a toolbox for tiny tasks on the internet, especially bookmark collections",
55
"private": true,
66
"scripts": {

telegram/commands.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/setcommands
22
start - Connect bot to your Anchr account
33
add - Add a new link to a collections
4+
setdefault - Set default collectio for adding new links
5+
cleardefault - Unset default collection (ask every time)
46
shorten - Shorten a link
57
logout - Unlink your Anchr account from bot
68
whoami - Check your user account

0 commit comments

Comments
 (0)