This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from jimmygchen/add-telegram-support
added telegram integration
- Loading branch information
Showing
8 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './sms-notifier.js'; | ||
export * from './telegram-notifier.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {logger} from '../logger.js'; | ||
import {TelegramBotApiClient} from '../telegram-bot-api-client.js'; | ||
|
||
class TelegramNotifier { | ||
constructor(config) { | ||
this.chatID = config.chatID; | ||
this.client = new TelegramBotApiClient(config.botURL, config.botToken); | ||
} | ||
|
||
notify(message) { | ||
logger.info(`Sending telegram message to chat_id[${this.chatID}] via bot: ${message}`); | ||
this.client.sendMessage(this.chatID, message) | ||
.then(r => logger.info("Message sent to " + this.chatID)) | ||
.catch(err => logger.error('Error sending a message', err)); | ||
} | ||
|
||
} | ||
|
||
export {TelegramNotifier}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import axios from 'axios'; | ||
import {logger} from './logger.js'; | ||
|
||
class TelegramBotApiClient { | ||
constructor(botAPI, botToken) { | ||
this.botAPI = botAPI; | ||
this.botToken = botToken; | ||
this.http = axios.create({ | ||
baseURL: this.botAPI + this.botToken | ||
}) | ||
} | ||
|
||
async sendMessage(chatID, message) { | ||
const body = { | ||
chat_id: chatID, | ||
text: message | ||
} | ||
|
||
try { | ||
await this.http.post('/sendMessage', body); | ||
} catch (err) { | ||
logger.error(`Request to sendMessage failed with error`, err); | ||
throw err | ||
} | ||
} | ||
|
||
} | ||
|
||
export {TelegramBotApiClient}; |