Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Throttle requests #25

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const client = require('twilio')(accountSid, authToken)
const ovh = require('ovh')({
appKey: process.env.OVH_APP_KEY,
appSecret: process.env.OVH_APP_SECRET_KEY,
consumerKey: process.env.OVH_CONSUMER_KEY
consumerKey: process.env.OVH_CONSUMER_KEY,
})

/**
Expand Down Expand Up @@ -48,8 +48,12 @@ const findOtherReviews = (id) => {

const placesContacted = [] // array temporaire pour stocker les lieux déjà contactés au cours du run actuel de l'algo

reviews.forEach((review) => {
if (placesContacted.length <= 100) {
const main = async () => {
for (const review of reviews) {
if (placesContacted.length > 100) {
continue
}

const googlePlaceId = review['Google Place ID'] // stockage du Google Place ID de la review traitée en cours

// on check si on est bien sur une review ou on nous a demandé de contacter le commerce + qu'il n'y a pas d'autres reviews déjà existantes + que le numéro n'a pas déjà été contacté
Expand Down Expand Up @@ -82,7 +86,7 @@ reviews.forEach((review) => {
senderForResponse: true,
noStopClause: true,
tag: 'Contact SMS LightsOff',
receivers: [phoneNumber]
receivers: [phoneNumber],
},
function (errsend, result) {
console.log(errsend, result)
Expand All @@ -91,7 +95,7 @@ reviews.forEach((review) => {
axios({
method: 'post',
url: `${process.env.API_URL}places/${googlePlaceId}/reviews`,
data: { do_it_for_me: false, type: 'SMS' }
data: { do_it_for_me: false, type: 'SMS' },
})
}
)
Expand All @@ -100,7 +104,7 @@ reviews.forEach((review) => {
.create({
url: 'https://handler.twilio.com/twiml/EHa81ec24cdcc086714c029aaada0a87ed',
to: phoneNumber,
from: phoneNumberVoiceFrom
from: phoneNumberVoiceFrom,
})
.then((call) => {
console.log(call.sid)
Expand All @@ -109,14 +113,18 @@ reviews.forEach((review) => {
axios({
method: 'post',
url: `${process.env.API_URL}places/${googlePlaceId}/reviews`,
data: { do_it_for_me: false, type: 'PHONE_CALL' }
data: { do_it_for_me: false, type: 'PHONE_CALL' },
})
})
}

// on ajoute la Google Place dans le tableau des places déjà contactées au cours du run
placesContacted.push(googlePlaceId)

await new Promise((resolve) => setTimeout(resolve, 500))
}
}
}
})
}

main()