-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traq.js
73 lines (63 loc) · 2.55 KB
/
traq.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
68
69
70
71
72
73
import TraQAdapter from "./src/adapter.js"
const NAME_ENV_NAME = "HUBOT_TRAQ_NAME"
const MODE_ENV_NAME = "HUBOT_TRAQ_MODE"
const VERIFY_TOKEN_ENV_NAME = "HUBOT_TRAQ_VERIFY_TOKEN"
const ACCESS_TOKEN_ENV_NAME = "HUBOT_TRAQ_ACCESS_TOKEN"
const PATH_ENV_NAME = "HUBOT_TRAQ_PATH"
const EMBED_ENV_NAME = "HUBOT_TRAQ_EMBED"
const DOMAIN_ENV_NAME = "HUBOT_TRAQ_DOMAIN"
const getEnvs = () => {
const name = process.env[NAME_ENV_NAME]
if (typeof name === "undefined") {
console.warn(`環境変数${NAME_ENV_NAME}が存在しません。意図した動作が得られない可能性があります。`)
}
console.info(`環境変数${NAME_ENV_NAME}の存在を確認(${name})`)
let mode = process.env[MODE_ENV_NAME]
if (typeof mode === "undefined") {
console.warn(`環境変数${MODE_ENV_NAME}が存在しません。'HTTP'が利用されます`)
mode = "HTTP"
} else {
console.info(`環境変数${MODE_ENV_NAME}の存在を確認`)
}
mode = mode.toUpperCase()
if (!['HTTP', 'WEBSOCKET'].includes(mode)) {
throw new Error(`環境変数${MODE_ENV_NAME}の値には'HTTP'または'WebSocket'のみ利用可能です`)
}
const accessToken = process.env[ACCESS_TOKEN_ENV_NAME]
if (typeof accessToken === "undefined") {
throw new Error(`環境変数${ACCESS_TOKEN_ENV_NAME}が存在しません`)
}
console.info(`環境変数${ACCESS_TOKEN_ENV_NAME}の存在を確認`)
const embed = process.env[EMBED_ENV_NAME] && process.env[EMBED_ENV_NAME].toUpperCase() === "TRUE"
console.info(`メンション・チャンネルリンクの自動埋め込みは${embed ? "有効" : "無効"}`)
let domain = process.env[DOMAIN_ENV_NAME]
if (typeof domain === "undefined") {
domain = 'q.trap.jp'
} else {
console.info(`環境変数${DOMAIN_ENV_NAME}の存在を確認`)
}
const verifyToken = process.env[VERIFY_TOKEN_ENV_NAME]
let path = process.env[PATH_ENV_NAME]
if (mode === 'HTTP') {
if (typeof verifyToken === "undefined") {
throw new Error(`環境変数${VERIFY_TOKEN_ENV_NAME}が存在しません`)
}
console.info(`環境変数${VERIFY_TOKEN_ENV_NAME}の存在を確認`)
if (typeof path === "undefined") {
console.warn(`環境変数${PATH_ENV_NAME}が存在しません。'/'が利用されます`)
path = "/"
} else {
console.info(`環境変数${PATH_ENV_NAME}の存在を確認`)
}
}
return { mode, accessToken, embed, verifyToken, path, domain }
}
export default {
use(robot) {
try {
return new TraQAdapter(robot, getEnvs())
} catch (e) {
console.error(e)
}
}
}