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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Custom Message Handler

NiKlaus
12-Amethyst

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 

ACCEPTED SOLUTION

Accepted Solutions
NiKlaus
12-Amethyst
(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

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.

Hi @NiKlaus 

Look at the below article for your reference.

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

NiKlaus
12-Amethyst
(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);
}
Announcements


Top Tags