-
Notifications
You must be signed in to change notification settings - Fork 1
/
pullIncomings.js
67 lines (63 loc) · 1.51 KB
/
pullIncomings.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
66
67
require('dotenv').config()
const ovh = require('ovh')({
appKey: process.env.OVH_APP_KEY,
appSecret: process.env.OVH_APP_SECRET_KEY,
consumerKey: process.env.OVH_CONSUMER_KEY
})
const axios = require('axios')
/**
* Function to fetch incomings sms on OVH service
* @returns err | data
*/
const tempData = async () => {
const dataPromised = await new Promise((resolve, reject) => {
ovh.request(
'GET',
`/sms/${process.env.OVH_SERVICE_NAME}/incoming`,
(err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
}
)
})
return dataPromised
}
tempData()
.then((messages) => {
messages.forEach((messageID) => {
ovh.request(
'GET',
`/sms/${process.env.OVH_SERVICE_NAME}/incoming/${messageID}`,
(err, msgText) => {
if (err) {
console.log(err)
} else {
axios({
method: 'post',
url: process.env.SLACK_WEBHOOK,
headers: { 'Content-type': 'application/json' },
data: { text: msgText.message }
})
}
}
)
ovh.request(
'DELETE',
`/sms/${process.env.OVH_SERVICE_NAME}/incoming/${messageID}`,
(err, res) => {
if (err) {
console.log(err)
} else {
console.log(`Réponse supprimée : ${messageID}`)
console.log(res)
}
}
)
})
})
.catch((err) => {
console.error(err)
})