-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
95 lines (76 loc) · 3.02 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
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
/**
* index.js is used to setup the whole Microsoft Teams extension application
* It defines the available routes for bot messages, API calls, notifications and static content
*/
// Import required packages
import path from 'path'
import express from 'express'
import cors from 'cors'
import { fileURLToPath } from 'url';
// Import environment variables and routers
import './loadEnv.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url));
//const PORT = process.env.PORT || 4001;
const PORT = process.env.PORT || 3333;
// Create HTTP server
const server = express();
server.use(cors());
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
server.use(express.static(path.resolve(__dirname, './client/build')))
// Load bot framwork adapter and bot activity handler
import adapter from './server/bots/botAdapter.js'
import botActivityHandler from './server/bots/botActivityHandler.js'
import { MessageFactory,TeamsInfo } from 'botbuilder'
import * as adaptiveCards from './server/models/adaptiveCard.js'
import s4HANAClient from './server/services/S4HANAClient.js';
// Import filters (passport strategies) to authenticate requests
import {teamsFilter} from './server/filters/teamsFilter.js'
import {xsuaaFilter} from './server/filters/xsuaaFilter.js'
// bot endpoint which is handling bot interaction
server.post('/api/messages', async (req, res) => {
console.log(req.body)
await adapter.process(req, res, (context) => botActivityHandler.run(context))
})
server.post('/em/pr-workflow', async(req, res) => {
if(req.body && Object.keys(req.body).length != 0){
const prId = req.body.NUMBER;
const wfId = req.body.WI_ID;
const MAIL_ID = req.body.MAIL_ID;
const data = await s4HANAClient.getPRDetailsUsingCloudSdk(prId)
try{
for (const conversationReference of Object.values(botActivityHandler.conversationReferences)) {
await adapter.continueConversation(conversationReference, async turnContext => {
const userEmail = await getSingleMember(turnContext)
console.log("email Id :"+userEmail.email)
if(userEmail.email === MAIL_ID){
const payload = data.d;
console.log("workflow Id :"+wfId)
payload.wfId = wfId;
const card = adaptiveCards.PurchaseRequisitionCard(payload)
await turnContext.sendActivity(MessageFactory.attachment(card));
}
});
}
}catch(err) {
console.log(err);
}
}
res.status(200).send();
})
async function getSingleMember(context) {
try {
const member = await TeamsInfo.getMember(
context,
context.activity.from.id
);
return member;
} catch (e) {
if (e.code === 'MemberNotFoundInConversation') {
return context.sendActivity(MessageFactory.text('Member not found.'));
} else {
throw e;
}
}
}
server.listen(PORT, () => { console.log(`Server listening on http://localhost:${ PORT }`)});