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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Gen AI & Third party integration sample – Sending a Text Message

BrunoD
12-Amethyst

Gen AI & Third party integration sample – Sending a Text Message

Overview

 

As I was pursuing some research around leveraging our corporate Gen AI, Copilot, I did want to apply this to a request from a customer. The request was simple, can we send a text message when an alert is triggered.

 

I wanted the outcome to be a set of instructions allowing to integrate a third-party service into a PTC ThingWorx without requiring digging into documentation from both sides.

 

The tutorial below is the result of the following prompt executed in Copilot:  
“Could help me writing a thingworx rest service call for sending a message through twilio?”

Note: The content generated has been formatted to make it more accessible from any public.

 

Step 1:  Set Up Twilio Account

 

Make sure you have a Twilio account and have your Account SID, Auth Token, and a Twilio phone number.

BrunoD_0-1732407271686.png

BrunoD_1-1732407271695.png

 

BrunoD_2-1732407271697.png

 

Step 2:  Create a ThingWorx Service

 

In ThingWorx, create a new service in your Thing or ThingTemplate.

As an example, below I created a “Thing” called “TwilioThing” with a service called “SendMessage”:  

BrunoD_3-1732407271700.png

 

Step 3:  Write the Service Code

 

Use the following code as a template for your service. This code will make an HTTP POST request to Twilio's API to send a message.

BrunoD_4-1732407271705.png

 

// Define the Twilio API endpoint
var url = "https://api.twilio.com/2010-04-01/Accounts/YOUR_ACCOUNT_SID/Messages.json";

// Set up the message parameters
var params = {
    "To": "RECIPIENT_PHONE_NUMBER", // Recipient's phone number
    "From": "YOUR_TWILIO_PHONE_NUMBER", // Your Twilio phone number
    "Body": "YOUR_MESSAGE" // Message body
};

// Convert the parameters to a URL-encoded string
var postData = "";

for (var key in params) {
    if (postData.length > 0) {
        postData += "&";
    }
    postData += encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
}

// Set up the headers for the request
var headers = {
    "Authorization": "Basic " + base64EncodeString("YOUR_AUTH_TOKEN"),
    "Content-Type": "application/x-www-form-urlencoded"
};

// Make the POST request to Twilio
var result = Resources["ContentLoaderFunctions"].PostText({
    url: url,
    headers: headers,
    content: postData
});

// Log the result
logger.info("Twilio Response: " + result);

 

Step 4:  Replace Placeholders

 

Make sure to replace the placeholders (YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN, RECIPIENT_PHONE_NUMBER, YOUR_TWILIO_PHONE_NUMBER, YOUR_MESSAGE) with your actual Twilio credentials and message details.

 

Step 5: Test the Service

 

Save and test your service to ensure it sends the message correctly.

BrunoD_5-1732407271707.png

BrunoD_6-1732407271711.png

 

Conclusion

 

This example is a multipurpose starting point.

If you want to send a text message with Twilio and PTC ThingWorx, feel free to use it.

If you want to go further with Twilio services in your ThingWorx applications please check out the extension provided by IQNOX partner through the following links:
https://community.ptc.com/t5/IoT-Tips/Send-Messages-with-Twilio/ta-p/834729

https://support.ptc.com/help/thingworx/platform/r9.6/fr/index.html#page/ThingWorx/Help/Extensibility/Twilio.html

 

Finally, if you have integration topics that you would like to explore leveraging Copilot (Gen AI) I hope this provided a vision on what it could help you achieve…

0 REPLIES 0
Announcements


Top Tags