@wabot-dev/framework 0.1.0-beta.4 → 0.1.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,11 +55,9 @@ class WhatsAppSender {
55
55
  if (!template) {
56
56
  throw new Error(`WhatsAppTemplate with name ${request.templateMessage.templateName} and language ${request.templateMessage.languageCode} not found`);
57
57
  }
58
+ const components = template.components.filter((x) => x.text != null).map((x) => x.text).join('\n');
58
59
  return {
59
- text: template.components
60
- .filter((x) => x.text != null)
61
- .map((x) => x.text)
62
- .join('\n'),
60
+ text: this.replaceTemplateParameters(components, request.templateMessage.parameters),
63
61
  senderName: request.senderName,
64
62
  };
65
63
  }
@@ -77,6 +75,22 @@ class WhatsAppSender {
77
75
  });
78
76
  await chatMemory.create(chatItem);
79
77
  }
78
+ replaceTemplateParameters(template, data) {
79
+ let result = template;
80
+ data.forEach(param => {
81
+ if (param.type === 'text' && param.parameter_name) {
82
+ const tag = `{{${param.parameter_name}}}`;
83
+ result = result.split(tag).join(param.text);
84
+ }
85
+ });
86
+ data.forEach((param, index) => {
87
+ if (param.type === 'text') {
88
+ const tag = `{{${index + 1}}}`;
89
+ result = result.split(tag).join(param.text);
90
+ }
91
+ });
92
+ return result;
93
+ }
80
94
  }
81
95
 
82
96
  export { WhatsAppSender };
@@ -536,6 +536,7 @@ declare function whatsapp(config: IWhatsappChannelConfig): (target: object, prop
536
536
  type IWhatsAppTemplateParameter = {
537
537
  type: 'text';
538
538
  text: string;
539
+ parameter_name?: string;
539
540
  } | {
540
541
  type: 'currency';
541
542
  currency: {
@@ -718,6 +719,7 @@ declare abstract class WhatsAppSender {
718
719
  getWhatsAppTemplate(request: IGetWhatsAppTemplateRequest): Promise<IWhatsAppTemplate | null>;
719
720
  protected resolveTemplateToChatMessage(request: ISendWhatsAppTemplateRequest): Promise<IChatMessage>;
720
721
  protected writePrivateChatMemory(message: IChatMessage, to: string): Promise<void>;
722
+ private replaceTemplateParameters;
721
723
  }
722
724
 
723
725
  declare class WhatsAppChannel implements IChatChannel {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.1.0-beta.4",
3
+ "version": "0.1.0-beta.5",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",