slack app test
unknown
javascript
5 months ago
2.7 kB
133
No Index
// app.ts
import express from 'express';
import bolt from 'bolt';
import app from './app';
import axios from 'axios';
const app = express();
const boltConfig = {
server: process.env.PORT || 5000,
};
// Initialize with Bolt's built-in auth system
app.use(bolt({
server: boltConfig.server,
auth: {
username: 'your_bolt_username', // Replace with your Bolt username
password: 'your_bolt_password' // Replace with your Bolt password
}
}));
// OAuth 2.0 Authentication Flow
const getAuthToken = (accessToken, scope = 'public_profile') => {
const provider = require('oauth2-providers-google').createProvider();
const client = require('oauth2-credentials-api').createClient();
const tokenUrl = createResponseTokenRequest(provider, client, 'https://www.googleapis.com/auth/oauth2.0 & scope=' + scope).promise();
const tokenRequest = createTokenRequest(provider, client, tokenUrl).promise();
const responseTokenRequest = createResponseTokenRequest(provider, client, tokenUrl).promise();
const { accessToken, responseToken } = await tokenRequest.match(responseTokenRequest);
return { accessToken, responseToken };
};
// Root endpoint for initial login and command handling
app.get('/', (req, res) => {
let accessToken;
// Check if token is in session
if (req.session?.slackToken) {
res.send('Logged in! Send a message with /slack');
} else {
// Try to get token from OAuth flow
if (req.query.accessToken) {
const { accessToken, responseToken } = getAuthToken(req.query.accessToken);
req.session.slackToken = accessToken;
res.send('Logged in with token: ' + accessToken);
} else {
res.status(401).send('Access token required for login');
}
}
});
// /auth endpoint for OAuth2 flow
app.post('/auth', (req, res) => {
const { accessToken, scope } = req.body;
const accessToken = accessToken;
const scope = scope || 'public_profile';
const { accessToken, responseToken } = getAuthToken(accessToken, scope);
req.session.slackToken = accessToken;
res.send(`Login successful! Token: ${accessToken}`);
});
// /slack command endpoint
app.get('/slack', (req, res) => {
const { accessToken } = req.body;
if (accessToken) {
axios.post(
'https://api.slack.com/v1.5/talk/',
{
token: accessToken,
message: 'Hello from Slack integration! Use /slack to send messages'
}
)
.then(res => {
res.json({ status: 'success' });
})
} else {
res.status(401).send('No token provided');
}
});
// Start the server
app.listen(boltConfig.server, () => {
console.log(`Server running on port ${boltConfig.server}`);
});
Editor is loading...
Leave a Comment