Skip to content

Commit 4889406

Browse files
committed
Refactoring
1 parent 981cfda commit 4889406

File tree

3 files changed

+61
-55
lines changed

3 files changed

+61
-55
lines changed

index.js

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ const pickCategory = {
4242
typing: true
4343
}
4444

45+
const WELCOME_SENTENCES = [
46+
"Hey, so I've heard that you need a little kick in the butt from time to time? Don't worry mate, that's my job and I'll do that for you 👏",
47+
"In exchange I only ask from you that you don't talk to me like I was human.. I'm clearly not! 🤖",
48+
"👉 Let's just stick to using buttons, that's going to be easier for the both of us"
49+
]
50+
51+
const WELCOME_TEXT_QUICK_REPLY = "That being said, choose a category right away and I'll make sure you get pumped up!"
52+
53+
const DEFAULT_ANSWERS = event => [
54+
event.user.first_name + ", I told you, I'm a bit dumb. I assume you want motivation, 'cause that's all I'm able to do :)",
55+
"I don't understand much of what you say " + event.user.first_name,
56+
"I'm only here to give you motivation",
57+
"My creators made me dumb on purpose, they say I shouldn't try to be human-like :s",
58+
"I'm not here to talk " + event.user.first_name + ", I'm here to give you motivation!"
59+
]
60+
61+
const shareTemplate = {
62+
template_type: 'generic',
63+
elements: [{
64+
title: 'Clicking this button could literally change your life',
65+
item_url: 'https://m.me/boostfuel',
66+
image_url: 'https://s27.postimg.org/dl8i0udqb/motivation_on_demand.png',
67+
buttons: [{
68+
type: 'web_url',
69+
title: '👏 Make it happen',
70+
url: 'https://m.me/boostfuel'
71+
}, { type: 'element_share' }]
72+
}]
73+
}
74+
75+
const SHARE_TEXT = "PLEASE! If you enjoy the service I am giving you, consider sharing the card below with some of your friends 👇!"
76+
4577
module.exports = function(bp) {
4678
bp.middlewares.load()
4779
subscription(bp)
@@ -55,21 +87,11 @@ module.exports = function(bp) {
5587

5688
bp.subscription.subscribe(event.user.id, 'daily')
5789

58-
const WELCOME_SENTENCES = [
59-
"Hey, so I've heard that you need a little kick in the butt from time to time? Don't worry mate, that's my job and I'll do that for you 👏",
60-
"In exchange I only ask from you that you don't talk to me like I was human.. I'm clearly not! 🤖",
61-
"👉 Let's just stick to using buttons, that's going to be easier for the both of us"
62-
]
63-
64-
const WELCOME_TEXT_QUICK_REPLY = "That being said, choose a category right away and I'll make sure you get pumped up!"
65-
66-
Promise.mapSeries(WELCOME_SENTENCES, txt => {
67-
bp.messenger.sendText(event.user.id, txt, { typing: true })
68-
return Promise.delay(2000)
69-
})
70-
.then(() => {
71-
bp.messenger.sendText(event.user.id, WELCOME_TEXT_QUICK_REPLY, pickCategory)
90+
return Promise.mapSeries(WELCOME_SENTENCES, txt => {
91+
return bp.messenger.sendText(event.user.id, txt, { typing: true, waitDelivery: true })
92+
.then(Promise.delay(250))
7293
})
94+
.then(() => bp.messenger.sendText(event.user.id, WELCOME_TEXT_QUICK_REPLY, pickCategory))
7395
})
7496

7597
bp.hear(/TRIGGER_DAILY/i, (event, next) => {
@@ -79,9 +101,7 @@ module.exports = function(bp) {
79101
const hearGetVideo = category => {
80102
bp.hear({ text: 'GET_VIDEO_' + category }, (event, next) => {
81103
const text = _.sample(TEXT_CATEGORIES[category])
82-
bp.messenger.sendText(event.user.id, text)
83-
84-
Promise.delay(1000)
104+
bp.messenger.sendText(event.user.id, text, { waitDelivery: true })
85105
.then(() => bp.sendRandomVideo(event.user.id, category))
86106
})
87107
}
@@ -90,22 +110,14 @@ module.exports = function(bp) {
90110
_.keys(TEXT_CATEGORIES).forEach(hearGetVideo)
91111

92112
bp.botDefaultResponse = event => {
93-
const ANSWERS = [
94-
event.user.first_name + ", I told you, I'm a bit dumb. I assume you want motivation, 'cause that's all I'm able to do :)",
95-
"I don't understand much of what you say " + event.user.first_name,
96-
"I'm only here to give you motivation",
97-
"My creators made me dumb on purpose, they say I shouldn't try to be human-like :s",
98-
"I'm not here to talk " + event.user.first_name + ", I'm here to give you motivation!"
99-
]
100-
101-
const text = _.sample(ANSWERS)
102-
bp.messenger.sendText(event.user.id, text, pickCategory)
113+
const text = _.sample(DEFAULT_ANSWERS(event))
114+
return bp.messenger.sendText(event.user.id, text, pickCategory)
103115
}
104116

105117
bp.sendRandomVideo = (userId, category) => {
106-
videos.getRandomVideo(category)
118+
return videos.getRandomVideo(category)
107119
.then(meta => {
108-
bp.messenger.sendTemplate(userId, {
120+
return bp.messenger.sendTemplate(userId, {
109121
template_type: 'generic',
110122
elements: [{
111123
title: meta.title,
@@ -129,36 +141,28 @@ module.exports = function(bp) {
129141
}]
130142
})
131143
})
144+
.then(() => {
145+
// 10% chance of saying this
146+
const n = _.random(0, 10)
147+
if (n === 5) {
148+
return Promise.delay(15000)
149+
.then(() => bp.sendShare(userId))
150+
}
151+
})
152+
}
132153

133-
const n = _.random(0, 10)
134-
if (n === 5) { // 10% chance of saying this
135-
setTimeout(() => {
136-
bp.messenger.sendText(userId, "PLEASE! If you enjoy the service I am giving you, consider sharing the card below with some of your friends 👇!")
137-
138-
setTimeout(() => {
139-
bp.messenger.sendTemplate(userId, {
140-
template_type: 'generic',
141-
elements: [{
142-
title: 'Clicking this button could literally change your life',
143-
item_url: 'https://m.me/boostfuel',
144-
image_url: 'https://s27.postimg.org/dl8i0udqb/motivation_on_demand.png',
145-
buttons: [{
146-
type: 'web_url',
147-
title: '👏 Make it happen',
148-
url: 'https://m.me/boostfuel'
149-
}, { type: 'element_share' }]
150-
}]
151-
})
152-
}, 2000)
153-
}, 15 * 1000)
154-
}
154+
bp.sendShare = userId => {
155+
return bp.messenger.sendText(userId, SHARE_TEXT)
156+
.then(Promise.delay(1000))
157+
.then(() => bp.messenger.sendTemplate(userId, shareTemplate))
155158
}
156159

157-
bp.sendDailyVideo = (userId) => {
160+
bp.sendDailyVideo = userId => {
158161
const category = _.sample(_.keys(TEXT_CATEGORIES))
159162
const text = "Here's your daily motivational video, have an excellent day 😁!"
160163

161164
bp.messenger.sendText(userId, text)
162-
setTimeout(() => bp.sendRandomVideo(userId, category), 1000)
165+
.then(Promise.delay(1000))
166+
.then(() => bp.sendRandomVideo(userId, category))
163167
}
164168
}

strip_tokens.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ content.accessToken = ''
99
content.appSecret = ''
1010
content.error = ''
1111
content.message = ''
12-
content.verifyToken = ''
12+
content.verifyToken = '1234567890'
13+
content.connected = false
1314

1415
fs.writeFileSync(configPath, JSON.stringify(content))

videos.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const axios = require('axios')
22
const _ = require('lodash')
33

4+
const YOUTUBE_API_KEY = process.env.YOUTUBE_API_KEY
5+
46
const videos = {
57
WORK: [
68
"DNFtCIzJQ7A",
@@ -64,7 +66,6 @@ const videos = {
6466
}
6567

6668
const getYoutubeVideoMetadata = (videoId) => {
67-
const YOUTUBE_API_KEY = process.env.YOUTUBE_API_KEY
6869
const apiUrl = `https://content.googleapis.com/youtube/v3/videos?id=${videoId}&part=snippet&key=${YOUTUBE_API_KEY}`
6970

7071
return axios.get(apiUrl)

0 commit comments

Comments
 (0)