Introduction

The currently supported version is Python2. You have to follow the Python2 syntax while writing the code.


The following libraries are already imported to help you implement various use-cases:

  • Requests: To make API calls.
  • Arrow: To creating, manipulating, formatting, converting dates, times, and timestamps.
  • Simplejson: Parse/Encode JSON.


This document contains the following:

  1. Examples
    1. Use existing properties & set new properties
    2. Send message
    3. Make API call & send message
    4. Importing python's internal libs
    5. Parse Rest API block response
  2. How to write code using any IDE.


1. Examples

Please follow the examples below to learn how to write code for different use-cases. You can also find all the below-mentioned example codes here.


1.1 Use existing properties

Use existing properties to get the current context of the conversation like the user's first name, etc. The syntax to access any existing property is: event["userVariables"]["<Property Name>"]


from morph.response import Response
from morph.action import *
from morph.variable import *
from morph.message import *

# DO NOT CHANGE THE FUNCTION NAME
def morph(event, context):
    response = Response()
    
    # Access exiting properties like this:
    user_first_name = event["userVariables"]["First Name"]
    
    # Set property
    variable = SetVariable(variable_scope="USER", variable_title="First Name Caps", variable=StringVariable(user_first_name.upper()))
    response.add_action(variable)

    return response.build()


1.2 Send Message

from morph.response import Response
from morph.action import *
from morph.variable import *
from morph.message import *

# DO NOT CHANGE THE FUNCTION NAME
def morph(event, context):
    response = Response()
    # Write your code here
    # Access exiting properties like this:
    user_first_name = event["userVariables"]["First Name"]

    # Create message
    message = 'Hello '+ user_first_name +', how can I help you?'

    # Send message
    publish_action = Publish()
    text_message = TextMessage(message)
    publish_action.add_message(text_message)
    response.add_action(publish_action)
    
    return response.build()

1.3 Make API call & send message

from morph.response import Response
from morph.action import *
from morph.variable import *
from morph.message import *
import requests 

# DO NOT CHANGE THE FUNCTION NAME
def morph(event, context):
    response = Response()
    # Write your code here
    # Call API
    url = 'http://180.149.246.197/RoxyCinemasWebServ/api/service/GetNowShowing?cinemaid=&userid=0&platform=4&Flag=home'
    d = requests.get(url = url)
    data = d.json()
    
    # Create message
    message = 'Hello, please select the movie from below.\n\n'
    for index,d in enumerate(data['nowShowingList']):
        message = message + str(index) +". "+ d['Title']+"\n"

    # Send message
    publish_action = Publish()
    text_message = TextMessage(message)
    publish_action.add_message(text_message)
    response.add_action(publish_action)
    
    return response.build()


1.4 Importing standard python library

Any python's standard library can be imported. For eg. in the below code, we will be using random lib.

from morph.response import Response
from morph.action import *
from morph.variable import *
from morph.message import *
import random

# DO NOT CHANGE THE FUNCTION NAME
def morph(event, context):
    response = Response()
    # Write your code here
    movies = ['Dolittle', 'The Gentlemen', 'Birds of Prey', 'The Lovebirds', 'Mulan', 'The Beatles: Get Back', 'The King’s Man', 'Black Widow']
    
    # Randomly select a movie
    movie = random.choice(movies)
    
    # Set property
    variable = SetVariable(variable_scope="USER", variable_title="Movie Name", variable=StringVariable(movie))
    response.add_action(variable)
    
    return response.build()


1.5 Parse Rest API block response

Sometimes, we use the Rest API block to make an API call but we want to write the code to parse the response.


This is a similar example where we made an API (using Rest API block) call to fetch employee detail and we want to parse the result to find the name of the employee. We used a dummy API, which gives the following response:

{
  "status": "success",
  "data": {
    "id": "2",
    "employee_name": "Garrett Winters",
    "employee_salary": "170750",
    "employee_age": "63",
    "profile_image": ""
  }
}

Now, let's write the code to parse this response.

from morph.response import Response
from morph.action import *
from morph.variable import *
from morph.message import *
import json

# DO NOT CHANGE THE FUNCTION NAME
def morph(event, context):
    response = Response()
    # Write your code here
    # Access Rest API block response
    api_response_string = event["flowVariables"]["API Response"]

    # Parse response
    api_response = json.loads(api_response_string);

    # Use the response
    employee_name = api_response['data']['employee_name']
    
    # Set property
    variable = SetVariable(variable_scope="USER", variable_title="Name", variable=StringVariable(employee_name))
    response.add_action(variable)
    
    return response.build()


How to write code using any IDE

You can write the code using the default Code block editor, but if needed you can write the code in your own IDE too. To do the same you need to install Morph.ai's code python SDK. Use the following to do the same:

pip install morph-ai