-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
65 lines (60 loc) · 1.44 KB
/
index.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
const feedr = require("feedr").create({});
const feeds = {
lemonde: "http://www.lemonde.fr/rss/une.xml"
};
const ITEMS_COUNT = 3;
const SILENCE_DURATION = 1;
// todo: handle hangup
function callback(
{
playSilence,
playText,
playSine,
hangupButton$
}
) {
return new Promise((resolve, reject) => {
let CONTINUE = true;
const hangupListener = hangupButton$.addListener({
next: value => {
console.log("value", value)
if (value === "hangup") {
CONTINUE = false;
}
}
});
feedr.readFeeds(feeds, {}, function(err, result) {
if (err) {
console.error(err);
reject(err);
return;
}
let promise = Promise.resolve();
const news = result.lemonde.rss.channel[0];
for (var i = 0; i < ITEMS_COUNT; i++) {
let text = news.item[i].title[0];
promise = promise
.then(() => {
console.log("text", text);
if (!CONTINUE) {
throw 'HANGUP'
} else {
return playText(text, { sine: false });
}
})
.then(() => playSilence(SILENCE_DURATION));
}
promise
.then(() => {
console.log("unsubscribe");
hangupButton$.removeListener(hangupListener);
playSine();
resolve();
})
.catch(e => {
console.error(e);
});
});
});
}
module.exports = callback;