Skip to content

Commit

Permalink
Add support for sending messages to specific topics within supergroups
Browse files Browse the repository at this point in the history
  • Loading branch information
hootanht committed Sep 13, 2024
1 parent 43bdb84 commit b84c519
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 62 deletions.
123 changes: 63 additions & 60 deletions SendTelegramNotification/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
Expand All @@ -13,13 +13,12 @@ const Telegram = require('telegraf/telegram');
const Telegraf = require('telegraf');
const tgtools = require('./tgtools');


if (tl.getBoolInput('getChatId',false)) {
if (tl.getBoolInput('getChatId', false)) {
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
console.log("You now can get chat IDs from your chats: \n - Use /chat command to get the ID \n - Then /stop to finish the task \n\n *If something goes wrong - clear chat history*");
const bot = new Telegraf(tl.getInput('botToken', true),{
const bot = new Telegraf(tl.getInput('botToken', true), {
telegram: tgtools.getProxyCfg()
});
bot.command('/chat', (ctx) => ctx.reply(ctx.chat));
Expand All @@ -34,62 +33,66 @@ if (tl.getBoolInput('getChatId',false)) {
run();
}
else {
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
var token = tl.getInput('botToken',true);
var chats = tl.getDelimitedInput('chats',',',false);
var body = "";

if (tl.getBoolInput("taskStatus",false)) {
switch (tl.getVariable("Agent.JobStatus")) {
case "Succeeded":
body += "\n<b>✅ Task Succeeded</b>"
break;
case "Failed":
body += "\n<b>⛔️ Task Failed</b>"
break;
case "SucceededWithIssues":
body += "\n<b>⚠️ Task has some Issues</b>"
break;
default:
break;
}
}
if (tl.getBoolInput('buildNumber', false)) {
body += "\n<b>#Build-" + tl.getVariable("Build.BuildNumber")+"</b>";
} else if (tl.getBoolInput('releaseName', false)) {
body += "\n<b>#" + tl.getVariable("Release.ReleaseName")+"</b>";
}
if (tl.getInput('message', false) !== null) {
var message = tl.getInput('message', false);
body += "\n" + message;
}
if (tl.getBoolInput('buildQueuedBy', false)) {
var buildQueuedBy = tl.getVariable("Build.QueuedBy");
body += "\n<b>Build queued by:</b> " + buildQueuedBy;
}
if (tl.getBoolInput('releaseRequestedFor', false)) {
var releaseRequestedFor = tl.getVariable("Release.RequestedFor");
body += "\n<b>Release queued by:</b> " + releaseRequestedFor;
}
if (tl.getBoolInput('teamUrl', false)) {
var teamLink = tl.getVariable("System.TeamFoundationCollectionUri");
var project = tl.getVariable("System.TeamProject");
body += "\n<b>Project URL: </b>" + tgtools.tglinkbuilder(teamLink+project, project);
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
var token = tl.getInput('botToken', true);
var chats = tl.getDelimitedInput('chats', ',', false);
var messageThreadId = tl.getInput('messageThreadId', false);
var body = "";

if (tl.getBoolInput("taskStatus", false)) {
switch (tl.getVariable("Agent.JobStatus")) {
case "Succeeded":
body += "\n<b>✅ Task Succeeded</b>"
break;
case "Failed":
body += "\n<b>⛔️ Task Failed</b>"
break;
case "SucceededWithIssues":
body += "\n<b>⚠️ Task has some Issues</b>"
break;
default:
break;
}
}
if (tl.getBoolInput('buildNumber', false)) {
body += "\n<b>#Build-" + tl.getVariable("Build.BuildNumber") + "</b>";
} else if (tl.getBoolInput('releaseName', false)) {
body += "\n<b>#" + tl.getVariable("Release.ReleaseName") + "</b>";
}
if (tl.getInput('message', false) !== null) {
var message = tl.getInput('message', false);
body += "\n" + message;
}
if (tl.getBoolInput('buildQueuedBy', false)) {
var buildQueuedBy = tl.getVariable("Build.QueuedBy");
body += "\n<b>Build queued by:</b> " + buildQueuedBy;
}
if (tl.getBoolInput('releaseRequestedFor', false)) {
var releaseRequestedFor = tl.getVariable("Release.RequestedFor");
body += "\n<b>Release queued by:</b> " + releaseRequestedFor;
}
if (tl.getBoolInput('teamUrl', false)) {
var teamLink = tl.getVariable("System.TeamFoundationCollectionUri");
var project = tl.getVariable("System.TeamProject");
body += "\n<b>Project URL: </b>" + tgtools.tglinkbuilder(teamLink + project, project);
}

const telegram = new Telegram(token, tgtools.getProxyCfg());
chats.forEach(chat => {
telegram.sendMessage(chat,body, {parse_mode: 'HTML'});
});
console.log('Message sent!');
}
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message);
}
});
const telegram = new Telegram(token, tgtools.getProxyCfg());
chats.forEach(chat => {
const options = { parse_mode: 'HTML' };
if (messageThreadId) {
options.message_thread_id = messageThreadId;
}
telegram.sendMessage(chat, body, options);
});
console.log('Message sent!');
}
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message);
}
});
}
run();
}
run();
}
12 changes: 11 additions & 1 deletion SendTelegramNotification/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
"helpMarkDown": "Target chat id's to notify (use comma as a delimiter). [More information about chat id] ()",
"visibleRule":"getChatId = false"
},
{
"name": "messageThreadId",
"type": "string",
"label": "Message Thread ID",
"defaultValue": "",
"required": false,
"groupName": "botConfig",
"helpMarkDown": "Target message thread id to notify (use comma as a delimiter). [More information about message thread id] ()",
"visibleRule":"getChatId = false"
},
{
"name": "useProxy",
"type": "boolean",
Expand Down Expand Up @@ -177,4 +187,4 @@
"argumentFormat": ""
}
}
}
}
20 changes: 19 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Telegram Notifications for Azure Pipelines
![TelegramTask](https://raw.githubusercontent.com/JackB1ack/telegram-notification/master/Screenshots/main.PNG)
Telegram Notification task uses [Telegraf - Telegram Bot Framework for Node.js](https://github.com/telegraf/telegraf) and allows you to send customizable notifications to your Telegram chats and groups.
Expand Down Expand Up @@ -74,3 +73,22 @@ There are some basic predefined features both for build and release tasks.
*NOTE: USE EITHER RELEASE OR BUILD RELATED OPTIONS DEPENDING ON YOUR PROCESS*

![NotificationResult](https://raw.githubusercontent.com/JackB1ack/telegram-notification/master/Screenshots/notification.PNG)

## Sending Messages to Specific Topics within Supergroups

To send messages to a specific topic within a supergroup, you need to specify both `chat_id` and `message_thread_id`. The `message_thread_id` parameter allows you to target a specific thread within a supergroup.

### Example Configuration

Here is an example of how to configure the task to send messages to a specific topic within a supergroup:

```json
{
"botToken": "YOUR_BOT_TOKEN",
"chats": "-123456678",
"messageThreadId": "123456789",
"message": "This is a message to a specific topic within a supergroup."
}
```

In this example, replace `YOUR_BOT_TOKEN` with your actual bot token, `-123456678` with the chat ID of the supergroup, and `123456789` with the message thread ID of the specific topic within the supergroup.

0 comments on commit b84c519

Please sign in to comment.