Last active
October 10, 2022 17:15
-
-
Save ci7lus/41d9170742279c8a7ab856213326ac09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'node-fetch' | |
/** | |
* @param {NapkinRequest} req | |
* @param {NapkinResponse} res | |
*/ | |
// Fork this function to input your own slack webhook url | |
const url = process.env.SLACK_WEBHOOK_URL | |
const INDIGO_CLIENT_ID = process.env.INDIGO_CLIENT_ID | |
const INDIGO_CLIENT_SECRET = process.env.INDIGO_CLIENT_SECRET | |
if (!INDIGO_CLIENT_ID || !INDIGO_CLIENT_SECRET || !url) { | |
throw new Error("invalid creds") | |
} | |
export default async (req, res) => { | |
const tokenReq = await fetch("https://api.customer.jp/oauth/v1/accesstokens", { | |
method: "POST", | |
headers: { | |
"content-type": "application/json" | |
}, | |
body: JSON.stringify({ | |
"grantType": "client_credentials", | |
"clientId": INDIGO_CLIENT_ID, | |
"clientSecret": INDIGO_CLIENT_SECRET, | |
"code": "" | |
}) | |
}) | |
const tokenResp = await tokenReq.json() | |
if (!tokenResp.accessToken) { | |
throw new Error("no access token:", JSON.stringify(tokenResp)) | |
} | |
const instancesReq = await fetch("https://api.customer.jp/webarenaIndigo/v1/vm/getinstancelist", { | |
headers: { | |
authorization: `Bearer ${tokenResp.accessToken}` | |
} | |
}) | |
const instances = await instancesReq.json() | |
console.log(instances) | |
const instancesText = instances.map(instance => { | |
return `${instance.instance_name}(${instance.plan}, ${instance.created_at}): ${instance.status}` | |
}).join("\n") | |
await fetch(url, { | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
method: "POST", | |
body: JSON.stringify({ text: instancesText }) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment