mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: relace tokens with contextVars
This commit is contained in:
parent
982200cc3a
commit
5d19889a7a
@ -65,6 +65,7 @@ import { LabelModel } from '../schemas/label.schema';
|
|||||||
import { FileType } from '../schemas/types/attachment';
|
import { FileType } from '../schemas/types/attachment';
|
||||||
import { Context } from '../schemas/types/context';
|
import { Context } from '../schemas/types/context';
|
||||||
import { PayloadType, StdOutgoingListMessage } from '../schemas/types/message';
|
import { PayloadType, StdOutgoingListMessage } from '../schemas/types/message';
|
||||||
|
import { SubscriberContext } from '../schemas/types/subscriberContext';
|
||||||
|
|
||||||
describe('BlockService', () => {
|
describe('BlockService', () => {
|
||||||
let blockRepository: BlockRepository;
|
let blockRepository: BlockRepository;
|
||||||
@ -421,6 +422,7 @@ describe('BlockService', () => {
|
|||||||
...contextBlankInstance,
|
...contextBlankInstance,
|
||||||
skip: { [blockProductListMock.id]: 0 },
|
skip: { [blockProductListMock.id]: 0 },
|
||||||
},
|
},
|
||||||
|
{ vars: {} }, //TODO: to correct
|
||||||
false,
|
false,
|
||||||
'conv_id',
|
'conv_id',
|
||||||
);
|
);
|
||||||
@ -454,6 +456,7 @@ describe('BlockService', () => {
|
|||||||
...contextBlankInstance,
|
...contextBlankInstance,
|
||||||
skip: { [blockProductListMock.id]: 2 },
|
skip: { [blockProductListMock.id]: 2 },
|
||||||
},
|
},
|
||||||
|
{ vars: {} }, //TODO: to correct
|
||||||
false,
|
false,
|
||||||
'conv_id',
|
'conv_id',
|
||||||
);
|
);
|
||||||
@ -498,9 +501,19 @@ describe('BlockService', () => {
|
|||||||
skip: { '1': 0 },
|
skip: { '1': 0 },
|
||||||
attempt: 0,
|
attempt: 0,
|
||||||
};
|
};
|
||||||
|
const subscriberContext: SubscriberContext = {
|
||||||
|
vars: {
|
||||||
|
phone: '123456789',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
it('should process empty text', () => {
|
it('should process empty text', () => {
|
||||||
const result = blockService.processText('', context, settings);
|
const result = blockService.processText(
|
||||||
|
'',
|
||||||
|
context,
|
||||||
|
subscriberContext,
|
||||||
|
settings,
|
||||||
|
);
|
||||||
expect(result).toEqual('');
|
expect(result).toEqual('');
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -509,6 +522,7 @@ describe('BlockService', () => {
|
|||||||
const result = blockService.processText(
|
const result = blockService.processText(
|
||||||
translation.en,
|
translation.en,
|
||||||
context,
|
context,
|
||||||
|
subscriberContext,
|
||||||
settings,
|
settings,
|
||||||
);
|
);
|
||||||
expect(result).toEqual(translation.fr);
|
expect(result).toEqual(translation.fr);
|
||||||
@ -518,6 +532,7 @@ describe('BlockService', () => {
|
|||||||
const result = blockService.processText(
|
const result = blockService.processText(
|
||||||
'{context.user.first_name} {context.user.last_name}, email : {context.vars.email}',
|
'{context.user.first_name} {context.user.last_name}, email : {context.vars.email}',
|
||||||
contextEmailVarInstance,
|
contextEmailVarInstance,
|
||||||
|
subscriberContext,
|
||||||
settings,
|
settings,
|
||||||
);
|
);
|
||||||
expect(result).toEqual('John Doe, email : email@example.com');
|
expect(result).toEqual('John Doe, email : email@example.com');
|
||||||
@ -525,17 +540,19 @@ describe('BlockService', () => {
|
|||||||
|
|
||||||
it('should process text replacements with context vars', () => {
|
it('should process text replacements with context vars', () => {
|
||||||
const result = blockService.processText(
|
const result = blockService.processText(
|
||||||
'{context.user.first_name} {context.user.last_name}, email : {context.vars.email}',
|
'{context.user.first_name} {context.user.last_name}, phone : {context.vars.phone}',
|
||||||
contextEmailVarInstance,
|
contextEmailVarInstance,
|
||||||
|
subscriberContext,
|
||||||
settings,
|
settings,
|
||||||
);
|
);
|
||||||
expect(result).toEqual('John Doe, email : email@example.com');
|
expect(result).toEqual('John Doe, phone : 123456789');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should process text replacements with settings contact infos', () => {
|
it('should process text replacements with settings contact infos', () => {
|
||||||
const result = blockService.processText(
|
const result = blockService.processText(
|
||||||
'Trying the settings : the name of company is <<{contact.company_name}>>',
|
'Trying the settings : the name of company is <<{contact.company_name}>>',
|
||||||
contextBlankInstance,
|
contextBlankInstance,
|
||||||
|
subscriberContext,
|
||||||
settings,
|
settings,
|
||||||
);
|
);
|
||||||
expect(result).toEqual(
|
expect(result).toEqual(
|
||||||
|
@ -33,6 +33,7 @@ import {
|
|||||||
} from '../schemas/types/message';
|
} from '../schemas/types/message';
|
||||||
import { NlpPattern, Pattern, PayloadPattern } from '../schemas/types/pattern';
|
import { NlpPattern, Pattern, PayloadPattern } from '../schemas/types/pattern';
|
||||||
import { Payload, StdQuickReply } from '../schemas/types/quick-reply';
|
import { Payload, StdQuickReply } from '../schemas/types/quick-reply';
|
||||||
|
import { SubscriberContext } from '../schemas/types/subscriberContext';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
||||||
@ -301,22 +302,19 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
processTokenReplacements(
|
processTokenReplacements(
|
||||||
text: string,
|
text: string,
|
||||||
context: Context,
|
context: Context,
|
||||||
|
subscriberContext: SubscriberContext,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
): string {
|
): string {
|
||||||
|
const vars = { ...subscriberContext.vars, ...context.vars };
|
||||||
// Replace context tokens with their values
|
// Replace context tokens with their values
|
||||||
Object.keys(context.vars || {}).forEach((key) => {
|
Object.keys(vars).forEach((key) => {
|
||||||
if (
|
if (typeof vars[key] === 'string' && vars[key].indexOf(':') !== -1) {
|
||||||
typeof context.vars[key] === 'string' &&
|
const tmp = vars[key].split(':');
|
||||||
context.vars[key].indexOf(':') !== -1
|
vars[key] = tmp[1];
|
||||||
) {
|
|
||||||
const tmp = context.vars[key].split(':');
|
|
||||||
context.vars[key] = tmp[1];
|
|
||||||
}
|
}
|
||||||
text = text.replace(
|
text = text.replace(
|
||||||
'{context.vars.' + key + '}',
|
'{context.vars.' + key + '}',
|
||||||
typeof context.vars[key] === 'string'
|
typeof vars[key] === 'string' ? vars[key] : JSON.stringify(vars[key]),
|
||||||
? context.vars[key]
|
|
||||||
: JSON.stringify(context.vars[key]),
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -368,7 +366,12 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
*
|
*
|
||||||
* @returns The text message translated and tokens being replaces with values
|
* @returns The text message translated and tokens being replaces with values
|
||||||
*/
|
*/
|
||||||
processText(text: string, context: Context, settings: Settings): string {
|
processText(
|
||||||
|
text: string,
|
||||||
|
context: Context,
|
||||||
|
subscriberContext: SubscriberContext,
|
||||||
|
settings: Settings,
|
||||||
|
): string {
|
||||||
const lang =
|
const lang =
|
||||||
context && context.user && context.user.language
|
context && context.user && context.user.language
|
||||||
? context.user.language
|
? context.user.language
|
||||||
@ -376,7 +379,12 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
// Translate
|
// Translate
|
||||||
text = this.i18n.t(text, { lang, defaultValue: text });
|
text = this.i18n.t(text, { lang, defaultValue: text });
|
||||||
// Replace context tokens
|
// Replace context tokens
|
||||||
text = this.processTokenReplacements(text, context, settings);
|
text = this.processTokenReplacements(
|
||||||
|
text,
|
||||||
|
context,
|
||||||
|
subscriberContext,
|
||||||
|
settings,
|
||||||
|
);
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,6 +431,7 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
async processMessage(
|
async processMessage(
|
||||||
block: Block | BlockFull,
|
block: Block | BlockFull,
|
||||||
context: Context,
|
context: Context,
|
||||||
|
subscriberContext: SubscriberContext,
|
||||||
fallback = false,
|
fallback = false,
|
||||||
conversationId?: string,
|
conversationId?: string,
|
||||||
): Promise<StdOutgoingEnvelope> {
|
): Promise<StdOutgoingEnvelope> {
|
||||||
@ -440,6 +449,7 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
const text = this.processText(
|
const text = this.processText(
|
||||||
this.getRandom(blockMessage),
|
this.getRandom(blockMessage),
|
||||||
context,
|
context,
|
||||||
|
subscriberContext,
|
||||||
settings,
|
settings,
|
||||||
);
|
);
|
||||||
const envelope: StdOutgoingEnvelope = {
|
const envelope: StdOutgoingEnvelope = {
|
||||||
@ -456,12 +466,22 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
const envelope: StdOutgoingEnvelope = {
|
const envelope: StdOutgoingEnvelope = {
|
||||||
format: OutgoingMessageFormat.quickReplies,
|
format: OutgoingMessageFormat.quickReplies,
|
||||||
message: {
|
message: {
|
||||||
text: this.processText(blockMessage.text, context, settings),
|
text: this.processText(
|
||||||
|
blockMessage.text,
|
||||||
|
context,
|
||||||
|
subscriberContext,
|
||||||
|
settings,
|
||||||
|
),
|
||||||
quickReplies: blockMessage.quickReplies.map((qr: StdQuickReply) => {
|
quickReplies: blockMessage.quickReplies.map((qr: StdQuickReply) => {
|
||||||
return qr.title
|
return qr.title
|
||||||
? {
|
? {
|
||||||
...qr,
|
...qr,
|
||||||
title: this.processText(qr.title, context, settings),
|
title: this.processText(
|
||||||
|
qr.title,
|
||||||
|
context,
|
||||||
|
subscriberContext,
|
||||||
|
settings,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
: qr;
|
: qr;
|
||||||
}),
|
}),
|
||||||
@ -476,12 +496,22 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
|
|||||||
const envelope: StdOutgoingEnvelope = {
|
const envelope: StdOutgoingEnvelope = {
|
||||||
format: OutgoingMessageFormat.buttons,
|
format: OutgoingMessageFormat.buttons,
|
||||||
message: {
|
message: {
|
||||||
text: this.processText(blockMessage.text, context, settings),
|
text: this.processText(
|
||||||
|
blockMessage.text,
|
||||||
|
context,
|
||||||
|
subscriberContext,
|
||||||
|
settings,
|
||||||
|
),
|
||||||
buttons: blockMessage.buttons.map((btn) => {
|
buttons: blockMessage.buttons.map((btn) => {
|
||||||
return btn.title
|
return btn.title
|
||||||
? {
|
? {
|
||||||
...btn,
|
...btn,
|
||||||
title: this.processText(btn.title, context, settings),
|
title: this.processText(
|
||||||
|
btn.title,
|
||||||
|
context,
|
||||||
|
subscriberContext,
|
||||||
|
settings,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
: btn;
|
: btn;
|
||||||
}),
|
}),
|
||||||
|
@ -74,6 +74,7 @@ export class BotService {
|
|||||||
await this.blockService.processMessage(
|
await this.blockService.processMessage(
|
||||||
block,
|
block,
|
||||||
context,
|
context,
|
||||||
|
event.getSender().context,
|
||||||
fallback,
|
fallback,
|
||||||
conservationId,
|
conservationId,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user