Hermes Messenger #
This plugin gives the application the ability to send various types of messages (SMS, email, or anything a custom provider can reach) using external providers.
Built-in providers, registered by default:
providerId | displayName | Channel | Recipient type |
|---|---|---|---|
smtp | SMTP | email | |
sendgrid | SendGrid | email | |
twilio | Twilio | SMS | phoneNumber |
smsenvoi | SMS Envoi | SMS | phoneNumber |
The providerId is the value to pass in the providerId argument of every account action and the key of the routes. The displayName is a label for user interfaces. Both are returned by hermes:listProviders. Accounts follow the same pattern: an accountId used in arguments and routes, and an optional displayName.
Coming from version 1? Read the Migration guide.
Concepts #
Provider: an integration with an external messaging service, implemented by extending
BaseProvider<T>. A provider declares its capabilities, the recipient types it accepts and three JSON Schemas.Account: a set of credentials registered on a provider under a name. Several accounts can coexist on the same provider (e.g. one per customer). Accounts are kept in memory and synchronized across cluster nodes.
Recipient type: a named JSON Schema describing one recipient string. Recipients are always plain strings; delivery options such as
ccorbccare providerparams. Recipient types are shared between providers through a registry, seehermes:listRecipientTypes. Three are built in:Name Format Audiences emailstring, format: "email"humanphoneNumberstring, E.164 pattern humanuristring, format: "uri"technicalAudience: each recipient type lists the audiences it is meant for.
humanrecipients designate a person (email, phone number),technicalones a resource (webhook URI, broker topic, bucket).listRecipientTypes,listProvidersandlistAccountsaccept anaudienceargument (one value or an array), so an application can offer only human oriented channels when editing a user's contacts.Capabilities: what a provider can carry in a message, as an array of strings. Well-known values:
text(short plain text),html(rich content),json(structured payload),file(attachments or file transfer). Used to filter providers inlistProviderswith thecapabilityargument.JSON Schemas:
accountParamsSchema(account credentials foraddAccount),messageContentSchema(thecontentobject ofsendMessage),messageAdditionalParamsSchema(theparamsobject ofsendMessage). They are returned bylistProvidersso clients can generate forms dynamically.
API #
The plugin exposes a single hermes controller. Account and message actions take the providerId and accountId arguments and work the same way for every provider:
| Action | HTTP | Description |
|---|---|---|
listProviders | GET /_/hermes/providers[?capability=…][&audience=…] | Providers, capabilities, audiences and JSON Schemas |
listRecipientTypes | GET /_/hermes/recipient-types[?audience=…] | Registered recipient types and their audiences |
addAccount | PUT /_/hermes/providers/:providerId/accounts/:accountId | Register an account (body.params) |
removeAccount | DELETE /_/hermes/providers/:providerId/accounts/:accountId | Remove an account |
listAccounts | GET /_/hermes/accounts[?providerId=…][&audience=…][&capability=…] | Accounts of every provider (or of one), with their provider key, recipient types, audiences and capabilities |
sendMessage | POST /_/hermes/providers/:providerId/accounts/:accountId | Send a message (body.recipients, body.content, body.params) |
The sendMessage body always has the same three parts:
{
"recipients": [ /* strings, each matching one of the provider's recipient types */ ],
"content": { /* matches the provider's messageContentSchema */ },
"params": { /* optional, matches the provider's messageAdditionalParamsSchema */ }
}Per-provider examples: SMTP, Sendgrid, Twilio, SMS Envoi.
Accounts Management #
Each provider supports multiple named accounts with different credentials. The following API actions manage accounts:
hermes:addAccounthermes:removeAccounthermes:listAccounts(all providers, or one with theproviderIdargument)
Register an account at startup #
Accounts can be added programmatically once the application has started, using getProvider(providerId).addAccount(accountId, params, displayName?). params must match the provider's accountParamsSchema.
import { HermesMessengerPlugin } from "kuzzle-plugin-hermes-messenger";
const hermesMessengerPlugin = new HermesMessengerPlugin();
app.plugin.use(hermesMessengerPlugin);
app
.start()
.then(() => {
hermesMessengerPlugin.getProvider("smtp").addAccount("common", {
host_name: "smtp.example.com",
port: 587,
user: "user@example.com",
password: "secret",
default_sender: "no-reply@example.com",
});
hermesMessengerPlugin.getProvider("twilio").addAccount("common", {
account_sid: "ACxxxxxxxx",
auth_token: "secret",
default_sender: "+33600000000",
});
})
.catch(console.error);Register a custom provider #
Custom providers and custom recipient types must be registered before the application starts:
import { HermesMessengerPlugin } from "kuzzle-plugin-hermes-messenger";
import { MyCustomProvider } from "./providers/MyCustomProvider";
const hermesMessengerPlugin = new HermesMessengerPlugin();
hermesMessengerPlugin.registerProvider("my-provider", new MyCustomProvider());
app.plugin.use(hermesMessengerPlugin);See the Custom Provider guide for details on implementing your own provider and recipient types.