Skip to Content
Technical Articles
Author's profile photo Mariajose Martinez

Send application/x-www-form-urlencoded data to a HTTP receiver adapter in SAP Cloud Integration to send SMS messages consuming a Twilio API

Hello!

This blog post belongs to a Tutorial Blog Post series about where its simulating a sales transaction on buying a new refrigerator using a SAP AppGyver custom app, while consuming different SAP and 3rd party services.

In the previous blog post we set up the OData receiver adapter to create Sales Orders in SAP Sales and Service Core (formerly SAP Cloud for Customer), and configured a Message Mapping artifact to transform the payload with the XML and XSD schema needed for this Sales Order creation.

In this blog post you’ll review how to pass parameters with urlencoded form to a HTTP receiver adapter, to send SMS messages using a Twilio API, as a notification that the Order has been successfully created.

 

Tutorial Blog Post Series – Sections

They are divided into several blog posts to not make it so long in one alone.

  1. Build an integral SAP Integration Suite project and consume it from a SAP AppGyver custom app – Tutorial Blog Post Series Introduction [Link]
  2. Consume a Stripe service from SAP Open Connectors and SAP Cloud Integration to create payment transactions [Link]
  3. Set up Write, Filter and Get tasks in SAP Cloud Integration to save, filter and get your needed message to execute other operations in your Integration Flow [Link]
  4. Consume a SAP Sales and Service Core API to create Sales Orders using an OData receiver adapter in SAP Cloud Integration [Link]
  5. Send application/x-www-form-urlencoded data to a HTTP receiver adapter in SAP Cloud Integration to send SMS messages consuming a Twilio API [Here you are in this blog post]
  6. Integrate SAP AppGyver with SAP Integration Suite, consuming an Integration Flow levering SAP API Management policies [Link]

 

Let’s start

To continue from the previous blog post where we configured a SAP Sales and Service Core API to create sales orders, now what we are going to do is to get the sales order id and send a SMS with a custom message, consuming the Twilio “create a message” API. Here’s the Twilio API documentation.

As you may see in the Twilio’s documentation, there are 3 needed urlencoded parameters to be sent to trigger the API call.

  • From <Twilio sender’s phone number>
  • To <Recipient’s phone number>
  • Body <Body of the message / SMS to send>

Keep in mind that in fact the recommended way to consume Twilio APIs, is by leveraging its Connector with SAP Open Connectors, as we used in the firsts blog posts with Stripe. However I wanted to conduct the API consumption directly just for testing and documentation purposes, because I looked for documentation on how to configure urlencoded parameters to call APIs from SAP Cloud Integration and I couldn’t find any. For this exercise, I want to thank Rafael Moncayo, who quickly he help me out to understand how to easily configure these parameters as needed.

First, we need to do is to get the order Id from the SAP Sales and Service Core API sales order creation response. For this step, we need to add a Content Modifier task and set up a new property as shown in the picture below. By selecting the Content Modifier task, go to the “Exchange Property” tab and add a new property. I’m calling mine “salesOrderId”, and the XPath needed is: /CustomerOrderCollection/CustomerOrder/ID. For the rest of the tabs, leave them empty.

Now as we are going to send dynamic SMS with different sales orders Id for every time we run the demo, we need to add a Groovy Script task to configure our new needed Body for the Twilio SMS API.

Add a Groovy Script and create a new script by clicking in the page and plus + icon while selecting the Groovy task:

Copy and paste this code:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Properties
    def properties = message.getProperties();
    salesOrder = properties.get("salesOrderId");
    messageText = ("Thank you for your purchase, your order id is " + salesOrder)
    message.setProperty("encodedMessageText", java.net.URLEncoder.encode(messageText, "UTF-8"));
    return message;
}

You should see like this:

As you can see, you don’t need to add the headers here or the body, as we will use another Content Modifier to set them up. We are just getting the salesOrderId from the SAP Sales and Service core API response and concatenating it with the text message we want to send as a body in the SMS. You can also configure the same to get the phone number of the related customer and use it in the encoded message body to dynamically change the recipient’s phone number as needed. For this exercise, I’ll just do it with the sales order id.

Now, add another Content Modifier and we are going to add the Content-Type as a header. Keep in mind that SAP Cloud Integration is case sensitive, so be sure to type down Content-Type with the C and T in capital letters.

In the source value, copy and paste: application/x-www-form-urlencoded

Go now to the Message Body, and here you’re going to use the type “Expression” (as the sales order and phone number will change in real life in every order).

As Twilio requires the message to be sent with an encoded URL, you can practice/check the message to be encoded. This is the web page that I used to encode the body as Twilio needs it.

Remember how Twilio needs the parameters to be sent:

  • From <Twilio sender’s phone number>
  • To <Recipient’s phone number>
  • Body <Body of the message / SMS to send>

And by passing these parameters together, its needed to add the ampersand symbol “&” between them. Like this (this is an example):

From=inputValue1&To=inputValue2&Body=inputValue3

To make it consistent with the Sales Order, I’m sending this text message and concatenating its Sales Order like with this new property previously created in the Groovy script: ${property.encodedMessageText}

Go back to your second Content Modifier and in the Message Body tab, change the type to Expression and add your encoded text message and needed parameters:

Now it’s time to request the call to the Twilio’s API. Let’s add a Request Reply task, a Receiver’s box and of course a HTTP receiver adapter.

In the connection tab you’ll need to specify your Twilio’s API endpoint (remember to check the documentation). Create your Twilio developer account’s credentials in SAP Cloud Integration, and extremely important -> add as a Request Header the “Content-Type” header as defined in the 2nd content type. This is key to send the urlencoded parameters to trigger the Twilio create a message’s API.

Just for a reminder purpose, to create your Twilio developer account credentials, you’ll need to go the eye icon (Monitor tab of the menu), click on Security Materials -> create and select User Credentials type with your Twilio user and password.

You should see it like this:

Save and deploy your iFlow.

Now it’s time run a test for the whole iFlow. Remember to activate the “Trace” mode in your already deployed iFlow:

I’m using Postman for testing my CPI API endpoint with the same initial payload mentioned in the previous blogs:

{
  "AppGyverSalesOrder": {
    "customer": {
      "ObjectID": "<Object ID from SAP C4C>",
      "BuyerPartyID": <BuyerPartyID from SAP C4C>
    },
    "paymentData": {
      "amount": <Payment transaction amount to be processed in Stripe>,
      "customer": "<stripe customer id>",
      "currency": "<currency selected in stripe account>",
      "source": "<card id>",
      "description": "Test payment via CPI"
    },
    "product": [
      {
        "ProductID": "<Product ID from SAP C4C>",
        "Quantity": <Product quantity to be purchased>
      }
    ]
  }
}

If you want to, check in the Monitor Processing Message and Trace how the message was encoded:

And the SMS:

 

Conclusion

Great! Whether you are following the previous blogs or just entered this one to learn how to send url encoded parameters, you’ve successfully have learned how to execute it by using SAP Cloud Integration.

Now it’s time to start consuming this integral SAP Integration suite project (and the SAP Cloud Integration API endpoint from our iFlow) from a custom AppGyver application. For this step, check this next blog: Integrate SAP AppGyver with SAP Integration Suite, consuming an Integration Flow levering SAP API Management policies [Link].

 

Want to know more about SAP Business Technology Platform?

To learn more about SAP BTP, see the learning journey Discover SAP Business Technology Platform, a great introduction to BTP and the Intelligent Enterprise strategy to see what it’s all about for free. Discover BTP, LCNC plus much more free learning at SAP Learning site.

 

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.