87 lines
2.7 KiB
JavaScript
87 lines
2.7 KiB
JavaScript
// メッセージ送信
|
|
async function sendLineworksMessage(botId, userId, messageText) {
|
|
const accessToken = await getAccessToken();
|
|
if (!accessToken) {
|
|
console.log('アクセストークンの取得に失敗しました');
|
|
throw new Error('アクセストークンの取得に失敗しました');
|
|
}
|
|
|
|
// userIdに@が含まれない場合はchannelIdとして扱う
|
|
let apiUrl;
|
|
if (userId.includes('@')) {
|
|
apiUrl = `https://www.worksapis.com/v1.0/bots/${botId}/users/${userId}/messages`;
|
|
} else {
|
|
apiUrl = `https://www.worksapis.com/v1.0/bots/${botId}/channels/${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('メッセージ送信成功');
|
|
return { success: true, message: '送信成功' };
|
|
} else {
|
|
const errorText = await response.text();
|
|
console.log('メッセージ送信失敗:', errorText);
|
|
return { success: false, message: `送信失敗: ${errorText}` };
|
|
}
|
|
}
|
|
|
|
// アクセストークン取得
|
|
async function getAccessToken() {
|
|
const clientId = 'V58hNobcAqRcQPerySMz';
|
|
const clientSecret = 'gJ84XQtigD';
|
|
const serviceAccount = 'x94xh.serviceaccount@works-nextgroup2';
|
|
const privateKey = fs.readFileSync('./modules/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();
|
|
console.log('アクセストークン取得成功');
|
|
return data.access_token;
|
|
} else {
|
|
const errorText = await response.text();
|
|
console.log('アクセストークン取得失敗:', errorText);
|
|
return null;
|
|
}
|
|
}
|
|
|