ken_nogi/pleasanter/develop2/LINEWORKStoPLEASANTER/sample/sendMessage_sample.js
Kenichiro NOGI 88a402ce0f up
2026-07-10 18:13:30 +09:00

56 lines
1.5 KiB
JavaScript

const https = require('https');
const LINEWORKS_API_URL = 'https://www.worksapis.com/v1.0/bots/{botId}/messages';
const BOT_ID = '10131520';
const BOT_SECRET = 'TehiDYmOqdySbgZKGfSRTp7qGB40/x';
const USER_ID = 'kenichiro.nogi@works-nextgroup2';
async function sendMessage(message) {
const payload = JSON.stringify({
botId: BOT_ID,
accountId: USER_ID,
content: {
type: 'text',
text: message
}
});
// userIdに@が含まれない場合はchannelIdとして扱う
let apiUrl;
if (USER_ID.includes('@')) {
apiUrl = `https://www.worksapis.com/v1.0/bots/${BOT_ID}/users/${USER_ID}/messages`;
} else {
apiUrl = `https://www.worksapis.com/v1.0/bots/${BOT_ID}/channels/${USER_ID}/messages`;
}
const body = {
'content': {
'type': 'text',
'text': messageText
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve({
status: res.statusCode,
body: data
});
});
});
req.on('error', reject);
req.write(payload);
req.end();
});
}
// 使用例
sendMessage('Hello from LINEWORKS Bot!')
.then(result => console.log('Success:', result))
.catch(error => console.error('Error:', error));