Skip to content

Instantly share code, notes, and snippets.

@dimitrinicolas
Last active February 6, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitrinicolas/4e8dcc6fec166ed1e67382a2f876c954 to your computer and use it in GitHub Desktop.
Save dimitrinicolas/4e8dcc6fec166ed1e67382a2f876c954 to your computer and use it in GitHub Desktop.
Telegram and SMS notifier module
import fetch from 'node-fetch';
import twilio from 'twilio';
const {
NOTIFICATION_PREFIX,
TELEGRAM_CHAT_ID,
TWILIO_ACCOUNT_SID,
TWILIO_FROM_NUMBER,
TWILIO_TO_NUMBER
} = require('../../../config');
let twilioClient = null;
try {
twilioClient = twilio(TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
} catch (error) {
console.error(error);
}
export enum NotificationService {
Telegram = 'telegram',
SMS = 'sms'
}
export enum NotificationType {
Default = 'default',
Error = 'error',
Warning = 'warning',
Success = 'success'
}
interface MessagePrefixMap {
[key: string]: string;
}
const messagePrefix: MessagePrefixMap = {};
messagePrefix[NotificationType.Default] = 'ℹ️ ';
messagePrefix[NotificationType.Error] = '❌ ';
messagePrefix[NotificationType.Warning] = '⚠️ ';
messagePrefix[NotificationType.Success] = '✅ ';
const getGlobalPrefix = (service: NotificationService): string =>
service === NotificationService.Telegram
? `\`${NOTIFICATION_PREFIX || ''}\` `
: `${NOTIFICATION_PREFIX || ''}: `;
const getPrefix = (
notificationType: NotificationType,
service: NotificationService
): string =>
`${getGlobalPrefix(service)}${messagePrefix[notificationType] || ''}`;
const prefixMessage = (
message: string,
notificationType: NotificationType,
service: NotificationService
): string => `${getPrefix(notificationType, service)}${message}`;
const sendTelegramMessage = (
message: string,
notificationType: NotificationType = NotificationType.Default
): void => {
const url = `https://api.telegram.org/bot${
process.env.TELEGRAM_BOT_TOKEN
}/sendmessage?chat_id=${TELEGRAM_CHAT_ID}&parse_mode=markdown&text=${encodeURIComponent(
prefixMessage(message, notificationType, NotificationService.Telegram)
)}`;
fetch(url)
.then(result => result.json().catch((error: Error) => console.error(error)))
.catch((error: Error) => console.error(error));
};
const sendSMSMessage = (
message: string,
notificationType: NotificationType = NotificationType.Default
): void => {
if (!twilioClient) {
return;
}
twilioClient.messages
.create({
from: TWILIO_FROM_NUMBER,
body: prefixMessage(message, notificationType, NotificationService.SMS),
to: TWILIO_TO_NUMBER
})
.catch(error => console.error(error));
};
/**
* Send notification message
* @param message Message to send
* @param notificationType Type de notification
*/
const notify = (
message: string,
notificationType: NotificationType = NotificationType.Default
): void => {
if (process.env.NODE_ENV === 'production') {
/** Send Telegram message */
sendTelegramMessage(message, notificationType);
/** Send SMS */
sendSMSMessage(message, notificationType);
}
};
export default notify;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment