-
Notifications
You must be signed in to change notification settings - Fork 24
/
subscription.js
53 lines (47 loc) · 1.75 KB
/
subscription.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
const axios = require('axios')
const moment = require('moment')
const manageSubscriptions = (bp, userId) => {
return bp.subscription.isSubscribed('facebook:' + userId, 'daily')
.then(subscribed => {
if (subscribed) {
bp.messenger.sendTemplate(userId, {
template_type: 'button',
text: "You are currently subscribed to receive daily motivational videos at 8AM (your time).",
buttons: [ { type: 'postback', title: 'Unsubscribe', payload: 'UNSUBSCRIBE_DAILY'} ]
})
} else {
bp.messenger.sendTemplate(userId, {
template_type: 'button',
text: "Click the button below to subscribe to daily motivational videos. You'll receive one every day at 8AM (your time)!",
buttons: [ { type: 'postback', title: 'Subscribe', payload: 'SUBSCRIBE_DAILY'} ]
})
}
})
}
const scheduleBroadcast = bp => async () => {
const tomorrow = moment().add(1, 'day').format('YYYY-MM-DD')
const token = await bp.security.login('admin', bp.botfile.login.password, '127.0.0.1')
const api = axios.create({
baseURL: 'http://localhost:' + bp.botfile.port + '/api/',
headers: {'Authorization': token && token.token}
})
return api.put('botpress-broadcast/broadcasts', {
date: tomorrow,
time: '08:00',
timezone: null, // users timezone
type: 'javascript',
content: "bp.sendDailyVideo(userId)",
filters: ["bp.subscription.isSubscribed('facebook:' + userId, 'daily')"]
})
.catch(err => {
bp.logger.error(err && err.message, err && err.stack)
bp.notifications.send({
level: 'error',
message: 'Could not schedule broadcast. See logs.'
})
})
}
module.exports = bp => {
bp.scheduleBroadcast = scheduleBroadcast(bp)
bp.manageSubscriptions = manageSubscriptions
}