forked from OfficeDev/microsoft-teams-sample-complete-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
115 lines (99 loc) · 4.29 KB
/
app.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { Request, Response } from "express";
let express = require("express");
let favicon = require("serve-favicon");
let http = require("http");
let path = require("path");
import * as config from "config";
import { SOEBot } from "./SOEBot";
import { VSTSTokenOAuth2API } from "./apis/VSTSTokenOAuth2API";
import * as teams from "botbuilder-teams";
import { DefaultTab } from "./endpoints/DefaultTab";
import { AllCommandsTab } from "./endpoints/AllCommandsTab";
import { TestRunNotificationJob } from "./endpoints/TestRunNotificationJob";
import { RunNotificationJob } from "./endpoints/RunNotificationJob";
import { MongoDbBotStorage } from "./storage/MongoDbBotStorage";
import { MongoDbBotChannelStorage } from "./storage/MongoDbBotChannelStorage";
import { AADUserValidation } from "./apis/AADUserValidation";
import { MongoDbTagStorage } from "./storage/MongoDbTagStorage";
import { MongoDbSOEQuestionStorage } from "./storage/MongoDbSOEQuestionStorage";
import { MongoDbConfigStorage } from "./storage/MongoDbConfigStorage";
// Configure instrumentation - tooling with Azure
// let appInsights = require("applicationinsights");
// let instrumentationKey = config.get("app.instrumentationKey");
// if (instrumentationKey) {
// appInsights.setup(instrumentationKey).start();
// }
let app = express();
app.set("port", process.env.PORT || 3978);
app.use(express.static(path.join(__dirname, "../../public")));
app.use(express.static(path.join(__dirname, "./public"))); // used for static dialogs
app.use(favicon(path.join(__dirname, "../../public/assets", "favicon.ico")));
app.get("/tabDisplay", DefaultTab.buildTab());
app.get("/allCommands", AllCommandsTab.buildTab());
// Create Teams connector for the bot
let connector = new teams.TeamsChatConnector({
appId: config.get("bot.botId"),
appPassword: config.get("bot.botPassword"),
});
// Create storage for the bot
let channelStorage = null;
let tagStorage = null;
let soeQuestionStorage = null;
let configStorage = null;
let botStorage = null;
if (config.get("channelStorageType") === "mongoDb") {
botStorage = new MongoDbBotStorage(config.get("mongoDb.botStateCollection"), config.get("mongoDb.connectionString"));
channelStorage = new MongoDbBotChannelStorage(config.get("mongoDb.botStateCollection"), config.get("mongoDb.connectionString"));
tagStorage = MongoDbTagStorage.createConnection();
soeQuestionStorage = MongoDbSOEQuestionStorage.createConnection();
configStorage = MongoDbConfigStorage.createConnection();
};
let botSettings = {
storage: botStorage,
channelStorage: channelStorage,
tagStorage: tagStorage,
soeQuestionStorage: soeQuestionStorage,
configStorage: configStorage,
};
let bot = new SOEBot(connector, botSettings);
// Configure bot routes
app.post("/api/messages", connector.listen());
app.get("/api/VSTSOauthCallback", VSTSTokenOAuth2API.setUserAccessToken(bot));
app.get("/api/validateUser", AADUserValidation.validateUser(bot));
app.get("/api/success", AADUserValidation.success(bot));
// test endpoint to send request to with query params to test notification job
app.get("/testRunNotificationJob", TestRunNotificationJob.getRequestHandler(bot));
// main endpoint to start notification job
app.post("/runNotificationJob", RunNotificationJob.getRequestHandler(bot));
// catch 404 and forward to error handler
app.use((req: Request, res: Response, next: Function) => {
let err: any = new Error("Not Found");
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
app.use(function(err: any, req: Request, res: Response, next: Function): void {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: err,
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err: any, req: Request, res: Response, next: Function): void {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: {},
});
});
http.createServer(app).listen(app.get("port"), function (): void {
console.log("Express server listening on port " + app.get("port"));
console.log("Bot running at endpoint + /api/messages. E.g. localhost:" + app.get("port") + "/api/messages");
});
module.exports = app;