web-paging/routes/api/sling.js

59 lines
1.8 KiB
JavaScript

// fetch(`https://api.getsling.com/v1/conversations/${sling_chat_id}/messages`, {
// method: 'POST',
// headers: {
// 'Authorization': `${global.slingToken}`,
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// 'content': sling_chat_message
// })
// }).then(res => res.json()).then(data => {
// if (data && data.success) {
// console.log('Sling chat message sent successfully.');
// } else {
// console.error('Error sending Sling chat message:', data);
// }
// });
const express = require("express");
const router = express.Router();
const path = require("path");
const fs = require("fs");
router.post("/message", global.apiAuth, async (req, res) => {
const { channel, content } = req.body;
if (!channel || !content) {
return res.status(400).json({ error: "Missing channel or content" });
}
if (!global.slingToken) {
return res.status(500).json({ error: "Sling token not configured" });
}
try {
const replacedContent = typeof content === 'string'
? content.replace(/%initiatingUser%/g, req.body.initiatingUser || '')
: content;
const response = await fetch(`https://api.getsling.com/v1/conversations/${channel}/messages`, {
method: 'POST',
headers: {
'Authorization': `${global.slingToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
'content': replacedContent
})
});
const data = await response.json();
if (data && data.success) {
return res.json({ success: true, message: "Message sent successfully" });
} else {
return res.status(500).json({ error: "Error sending message", details: data });
}
} catch (error) {
console.error('Error sending Sling chat message:', error);
return res.status(500).json({ error: "Internal server error" });
}
});
module.exports = router;