Building Templates
Introduction
In this section, we will learn how to create NotificationTemplate
and use it to easily and beautifully send your notifications.
But first, we need to learn about a few jargons specific to this package.
Jargon | Definition |
---|---|
Notifiable | Notifiable is the receipent of the notification. |
Channel | Channels are different mechanism and transports over which the notifications can be sent. For example, sms, email are fcm are channels |
Now that you know a few stuff, let's get onto development now.
Before sending notifications, we need to first create a notification template. Let's take an example to understand this better, assume that you are building a Login with OTP
functionality, you can send SMS to the user using following approaches.
- Traditional Method - Dispatching it via Queue and calling your SMS provider via API in the Job Handler
- Notification Way - You can be smart and create a notification template, and then
Notify
the user about their OTP. 😎
Extending NotificationTemplate
You can easily create notifications by extending NotificationTemplate
and decorating your template Notification
decorator.
Creating first template
import {
Notifiable,
NotificationTemplate,
Notification,
} from '@libs/notifications';
import { MailMessage } from '@squareboat/nest-mailman';
import { Injectable } from '@nestjs/common';
@Injectable()
@Notification('sendOtp')
export class SendOtpNotificationTemplate extends NotificationTemplate {
constructor() {
super();
}
channels(): string[] {
return ['gupshup:sms'];
}
async toGupshupSms(notifiable: Notifiable): Promise<any> {
return `Hello there, ${this.payload.name}, your OTP is ${this.payload.otp}`;
}
}
In the class above, as you can see there is a channels
method which returns the names of all channels which this NotificationTemplate
is supposed to have.
channels(): string[] {
return ['gupshup:sms']; // you can add more channels here.
}
Each channel needs to have a method which will prepare and return the payload specific to the notifiable
to whom the notification is being sent.
async toGupshupSms(notifiable: Notifiable): Promise<any> {
return `Hello there, ${this.payload.name}, your OTP is ${this.payload.otp}`;
}
The channels look for a specific method in the NotificationTemplate
, for example, gupshup:sms
channel looks for toGupshupSms
method in the template. Complete list is given below.
Channel | Lookup Method |
---|---|
gupshup:sms | toGupshupSms |
toEmail |
Each lookupMethod
get injected with a notifiable
variable which contains the necessary information about the receipent of the notification.
Last step is to register the template, simply put it inside your providers
array of your respective module.
Bonus
Now let's say you want to send OTP to the user on email as well. Well, you can do that easily just by adding 'email'
to channels array and adding the lookupMethod toEmail
.
...
channels(): string[] {
return ['gupshup:sms', 'mail'];
}
async toEmail(notifiable: Notifiable): Promise<any> {
return MailMessage.init()
.greeting(`Hello ${notifiable.name} 👋`)
.line('Thank you for choosing this package to deliver your mails. ')
.line('We cannot wait you see build with this package. 🫶')
.table([
{ website: 'Squareboat', href: 'https://squareboat.com' },
{ website: 'Github', href: 'https://github.com/squareboat' },
])
.action('View Docs', 'https://squareboat.com/')
.subject('Hey there from Squareboat');
}
...