To substitute the appropriate text for `firstName`, `lastName`, and `email`, users can provide the appropriate data to the `send()` function. This data is then passed to the template and rendered automatically.
# Examples for using this library
This library can be used to easily send a variety of notifications. In this section, we'll cover how to implement some simple use cases.
## Sending a simple email
Using the send function, we can email '[email protected]' from '[email protected]' using the generic template 'generic-template-foo'. We'll also use the provider type `AirhornProviderType.SMTP` to indicate that we're sending an email:
```javascript
import { Airhorn, AirhornProviderType } from 'airhorn';
const airhorn = new Airhorn();
await airhorn.send('[email protected]', '[email protected]', 'generic-template-foo', AirhornProviderType.SMTP);
Sending a simple webhook
Here, we'll send a simple webhook to the URL 'https://httpbin.org/post':
const airhorn = new Airhorn();
airhorn.send('https://httpbin.org/post', 'foo', 'bar', AirhornProviderType.WEBHOOK);
Using multiple providers
In this example, we'll send a message using multiple email providers:
- Add in the AWS SES configuration
- Add in the Sendgrid configuration
- Send the message and it will randomly balance between the two providers.
const airhorn = new Airhorn({
AWS_SES_REGION = 'us-east-1',
TWILIO_SENDGRID_API_KEY = 'SENDGRID_API_KEY'
});
await airhorn.send('[email protected]', '[email protected]', 'generic-template-foo', AirhornProviderType.SMTP);
Supported Cloud Service Providers
This library supports sending notifications via email, SMS, and Mobile Push for the following providers:
- Email: AWS SES and Twilio Sendgrid
- SMS: AWS SMS and Twilio
- Mobile Push: AWS SNS and Google Firebase
In this section, we'll describe how to use each of these notification services.
Email providers
This library supports sending emails via AWS SES and Twilio Sendgrid.
AWS SES
After configuring your system to use AWS SES, you can easily use airhorn
to send emails. In this example, we'll email '[email protected]' from '[email protected]' using the email template 'generic-template-foo'. We'll list the provider type as AirhornProviderType.SMTP
to indicate that we're sending an email:
const airhorn = new Airhorn({
AWS_SES_REGION = 'us-east-1',
});
await airhorn.send('[email protected]', '[email protected]', 'generic-template-foo', AirhornProviderType.SMTP);
Twilio Sendgrid
To send emails via Twilio Sendgrid, first update the TWILIO_SENDGRID_API_KEY
value via AirhornOptions
. Then, we can use the same syntax as above to send an email through Twilio Sendgrid:
const airhorn = new Airhorn({
TWILIO_SENDGRID_API_KEY = 'SENDGRID_API_KEY'
});
await airhorn.send('[email protected]', '[email protected]', 'generic-template-foo', AirhornProviderType.SMTP);
SMS providers
This library supports sending SMS using AWS SMS and Twilio.
AWS SMS
Once your system is configured to use AWS SMS, you can send SMS notifications through AWS SMS. In this example, we'll send the notification to the phone number '5555555555' from the number '5552223333' with the raw text data 'Test message text'. Then, we'll list the provider type as AirhornProviderType.SMS
.
const airhorn = new Airhorn({
AWS_SMS_REGION = 'us-east-1',
});
await airhorn.send('5555555555', '5552223333', 'Test message text', AirhornProviderType.SMS);
Twilio SMS
To send SMS notifications via Twilio SMS, first update the TWILIO_SMS_ACCOUNT_SID
and the TWILIO_SMS_AUTH_TOKEN
values via the AirhornOptions
as shown below. Then, we can send an SMS notification using the same syntax as above:
const airhorn = new Airhorn({
TWILIO_SMS_ACCOUNT_SID = 'TWILIO_SMS_ACCOUNT_SID',
TWILIO_SMS_AUTH_TOKEN = 'TWILIO_SMS_AUTH_TOKEN'
});
await airhorn.send('5555555555', '5552223333', 'Test message text', AirhornProviderType.SMS);
Mobile push providers
This library supports sending Mobile Push notifications using AWS SNS and Google Firebase.
AWS SNS
To use AWS SNS you will need to create a new SNS application in the AWS console and integrate the AWS SNS SDK into your application.
- Obtain the credentials and device token for the mobile platforms that you want to support.
- Use the credentials to create a platform application object (PlatformApplicationArn) using Amazon SNS. For more information, see Creating a platform endpoint.
- Use the returned credentials to request a device token for your mobile app and device from the mobile platforms. The token you receive represents your mobile app and device.
- Use the device token and the PlatformApplicationArn to create a platform endpoint object (EndpointArn) using Amazon SNS. For more information, see Creating a platform endpoint.
Then, you can send the push message to the device endpoint using airhorn
:
const airhorn = new Airhorn({
AWS_SNS_REGION = 'us-east-1',
});
await airhorn.send('endpointArn', '', 'generic-template-foo', AirhornProviderType.MOBILE_PUSH);
Firebase for Mobile Push
To use Firebase in your application, you will need to create a new project in the Firebase console and integrate the Firebase SDK according to the Firebase documentation.
In your Firebase Project Settings, go to the Service accounts
tab to generate your private key
as a json file and put the content of the file as FIREBASE_CERT
environment variable.
Then, you can send the push message to the device endpoint using airhorn
:
const airhorn = new Airhorn({
FIREBASE_CERT = 'FIREBASE_CERT'
});
await airhorn.send('endpointArn', '', 'generic-template-foo', AirhornProviderType.MOBILE_PUSH);
How to Contribute
Now that you've set up your workspace, you're ready to contribute changes to the airhorn
repository you can refer to the CONTRIBUTING guide. If you have any questions please feel free to ask by creating an issue and label it question
.
Setting up your Development Environment
To set up your development environment, you'll need the following dependencies:
- Node.js (latest)
- Docker
- Firebase Account (this is for the firebase-cert.json file)
Set up your firebase account and generate the firebase-cert.json file. Then, place the file in the root of the project. This is needed even for testing as mocking the firebase-admin is almost impossible. After that you should run the following commands:
npm i && npm run test:services:start && npm test
This will start the services needed for testing and run the tests.
To stop the services, you can run:
npm run test:services:stop
If you are using nvm
you can run the following:
nvm use && npm i && npm run test:services:start && npm test
Licensing
This project is licensed under MIT and copyright by Jared Wray 2021-future.
Latest's Releases
What's Changed
- upgrading xo to 0.59.3 by @jaredwray in https://github.com/jaredwray/airhorn/pull/326
- upgrading wepback and typescript to latest by @jaredwray in https://github.com/jaredwray/airhorn/pull/327
- removing winston for standard logging by @jaredwray in https://github.com/jaredwray/airhorn/pull/328
- migrating to nodejs 20 by @jaredwray in https://github.com/jaredwray/airhorn/pull/329
- upgrading vitest to 2.0.5 by @jaredwray in https://github.com/jaredwray/airhorn/pull/330
- upgrading rimraf to 6.0.1 by @jaredwray in https://github.com/jaredwray/airhorn/pull/331
- upgrading firebase-admin to 12.3.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/332
- upgrading ecto to 3.0.8 by @jaredwray in https://github.com/jaredwray/airhorn/pull/333
- upgrading axios to 1.7.3 by @jaredwray in https://github.com/jaredwray/airhorn/pull/334
- upgrading docula to 0.7.2 by @jaredwray in https://github.com/jaredwray/airhorn/pull/335
- upgrading aws-sdk to 3.623.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/336
- upgrading firebase-admin to 12.3.1 by @jaredwray in https://github.com/jaredwray/airhorn/pull/337
- upgrading aws-sdk to 3.629.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/338
- Adding in airhorn store by @jaredwray in https://github.com/jaredwray/airhorn/pull/339
- updating workflows and dist locations by @jaredwray in https://github.com/jaredwray/airhorn/pull/340
- upgrading docula to 0.8.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/341
- upgrading axios to 1.7.4 by @jaredwray in https://github.com/jaredwray/airhorn/pull/342
- upgrading aws-sdk to 3.631.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/343
- moving ProviderType to AirhornProviderType by @jaredwray in https://github.com/jaredwray/airhorn/pull/344
- Airhorn store with notifications and subscriptions by @jaredwray in https://github.com/jaredwray/airhorn/pull/345
- upgrading docula to 0.9.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/346
- upgrading axios to 1.7.5 by @jaredwray in https://github.com/jaredwray/airhorn/pull/347
- upgrading firebase-admin to 12.4.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/348
- fixing logo spacing and sizing by @jaredwray in https://github.com/jaredwray/airhorn/pull/349
- airhorn store crud methods for subscription and notification by @jaredwray in https://github.com/jaredwray/airhorn/pull/350
- updating store file paths by @jaredwray in https://github.com/jaredwray/airhorn/pull/351
- breaking out files for better organization by @jaredwray in https://github.com/jaredwray/airhorn/pull/352
- Airhorn queue by @jaredwray in https://github.com/jaredwray/airhorn/pull/353
- upgrading twilio to 5.2.3 by @jaredwray in https://github.com/jaredwray/airhorn/pull/354
- upgrading ecto to 4.0.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/355
- upgrading axios to 1.7.7 by @jaredwray in https://github.com/jaredwray/airhorn/pull/356
- upgrading aws-sdk to 3.637.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/357
- fixing webhook to use local only by @jaredwray in https://github.com/jaredwray/airhorn/pull/358
- removing env and dotenv requirement by @jaredwray in https://github.com/jaredwray/airhorn/pull/359
- moving to AirhornOptions type by @jaredwray in https://github.com/jaredwray/airhorn/pull/360
- airhorn subscription functions and store by @jaredwray in https://github.com/jaredwray/airhorn/pull/361
- google pub/sub updates and fixes by @jaredwray in https://github.com/jaredwray/airhorn/pull/362
- moving to cjs and esm via tsup by @jaredwray in https://github.com/jaredwray/airhorn/pull/363
- removing google pubsub and adding nodejs 22 testing by @jaredwray in https://github.com/jaredwray/airhorn/pull/364
- upgrading vitest, typescript, and tsup to latest by @jaredwray in https://github.com/jaredwray/airhorn/pull/365
- upgrading twilio to 5.3.1 by @jaredwray in https://github.com/jaredwray/airhorn/pull/366
- upgrading mongodb to 6.9.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/367
- upgrading docula to 0.9.1 by @jaredwray in https://github.com/jaredwray/airhorn/pull/368
- upgrading ecto to 4.1.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/369
- upgrading vitest to 2.1.2 by @jaredwray in https://github.com/jaredwray/airhorn/pull/370
- Upgrading twilio to 5.3.3 by @jaredwray in https://github.com/jaredwray/airhorn/pull/371
- upgrading firebase-admin to 12.6.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/372
- upgrading aws-sdk to 3.665.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/373
Full Changelog: https://github.com/jaredwray/airhorn/compare/v3.0.4...v3.1.0
What's Changed
- upgrading typescript webpack and types to latest by @jaredwray in https://github.com/jaredwray/airhorn/pull/318
- upgrading twilio to 5.2.2 by @jaredwray in https://github.com/jaredwray/airhorn/pull/319
- upgrading rimraf to 5.0.8 by @jaredwray in https://github.com/jaredwray/airhorn/pull/320
- upgrading firebase-admin to 12.2.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/321
- upgrading ecto to 3.0.6 by @jaredwray in https://github.com/jaredwray/airhorn/pull/322
- upgrading aws sdk to 3.609.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/324
- updating github actions with workflow_dispatch by @jaredwray in https://github.com/jaredwray/airhorn/pull/325
Full Changelog: https://github.com/jaredwray/airhorn/compare/v3.0.3...v3.0.4
What's Changed
- upgrading twilio to 5.1.1 by @jaredwray in https://github.com/jaredwray/airhorn/pull/311
- removing eslint and modules by @jaredwray in https://github.com/jaredwray/airhorn/pull/312
- upgrading firebase-admin to 12.1.1 by @jaredwray in https://github.com/jaredwray/airhorn/pull/313
- upgrading ecto to 3.0.5 by @jaredwray in https://github.com/jaredwray/airhorn/pull/314
- upgrading docula to 0.5.4 by @jaredwray in https://github.com/jaredwray/airhorn/pull/315
- upgrading axios to 1.7.2 by @jaredwray in https://github.com/jaredwray/airhorn/pull/316
- upgrading aws-sdk to 3.590.0 by @jaredwray in https://github.com/jaredwray/airhorn/pull/317
Full Changelog: https://github.com/jaredwray/airhorn/compare/v3.0.2...v3.0.3