sender-js
Simple Node.js module for sending messages by e-mail, Slack, Telegram or HTTP API.
Installation
npm install sender-js
Usage overview
This module provides e-mail sending functionality via Nodemailer or Mailgun modules, supports sending messages to Slack team collaboration tool as well as HTTP API is supported too.
The messages can be sent separately or by all four services simultaneously.
Common Nodemailer usage with single service instance:
var sender = ; var settings = gmail: "username": "your.mail@gmail.com" "password": "yourPassword" ; sender; var messageOptions = to: 'recipient.mail@somemail.com' from: 'your.mail@mail.mail' subject: 'fff' text: 'test nodemailer text'; sender;
Common Mailgun usage with single service instance:
var sender = ; var settings = mailgun: "apiKey": "key-somekey" "domain": "mg.yourdomain" ; sender; var messageOptions = to: 'recipient.mail@somemail.com' from: 'your.mail@mail.mail' subject: 'fff' text: 'test mailgun test'; sender;
Common Slack usage with single service instance:
var sender = ; var settings = slack: "token": "sasd-asdasdasdasdasdasda" ; sender; var messageOptions = to: 'common' text: 'test slack text'; sender;
Common HTTP usage with single service instance:
var sender = ; var settings = http: "url": "http://some.url" "method": "POST" "json": true "headers": "Content-type": "your content type" "Custom-header": "some header" "queryString": "id": 113 ; sender; var messageOptions = text: 'test slack text'; sender;
Common Telegram usage with single service instance:
var sender = ; var settings = telegram: "token": "43432424234:asdasdasdasdasdasda" "chatId": "-25434543" // Can be ommited ; sender; var messageOptions = text: 'test slack text'; sender;
To send messages to Telegram sender-js needs respective chat ID. Chat ID can be received automatically by sending
/start
command to the library. To do that you need follow the next few steps:
- Add BotFather (@BotFather) to your chat
- Create new bot with
/newbot
command - Add generated bot token to Telegram sender-js settings
- Add generated bot to your chat
- Start your application with sender-js lib
- Send
/start
command from Telegram to newly created bot
Also there is an option to add chat ID manually, if you already have it. There is one way to get it for the group chat:
- Open Telegram Web in your browser
- Do the necessary authorization
- Create new chat group and add your bot to it
- In browser address bar there is chat ID which looks like
g144782871
- Add it to sender-js settings as
chatId
field replacing "g" with "-":-144782871
Now sender-js can send messages to the group without /start
command. Also you can transmit group chat ID to sender-js
with the first technique described above.
To send multiple messages you should specify options to all services you going to use:
var sender = ; var settings = gmail: "username": "your.mail@gmail.com" "password": "yourPassword" mailgun: "apiKey": "key-somekey" "domain": "mg.yourdomain" slack: "token": "sasd-asdasdasdasdasdasda" http: "url": "http://jsonplaceholder.typicode.com/posts" "method": "POST" "json": true "headers": "Content-type": "your content type" "Custom-header": "some header" "queryString": "id": 11 ; // If we don't use the second argument, multiple service objects will be createdsender; var messageOptions = to: 'recipient.mail@somemail.com' from: 'your.mail@mail.mail' subject: 'fff' text: 'test mailgun test' slack: // Slack destination name should be specified separately to: 'common' http: // HTTP request URL is specified separately, if needed url: 'http://some.url' ; sender;
Tests
To run the test suite you must provide e-mail and Mailgun credetials. E-mail credentials is passed to Nodemailer module and Mailgun requires api key and domain setup to run.
All test settings are stored in ./test/settings.json
file. Before running test update it with your credentials.
Then install the dev dependencies and execute the test suite:
$ npm install
$ npm test
The tests will call all module functions and will check the sending functionality.
License
Copyright 2015 Invatechs
Licensed under the MIT License.