2020-03-22 22:09:33 +00:00
|
|
|
'use strict';
|
|
|
|
const TelegramBot = require('node-telegram-bot-api');
|
2020-03-25 22:40:18 +00:00
|
|
|
import { IMessageConfig } from '../config/config';
|
|
|
|
import InvalidBotOrChatConfig from '../errors/invalid_bot_chat_config.error';
|
|
|
|
import InvalidArgumentsError from '../errors/invalid_arguments.error';
|
2020-03-22 22:09:33 +00:00
|
|
|
export default class SendMessage {
|
2020-03-25 22:40:18 +00:00
|
|
|
static async send(config: IMessageConfig, msg: string) {
|
|
|
|
validate(config, msg);
|
2020-03-22 22:09:33 +00:00
|
|
|
const bot = new TelegramBot(config.bot_token);
|
2020-03-25 22:40:18 +00:00
|
|
|
try {
|
|
|
|
await bot.sendMessage(config.chat_id, `${msg}`, { parse_mode: 'Markdown' });
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
throw new InvalidBotOrChatConfig();
|
|
|
|
}
|
2020-03-22 22:09:33 +00:00
|
|
|
}
|
2020-03-25 22:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validate(config: IMessageConfig, msg: string) {
|
|
|
|
if (!config.bot_token || !config.chat_id) throw new InvalidArgumentsError(`Config object must have bot_token<string>, chat_id<string>`);
|
|
|
|
if (typeof msg !== 'string') throw new InvalidArgumentsError(`message must be of type string`)
|
2020-03-22 22:09:33 +00:00
|
|
|
}
|