-
Notifications
You must be signed in to change notification settings - Fork 839
/
Copy pathindex.js
62 lines (50 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const path = require('path');
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const { BotFrameworkAdapter } = require('botbuilder');
const { TeamsBot } = require('./teamsBot');
const config = require("./config");
// Load environment variables from .env file
dotenv.config({ path: path.join(__dirname, '.env') });
const PORT = process.env.PORT || 3978;
const server = express();
// Middleware setup
server.use(cors());
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
// Create adapter
const adapter = new BotFrameworkAdapter({
appId: config.botId,
appPassword: config.botPassword
});
// Error handling for the adapter
adapter.onTurnError = async (context, error) => {
console.error(`\n [onTurnError] unhandled error: ${error}`);
await context.sendTraceActivity(
'OnTurnError Trace',
`${error}`,
'https://www.botframework.com/schemas/error',
'TurnError'
);
await context.sendActivity('The bot encountered an error or bug.');
await context.sendActivity('To continue to run this bot, please fix the bot source code.');
};
// Create the bot that will handle incoming messages
const bot = new TeamsBot();
// Start the server
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
// Serve static images
server.use("/Images", express.static(path.resolve(__dirname, 'Images')));
// Handle undefined routes
server.get('*', (req, res) => {
res.status(404).json({ error: 'Route not found' });
});
// Endpoint for bot messages
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
await bot.run(context);
});
});