-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLexClient.ts
57 lines (46 loc) · 1.89 KB
/
LexClient.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
import LexRuntime from 'aws-sdk/clients/lexruntime';
import get from 'lodash.get';
import { BotClient } from './BotClient';
export default class LexClient extends BotClient {
private botName: string;
private botAlias: string;
private userId: string;
private lastResponse: any;
private sessionAttributes: any;
private props: any;
private lex: LexRuntime;
constructor(botContext: any, userContext: any) {
super(botContext, userContext);
this.botName = this.botContext.botName;
this.botAlias = this.botContext.botAlias;
this.userId = `${this.userContext.userId}-${Date.now()}`;
this.lastResponse = null;
this.sessionAttributes = this.userContext.userAttributes;
this.props = {
region: this.botContext.region,
};
// Optional Auth Environment Variables
this.props.accessKeyId = process.env.chatpickle_access_id || undefined;
this.props.secretAccessKey = process.env.chatpickle_access_secret || undefined;
this.lex = new LexRuntime(this.props);
console.log(`[${this.userId}] New Conversation with ${this.botName}`);
}
public async speak(inputText: string): Promise<string> {
console.log(`[${this.userId}] User: ${inputText}`);
const params = {
botName: this.botName,
botAlias: this.botAlias,
userId: this.userId,
inputText,
sessionAttributes: this.sessionAttributes,
};
this.lastResponse = await this.lex.postText(params).promise();
this.sessionAttributes = this.lastResponse.sessionAttributes;
const reply: string = this.lastResponse.message.trim();
console.log(`[${this.userId}] Bot: ${reply}`);
return reply;
}
public async fetch(attributePath: string): Promise<string> {
return await get(this.lastResponse, attributePath);
}
}