This repository was archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
60 lines (49 loc) · 1.51 KB
/
background.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
const amqp = require('amqplib/callback_api');
const axios = require("axios");
const { sendWithDelay } = require("./send-message");
const TOKENS = JSON.parse(process.env.TOKENS);
const TURN_URL = process.env.TURN_URL;
const AMQP_URL = process.env.AMQP_URL;
let AMQP_PREFETCH_COUNT = process.env.AMQP_PREFETCH_COUNT;
if (AMQP_PREFETCH_COUNT === undefined) {
AMQP_PREFETCH_COUNT = 10
}
function sendBackgroundedMsgsWithDelay(data){
msgId = data.messageId
msgs = data.msgs
user = data.user
number = data.number
delay = data.delay
if (number === undefined) {
number = "41798931892";
}
const token = TOKENS[number];
const client = axios.create({
baseURL: TURN_URL,
timeout: 1000,
headers: { Authorization: `Bearer ${token}` }
});
return sendWithDelay(client, msgId, msgs, user, delay);
}
function channel(err, ch){
const q = 'background';
ch.assertQueue(q, { durable: true });
ch.prefetch(AMQP_PREFETCH_COUNT);
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
ch.consume(q, async function(obj){
console.log(" [x] Received %s", obj.content.toString());
let data = JSON.parse(obj.content.toString());
await sendBackgroundedMsgsWithDelay(data);
ch.ack(obj);
}, { noAck: false });
}
// Only start consumer if it was run from the command line (allows cleaner tests)
if (require.main === module) {
amqp.connect(AMQP_URL, function(err, conn) {
conn.createChannel(channel);
});
}
module.exports = {
sendBackgroundedMsgsWithDelay,
channel
};