Introduction

Our python SDK is open source & is available at https://github.com/scupid-admin/morph-python-sdk. Any contributions are well appreciated.

The skeleton python code is below.

Method name has to be morph

The dependencies that are pre-installed are listed below:

  • requests
  • arrow
  • simplejson
  • morph-ai

If you think that there should be any dependencies that should be pre-installed, please contact support.

Support for custom python dependencies is coming soon.

Setting up your workspace.

To install morph package run sudo pip install morph-ai.

Skeleton Code

from morph.response import Response
from morph.action import GoToFlow


def morph(event, context): 
    ...
    response = Response()
    response.add_action(GoToFlow("My Conversation"))
    return response.build()

Lets analyse above code. event is the parameter that we need to be concerned about. Leave the context parameter.

Event parameter

event contains information like USER or FLOW level variables. It is of dict type. A sample json of event would be:

{
  "userVariables": {
    "city": "New York",
    "age": 17,
    "orders": [
      "#o-111",
      "#o-122"
    ]
  },
  "flowVariables": {
    "booking date": "1st July 2017"
  }
}

Response object

Let's look at the Response object that we are returning. Here is the source code of it.

class Response(MorphSerializable):
    def __init__(self):
        MorphSerializable.__init__(self, ["actions"], None)
        self.actions = []

    def add_action(self, action):
        self.actions.extend(action)

It just contains a list of action. Let's look at the action class.

Action

There are 3 Actions available.

Action Description import
GoToFlow This is jump module of UI. It redirects to a conversation. from morph.action import GoToFlow
Publish Used to send a message back to user. from morph.action import Publish
SetVariable This action is used to set an attribute value. The attribute can either be of FLOW or USER scope. from morph.action import SetVariable

The usage of these actions is described in examples below.

1) Extract a value of an attribute and setting some attributes

In the below code we are reading the phone number of the user & checking if it exists in our database. If it does then we are setting some of the attributes. If it does not then we are taking the user to another conversation named "PHONE_DOES_NOT_EXIST".

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


def morph(event, context):
    response = Response()

    number_dict = {
        "9711xxx400": True,
        "8130xxx599": True
    }
    phone_number = event["userVariables"]["_PHONE_NUMBER"]
    if phone_number is None:
        phone_number = ""
    else:
        phone_number = str(phone_number)

    if phone_number in number_dict.keys():
        # This is creating a variable. Variables can be of different kinds. See variable.py. They can be 
        # StringVariable, NumberVariable, StringArrayVariable 
        # 
        # variable_scope can be either USER or FLOW
        variable = SetVariable(variable_scope="USER", variable_title="Valid Customer", variable=StringVariable("true"))
        response.add_action(variable)
    else:
        # This redirects user to a new conversation called "number invalid"
        response.add_action(GoToFlow("number invalid"))
    return response.build()

2) Sending a message to the user

Let's see how we can reply back to the user. There are various types of messages that you can send. They are:

1) statement (Text message with optional buttons) 2) carousel (A horizontal carousal message) 3) list (A vertical carousal message) 4) media (Image/Video message)

All of these messages can contain suggestions (Quick Replies).

suggestions (Quick Replies)

Below code will demonstrate how to add suggestions to any kind of message. The code assumes statement (text) message.

def morph(event, context):
    publish_action = Publish()
    text_message = TextMessage("Hello, please select options from below.")
    complaint_suggestion = Suggestion("File a complain", "TEXT", "complaint", None)
    demo_suggestion = Suggestion("Schedule a demo", "TEXT", "demo", None)
    text_message.add_suggestion(complaint_suggestion)
    text_message.add_suggestion(demo_suggestion)
    publish_action.add_message(text_message)

    return Response().add_action(publish_action).build()

message object

Message object has different attributes based on the message type. The type of the message can be one of the following:

statement

Represents a text message

Property Description Required
type value=statement Y
text The text to publish Y
buttons An array of Button objects N
suggestionElements An array of Suggestion objects N

Example code:

def morph(event, context):
    publish_action = Publish()
    text_message = TextMessage("Hello, please select options from below.")
    complaint_suggestion = Suggestion("File a complain", "TEXT", "complaint", None)
    demo_suggestion = Suggestion("Schedule a demo", "TEXT", "demo", None)
    text_message.add_suggestion(complaint_suggestion)
    text_message.add_suggestion(demo_suggestion)
    publish_action.add_message(text_message)

    return Response().add_action(publish_action).build()
Property Description Required
type value=carousel Y
carousalElements An array of CarousalElement objects Y
suggestionElements An array of Suggestion objects N

Example object:

def morph(event, context):
    publish_action = Publish()
    carousal_message = CarousalMessage()
    element = CarousalElement(title="Morph.ai", sub_title="Experience the best",
                              image_url="http://www.morph.ai/logo.jpg", click_url="http://www.morph.ai")
    element.add_button(button=URLButton(title="Signup", url="app.morph.ai", height="TALL"))
    carousal_message.add_carousal_element(
        element)
    publish_action.add_message(carousal_message)
    return Response().add_action(publish_action).build()
media
Property Description Required
type value=media Y
mediaUrl The public URL of the media Y
mediaType Either "image" or "video" Y

Example code:

def morph(event, context):
    publish_action = Publish()
    media_message = MediaMessage(media_url="http://morph.ai/logo.jpeg", media_type="image")
    publish_action.add_message(media_message)
    return Response().add_action(publish_action).build()

FAQs

My code does not work

Please check the following checklist:

  • You are not using any package that is not listed in the dependencies
  • Your method name is morph
  • You are returning response.build() and not just returning response
  • You have tested the code locally
  • You have gone through the examples provided in the documentation above
  • You have used your colleague as a rubber duck 🦆

A package that I want to use is not listed as a dependency

Please contact support with relevant info. email: [email protected]

results matching ""

    No results matching ""