cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Custom Message Handler

NiKlaus
7-Bedrock

Custom Message Handler

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 

1 ACCEPTED SOLUTION

Accepted Solutions
NiKlaus
7-Bedrock
(To: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);
}

View solution in original post

3 REPLIES 3
Rocko
17-Peridot
(To:NiKlaus)

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.

Surya_Tiwari
13-Aquamarine
(To:NiKlaus)

Hi @NiKlaus 

Look at the below article for your reference.

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

NiKlaus
7-Bedrock
(To: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);
}
Top Tags