express/LINEWORKS_Pleasanter/LINEWORKS_sendMessage、いずれも過去の Express実装。Gitea移行に伴い保管。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
//
|
|
exports.sendLineworksMessage = async (botId, userId, messageText) => {
|
|
//アクセストークンの取得
|
|
const accessToken = await getAccessToken();
|
|
if (!accessToken) return;
|
|
|
|
const apiUrl = `https://www.worksapis.com/v1.0/bots/${botId}/users/${userId}/messages`;
|
|
|
|
const body = {
|
|
'content': {
|
|
'type': 'text',
|
|
'text': messageText
|
|
}
|
|
};
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
'Authorization': `Bearer ${accessToken}`
|
|
},
|
|
body: JSON.stringify(body)
|
|
});
|
|
|
|
if (response.ok) {
|
|
console.log('送信成功');
|
|
} else {
|
|
console.error('送信失敗', await response.text());
|
|
}
|
|
};
|
|
|
|
// アクセストークン取得
|
|
async function getAccessToken() {
|
|
const clientId = 'V58hNobcAqRcQPerySMz';
|
|
const clientSecret = 'gJ84XQtigD';
|
|
const serviceAccount = 'x94xh.serviceaccount@works-nextgroup2';
|
|
const privateKey = fs.readFileSync('./private_20250530180606.key', 'utf8');
|
|
const apiUrl = 'https://auth.worksmobile.com/oauth2/v2.0/token';
|
|
const now = Math.floor(Date.now() / 1000);
|
|
|
|
// JWT生成
|
|
const payload = {
|
|
'iss': clientId,
|
|
'sub': serviceAccount,
|
|
'iat': now,
|
|
'exp': now + 60 * 5,
|
|
'aud': apiUrl
|
|
};
|
|
const assertion = jwt.sign(payload, privateKey, { 'algorithm': 'RS256' });
|
|
|
|
const body = new URLSearchParams({
|
|
'assertion': assertion,
|
|
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
|
'client_id': clientId,
|
|
'client_secret': clientSecret,
|
|
'scope': 'bot'
|
|
});
|
|
|
|
const response = await fetch(apiUrl, {
|
|
'method': 'POST',
|
|
'headers': { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
'body': body.toString()
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
return data.access_token;
|
|
} else {
|
|
console.error('アクセストークン取得失敗:', await response.text());
|
|
return null;
|
|
}
|
|
} |