65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
|
|
// Webex Bot DM Helper
|
||
|
|
// Fire-and-forget pattern — logs success/failure but never throws.
|
||
|
|
// Used to notify users when their feedback issues are resolved.
|
||
|
|
|
||
|
|
const https = require('https');
|
||
|
|
|
||
|
|
const WEBEX_API_URL = 'https://webexapis.com/v1/messages';
|
||
|
|
const WEBEX_BOT_TOKEN = process.env.WEBEX_BOT_TOKEN || '';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a direct message to a user via Webex Teams bot.
|
||
|
|
* @param {string} email - Recipient's email address
|
||
|
|
* @param {string} markdownMessage - Message body (Webex markdown supported)
|
||
|
|
*/
|
||
|
|
function sendDirectMessage(email, markdownMessage) {
|
||
|
|
if (!WEBEX_BOT_TOKEN) {
|
||
|
|
console.warn('[WebexBot] WEBEX_BOT_TOKEN not configured — skipping DM');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!email || !markdownMessage) {
|
||
|
|
console.warn('[WebexBot] Missing email or message — skipping DM');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const postData = JSON.stringify({
|
||
|
|
toPersonEmail: email,
|
||
|
|
markdown: markdownMessage,
|
||
|
|
});
|
||
|
|
|
||
|
|
const parsed = new URL(WEBEX_API_URL);
|
||
|
|
|
||
|
|
const reqOpts = {
|
||
|
|
method: 'POST',
|
||
|
|
hostname: parsed.hostname,
|
||
|
|
path: parsed.pathname,
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Authorization': `Bearer ${WEBEX_BOT_TOKEN}`,
|
||
|
|
'Content-Length': Buffer.byteLength(postData),
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
const req = https.request(reqOpts, (res) => {
|
||
|
|
let data = '';
|
||
|
|
res.on('data', chunk => { data += chunk; });
|
||
|
|
res.on('end', () => {
|
||
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||
|
|
console.log(`[WebexBot] DM sent to ${email} (${res.statusCode})`);
|
||
|
|
} else {
|
||
|
|
console.error(`[WebexBot] Failed to DM ${email} — ${res.statusCode}: ${data}`);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
req.on('error', (err) => {
|
||
|
|
console.error(`[WebexBot] Request error sending DM to ${email}:`, err.message);
|
||
|
|
});
|
||
|
|
|
||
|
|
req.write(postData);
|
||
|
|
req.end();
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = { sendDirectMessage };
|