Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
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.
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”:
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.
// 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.
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
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…