How to use Serverless Function

In the previous post, we have seen how to create a trigger and before/after function.
Now, we will see how to use Serverless Function on Hypi’s low code platform.

A brief on Serverless Function:

A serverless function is a programmatic function that is hosted and maintained on cloud. The cloud companies take care of code maintenance and execution. They have Function as a Service (FaaS) platforms that execute application logic of the Serverless function. However, these platforms do not store data.

To implement custom behaviour outside the scope of Hypi APIs, you can use serverless function.
So, how do you use the Serverless function on hypi’s low code backend?

You need to create the function on Apache OpenWhisk Serverless Platform. The function can be written in any programming language of your choice. OpenWhisk supports growing list of programming languages.

Let’s create a Serverless function on OpenWhisk and execute it.

***Please note you need to install OpenWhisk to use Serverless function

Example

Let’s say we want to send SMS to various numbers as a functionality in an Application. We can do this by implementing logic on Serverless function.

We will use APIs by ClickSend (https://www.clicksend.com/). You may check API settings to understand the flow of sending SMS.

Here is the Python function written to send the SMS programmatically. We will use this as our Serverless function.

import json
import requests
#Function to send SMS
def main(args):
    toNo = args.get("to")
    fromNo = args.get("from")
    body = args.get("body")
    url = "https://rest.clicksend.com/v3/sms/send"
    payload = json.dumps({
    "messages": [
        {
    	"body": body,
    	"to": toNo,
        "from": fromNo
        }
    ]
    })
    headers = {
  	'Content-Type': 'application/json',
    	'Authorization': 'Basic YXNhQGh5cGkuaW86RTI4QjMzQjgtMTRCMy01OUM3LUIyNUYtODUzQzExNEM5RjJB'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    text = response.text
    return json.loads(text)

This function sends POST messages to the API url with specific headers. The message body gets sent from a mobile number to another number.

We will first configure OpenWhisk.

wsk property set --apihost "https://fn.hypi.app" --auth "<InstanceDomain\>:<Auth_Key>"

Now create an action sendSMS using the python function stored in the file sendSMS.py.

wsk action create sendSMS sendSMS.py
ok: created action sendSMS

Invoke the function (It is an activation of Serverless function on OpenWhisk). -p takes input parameters for the function. --result shows the result of the function.

wsk action invoke sendSMS -p to +6141111111 -p from +61422222222 -p body "You've got a message" --result
{
    "data": {
        "_currency": {
            "currency_name_long": "Indian Rupees",
            "currency_name_short": "INR",
            "currency_prefix_c": "¢",
            "currency_prefix_d": "₹"
        },
        "messages": [
            {
                "body": "You've got a message",
                "custom_string": "",
                "from": "+61422222222",
                "message_id": "742B2652-E704-4B85-B430-AB30984999D6",
                "schedule": "",
                "status": "INVALID_RECIPIENT",
                "to": "+6141111111"
            }
        ],
        "queued_count": 0,
        "total_count": 1,
        "total_price": 0
    },
    "http_code": 200,
    "response_code": "SUCCESS",
    "response_msg": "Messages queued for delivery."
}

Now is the time to use the function! Let’s define the function as Query on Hypi’s low code platform like this.

type Query {
   SMSfunction(to: String,from: String,body:String): Json @tan(type:OpenWhisk, name:"sendSMS")
}

SMSfunction takes the input parameters just like python function and returns Json output. @tan directive is used to define the function.

Let’s execute the serverless function now.

{
  SMSfunction(to:"+6141111111",from:"+61422222222",body:"You've got a message")
}
#result
{
  "data": {
    "SMSfunction": {
      "data": {
        "_currency": {
          "currency_name_long": "Indian Rupees",
          "currency_name_short": "INR",
          "currency_prefix_c": "¢",
          "currency_prefix_d": "₹"
        },
        "messages": [
          {
            "body": "You've got a message",
            "custom_string": "",
            "from": "+61422222222",
            "message_id": "06AFD712-DFE0-4E8A-8947-A48990D29860",
            "schedule": "",
            "status": "INVALID_RECIPIENT",
            "to": "+6141111111"
          }
        ],
        "queued_count": 0,
        "total_count": 1,
        "total_price": 0
      },
      "http_code": 200,
      "response_code": "SUCCESS",
      "response_msg": "Messages queued for delivery."
    }
  }
}

***Please not the SMS was sent to a test number.