-
Notifications
You must be signed in to change notification settings - Fork 6
/
telegram.js
executable file
·53 lines (47 loc) · 1.41 KB
/
telegram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @module telegram_notification/telegram
*/
var WEBHOOK_URL = "https://api.telegram.org/bot";
var http = require('@jetbrains/youtrack-scripting-api/http');
/**
* @typedef {Object} Telegram
*
* @classdesc Main class that is used to connect workflow to telegram.
*
* @property {string} [botToken] Telegram BOT-Token
*
* @example
* var telegramClient = new telegram.Telegram('MyBotToken');
* telegramClient.send(123456789, 'Test text');
*/
/**
* Creates an object that lets you send notify to telegram
* @param {string} [botToken] Telegram BOT-Token
* @constructor Telegram
*
* @see sendMessage
*/
var Telegram = function(botToken) {
this.botToken = botToken;
};
/**
* Send message to Telegram
* @param {int|string} [chatId] - Reciever ID in Telegram
* @param {string} [text] Text to send.
* @returns {boolean} If sended - return true, else - return false
*/
Telegram.prototype.sendMessage = function(chatId, text) {
var payload = {
"chat_id": chatId,
"text": text,
"parse_mode": "Markdown",
};
var connection = new http.Connection(WEBHOOK_URL + this.botToken + "/sendMessage", null, 2000);
connection.addHeader("Content-Type", "application/json");
var response = connection.postSync("", [], JSON.stringify(payload));
if (!response.isSuccess) {
console.warn('Failed to post notification to Telegram. Details: ' + response.toString());
}
return this;
};
exports.Telegram = Telegram;