Skip to main content
12-Amethyst
February 27, 2024
Solved

Custom Message Handler

  • February 27, 2024
  • 3 replies
  • 1592 views

Hi all. I'd like to set up a custom message handler on Thingworx 9.4.3 and I'd like to use telegram. 

Wondering if anyone has done this before or can provide me with steps to achieving this. 

 

Thanks 

Best answer by NiKlaus

Here is where I found the solution: 

 

 

https://support.ptc.com/help/thingworx/platform/r9.5/en/#page/ThingWorx/Help/Composer/Things/ThingSubscriptions/ThingSubscriptions.htmlhttps://support.ptc.com/help/thingworx/platform/r9.5/en/#page/ThingWorx/Help/Composer/Things/ThingSubscriptions/ThingSubscriptions.html

 

var message = "New Event with type: " + eventName + ". " + ThingName + "'s " + propName + " changed from " + oldValue + " at " + oldValueTime + " to " + newValue + " at " + newValueTime + ".";

handlerIDs = NotificationDefinitions["Sample_RoA"].GetAvailableHandlers();
//handlerID = handlerIDs.rows[3].handlerID;
var handlerIDsList = handlerIDs.rows;
var telegramHandlerID = "";

for(var i = 0; i < handlerIDsList.length; i++) {
 // Check if contentName contains 'Telegram'
 if(handlerIDsList[i].contentName.indexOf("Telegram") !== -1) {
 telegramHandlerID = handlerIDsList[i].handlerID;
 break; // Exit the loop once the ID is found
 }
}
//logger.warn("handlerID: " + telegramHandlerID);
//logger.info("handlerID: " + telegramHandlerID);

var UserRecipients = NotificationDefinitions["Sample_RoA"].GetUserRecipientsOfEvent({
 handler: telegramHandlerID /* STRING */,
 propertyName: propName /* STRING */,
 alertName: "" /* STRING */,
 entityType: "Thing" /* STRING */,
 entityName: ThingName /* STRING */,
 eventName: eventName /* STRING */
});
//logger.warn("Recipient List: " + UserRecipients.rows);
var UserRecipientsList = UserRecipients.rows;
for (var j = 0; j < UserRecipientsList.length; j++){
 var User = UserRecipientsList[j].name;
 let params = {
 headers: {
 token: token/*token from the configuration*/
 },
 url: 'https://api.telegram.org/bot'+token+'/sendMessage',
 content: {
 chat_id: Users[User].TelegramID, //this is the telegram user ID= '-xxx’ 
 text: message
 }
 };
// logger.warn("message: " + message);
 
 let post = Resources["ContentLoaderFunctions"].PostJSON(params);
}

3 replies

Rocko
19-Tanzanite
February 27, 2024

Telegram has a REST Bot API https://core.telegram.org/bots/api#authorizing-your-bot

In ThingWorx you would use Resources["ContentLoaderFunctions"].PostJSON or one of its other services to send the data.

The tricky part is the authorization but this is usually done by creating an appKey in the target system (here telegram) and putting in into the message header.

For ease of development, you create and test the requests with Postman and then move them over to TWX once they're working.

16-Pearl
February 27, 2024

Hi @NiKlaus 

Look at the below article for your reference.

https://www.ptc.com/en/support/article/CS395620

NiKlaus12-AmethystAuthorAnswer
12-Amethyst
April 11, 2024

Here is where I found the solution: 

 

 

https://support.ptc.com/help/thingworx/platform/r9.5/en/#page/ThingWorx/Help/Composer/Things/ThingSubscriptions/ThingSubscriptions.htmlhttps://support.ptc.com/help/thingworx/platform/r9.5/en/#page/ThingWorx/Help/Composer/Things/ThingSubscriptions/ThingSubscriptions.html

 

var message = "New Event with type: " + eventName + ". " + ThingName + "'s " + propName + " changed from " + oldValue + " at " + oldValueTime + " to " + newValue + " at " + newValueTime + ".";

handlerIDs = NotificationDefinitions["Sample_RoA"].GetAvailableHandlers();
//handlerID = handlerIDs.rows[3].handlerID;
var handlerIDsList = handlerIDs.rows;
var telegramHandlerID = "";

for(var i = 0; i < handlerIDsList.length; i++) {
 // Check if contentName contains 'Telegram'
 if(handlerIDsList[i].contentName.indexOf("Telegram") !== -1) {
 telegramHandlerID = handlerIDsList[i].handlerID;
 break; // Exit the loop once the ID is found
 }
}
//logger.warn("handlerID: " + telegramHandlerID);
//logger.info("handlerID: " + telegramHandlerID);

var UserRecipients = NotificationDefinitions["Sample_RoA"].GetUserRecipientsOfEvent({
 handler: telegramHandlerID /* STRING */,
 propertyName: propName /* STRING */,
 alertName: "" /* STRING */,
 entityType: "Thing" /* STRING */,
 entityName: ThingName /* STRING */,
 eventName: eventName /* STRING */
});
//logger.warn("Recipient List: " + UserRecipients.rows);
var UserRecipientsList = UserRecipients.rows;
for (var j = 0; j < UserRecipientsList.length; j++){
 var User = UserRecipientsList[j].name;
 let params = {
 headers: {
 token: token/*token from the configuration*/
 },
 url: 'https://api.telegram.org/bot'+token+'/sendMessage',
 content: {
 chat_id: Users[User].TelegramID, //this is the telegram user ID= '-xxx’ 
 text: message
 }
 };
// logger.warn("message: " + message);
 
 let post = Resources["ContentLoaderFunctions"].PostJSON(params);
}