This repository was archived by the owner on Mar 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-skills.js
56 lines (47 loc) · 1.56 KB
/
create-skills.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
const _ = require('lodash')
const fs = require('fs').promises
const config = require('config')
const axios = require('axios')
const m2mAuth = require('tc-core-library-js').auth.m2m
const ubahnM2MConfig = _.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL'])
const ubahnM2M = m2mAuth({ ...ubahnM2MConfig, AUTH0_AUDIENCE: ubahnM2MConfig.AUTH0_AUDIENCE })
const url = `https://${config.DOMAIN}/v5/skills`
const skillProviderId = config.SKILLPROVIDERID
async function getUbahnM2Mtoken() {
return ubahnM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function createSkill() {
try {
const fails = new Array()
const skillsFile = await fs.readFile(config.FILE_SKILLS)
const skills = skillsFile.toString().split('\n')
console.log(`loading ${skills.length} skills to ${skillProviderId}`)
const token = await getUbahnM2Mtoken()
for (let i = 0; i < skills.length; i++) {
const name = skills[i]
console.log(`${i}: ${name}`)
try {
await axios.post(url, {
skillProviderId,
name
}, {
headers: {
Authorization: `Bearer ${token}`
}
})
} catch (error) {
console.log(`Error for skill: '${name}'`)
console.log(error)
console.log(error.message)
fails[fails.length] = { postion: i, name}
}
await sleep(config.SLEEP_LENGTH)
}
} catch (e) {
console.log(e)
}
}
createSkill()