From 679e9b489e0d37e9cc99933f640e2d51d422e355 Mon Sep 17 00:00:00 2001 From: Revanth <109272714+revanth1718@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:02:52 +0530 Subject: [PATCH] Slack API --- New_APIs/Slack API/index.html | 25 +++++++++++ New_APIs/Slack API/index.js | 25 +++++++++++ New_APIs/Slack API/package-lock.json | 37 ++++++++++++++++ New_APIs/Slack API/package.json | 17 ++++++++ New_APIs/Slack API/server.js | 43 +++++++++++++++++++ New_APIs/Slack API/style.css | 63 ++++++++++++++++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 New_APIs/Slack API/index.html create mode 100644 New_APIs/Slack API/index.js create mode 100644 New_APIs/Slack API/package-lock.json create mode 100644 New_APIs/Slack API/package.json create mode 100644 New_APIs/Slack API/server.js create mode 100644 New_APIs/Slack API/style.css diff --git a/New_APIs/Slack API/index.html b/New_APIs/Slack API/index.html new file mode 100644 index 0000000..fff21ba --- /dev/null +++ b/New_APIs/Slack API/index.html @@ -0,0 +1,25 @@ + + + + + + + Slack Message Sender + + +
+

Slack Message Sender

+
+
+
+
+ + + +
+
+
+
+ + + diff --git a/New_APIs/Slack API/index.js b/New_APIs/Slack API/index.js new file mode 100644 index 0000000..cababf1 --- /dev/null +++ b/New_APIs/Slack API/index.js @@ -0,0 +1,25 @@ +document.getElementById('message-form').addEventListener('submit', async (e) => { + e.preventDefault(); + + const channel = document.getElementById('channel').value; + const message = document.getElementById('message').value; + + try { + const response = await fetch('/send-message', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ channel, message }) + }); + + const data = await response.json(); + + if (data.success) { + document.getElementById('status').textContent = 'Message sent successfully!'; + } else { + document.getElementById('status').textContent = 'Failed to send message.'; + } + } catch (error) { + console.error('Error:', error); + document.getElementById('status').textContent = 'An error occurred while sending the message.'; + } +}); diff --git a/New_APIs/Slack API/package-lock.json b/New_APIs/Slack API/package-lock.json new file mode 100644 index 0000000..3c1422f --- /dev/null +++ b/New_APIs/Slack API/package-lock.json @@ -0,0 +1,37 @@ +{ + "name": "slack-message-sender", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "express": { + "version": "4.17.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", + "integrity": "sha512-G7fYv8zS0D7ftu3gnLsOniwhgLU4k9v+1NEtFPP07/Oa8XJ51FtdUKLqIvsTcZo5ua23NO4s9Hr77BM19DOf1g==", + "requires": { + "accepts": "1.3.7", + "array-flatten": "1.1.1", + "body-parser": "1.19.0", + "content-disposition": "0.5.3", + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "1.1.2", + "encodeurl": "1.0.2", + "escape-html": "1.0.3", + "etag": "1.8.1", + "finalhandler": "1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "1.1.2", + "on-finished": "2.3.0", + "parseurl": "1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "2.0.6", + "qs": "6.7.0", + "range-parser": "1.2.1" + } + } + } + } + \ No newline at end of file diff --git a/New_APIs/Slack API/package.json b/New_APIs/Slack API/package.json new file mode 100644 index 0000000..cbd15bf --- /dev/null +++ b/New_APIs/Slack API/package.json @@ -0,0 +1,17 @@ +{ + "name": "slack-message-sender", + "version": "1.0.0", + "description": "A simple app to send messages to Slack channels using the Slack API", + "main": "server.js", + "scripts": { + "start": "node server.js" + }, + "author": "Your Name", + "license": "ISC", + "dependencies": { + "express": "^4.17.1", + "node-fetch": "^2.6.1", + "body-parser": "^1.19.0" + } + } + \ No newline at end of file diff --git a/New_APIs/Slack API/server.js b/New_APIs/Slack API/server.js new file mode 100644 index 0000000..ba1706c --- /dev/null +++ b/New_APIs/Slack API/server.js @@ -0,0 +1,43 @@ +const express = require('express'); +const fetch = require('node-fetch'); +const bodyParser = require('body-parser'); + +const app = express(); +app.use(express.static('public')); +app.use(bodyParser.json()); + +const SLACK_TOKEN = 'YOUR_SLACK_BOT_TOKEN'; + +app.post('/send-message', async (req, res) => { + const { channel, message } = req.body; + + try { + const response = await fetch('https://slack.com/api/chat.postMessage', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${SLACK_TOKEN}` + }, + body: JSON.stringify({ + channel: `#${channel}`, + text: message + }) + }); + + const data = await response.json(); + + if (data.ok) { + res.json({ success: true }); + } else { + console.error('Slack API error:', data.error); + res.json({ success: false }); + } + } catch (error) { + console.error('Error sending message:', error); + res.status(500).json({ success: false }); + } +}); + +app.listen(3000, () => { + console.log('Server is running on port 3000'); +}); diff --git a/New_APIs/Slack API/style.css b/New_APIs/Slack API/style.css new file mode 100644 index 0000000..7d5111e --- /dev/null +++ b/New_APIs/Slack API/style.css @@ -0,0 +1,63 @@ +body { + font-family: 'Arial', sans-serif; + background: linear-gradient(135deg, #4A154B, #36C5F0); + color: #333; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + min-height: 100vh; + margin: 0; +} + +header { + text-align: center; + margin-bottom: 20px; +} + +header h1 { + font-size: 2.5em; + color: #fff; +} + +main { + background: #fff; + padding: 20px; + border-radius: 10px; + box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); + width: 300px; +} + +form { + display: flex; + flex-direction: column; + gap: 10px; +} + +input, textarea { + padding: 10px; + font-size: 1em; + border: 1px solid #ccc; + border-radius: 5px; +} + +button { + padding: 10px; + font-size: 1em; + color: #fff; + background-color: #4A154B; + border: none; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s; +} + +button:hover { + background-color: #36C5F0; +} + +#status { + margin-top: 10px; + font-weight: bold; + color: #4A154B; +}