-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
64 lines (52 loc) 路 2.04 KB
/
index.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
// Import Wolf dependency
import * as wolf from 'wolf-core'
// Import Wolf's Bot Builder storage layer
// import { createBotbuilderStorageLayer, createWolfStorageLayer, StorageLayerType } from 'wolf-botbuilder'
import { createBotbuilderStorageLayer, createWolfStorageLayer, StorageLayerType } from '../../src'
// Import Wolf abilities and slots
import { UserState, abilities } from './abilities'
import { slots } from './slots'
// Import NLP
import nlp from './nlp'
// Bring in Bot Builder dependency
import * as restify from 'restify'
import { BotFrameworkAdapter, MemoryStorage, ConversationState } from 'botbuilder'
// Create HTTP server with restify
const server = restify.createServer()
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log(`\n${server.name} listening to ${server.url}`)
})
// Create adapter (Bot Builder specific)
const adapter = new BotFrameworkAdapter({
appId: process.env.microsoftAppID,
appPassword: process.env.microsoftAppPassword,
})
// Settup storage layer
const memoryStorage = new MemoryStorage()
const conversationState = new ConversationState(memoryStorage)
const conversationStorageLayer = createBotbuilderStorageLayer<UserState>(conversationState)
const wolfStorageLayer = createWolfStorageLayer(conversationState)
const flow: wolf.Flow<UserState, StorageLayerType<UserState>> = {
abilities,
slots
}
// Listen for incoming requests
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Has to be a message, ignores all other activity (such as conversation update events)
if (context.activity.type !== 'message') {
return
}
// Bot logic here
const wolfResult = await wolf.run(
wolfStorageLayer(context),
conversationStorageLayer(context, { alarms: [] }),
() => nlp(context.activity.text),
() => flow,
'greeting'
)
// Respond Wolf messages
const sendActivities = wolfResult.messageStringArray.map((message) => context.sendActivity(message))
await Promise.all(sendActivities)
})
})