Code snippet

For nearly all bot building requirements, conditional responses - with their rich functionality and varied response templates - should be able to satisfy the requirements. For the rare case when developers aren't able to build their uses cases through conditional responses or for code-savvy developers who prefer using code, Code snippet response type is available.

Code snippet can be configured using snippets of python code. You can configure all types of response templates such as quick reply, text, carousel, image, audio, video, and file using this code for a response template or an article.

The function code configured in the code template is used to set variables which can be further used in templates. However, the function code cannot return responses when used within the conditional responses.

873

Use of function code within the conditional response

🚧

Code snippet validation

The platform only looks for syntax errors with the code snippet that is being configured. Any errors with the content of the response in code snippet will lead to errors for anyone interacting with the bot on the channel where it is configured. For example, the editor will not stop you from configuring a 'time picker' response for web channel but it will lead to errors when a user query triggers that particular response.

If the bot developer chooses not to configure a unique response for different channels, the Web response will be considered as the default response and will be sent to the end-user. 

Syntax for Web channel templates

Text

Simple text message that can have multiple variants. This configured message is shown to the consumer based on the query. Following is an example of configuring text response for multiple channels using code snippet:

output = {
  "generated_msg": [
    {
      "text": [
        "If you've lost your card or it's been stolen, it's important to call us as soon as you can, so we can cancel your card.\\nFrom the UK 0800 151 XXXX.From abroad +44 1604 230 XXX.You can also use our Next Generation Text or SignVideo service.\\nCheck your statement in case there's been any fraudulent activity on your card. We'll get your new card to you within three to five working days."
      ],
      "include": [
        "slack"
      ]
    },
    {
      "text": [
        "If you've lost your card or it's been stolen, it's important to call us as soon as you can, so we can cancel your card.\\nFrom the UK 0800 151 XXXX.From abroad +44 1604 230 XXX.You can also use our Next Generation Text or SignVideo service.\\nCheck your statement in case there's been any fraudulent activity on your card. We'll get your new card to you within three to five working days."
      ],
      "include": [
        "facebook"
      ]
    },
    {
      "text": [
        "If you've lost your card or it's been stolen, it's important to call us as soon as you can, so we can cancel your card.\\nFrom the UK 0800 151 XXXX.From abroad +44 1604 230 XXX.You can also use our Next Generation Text or SignVideo service.\\nCheck your statement in case there's been any fraudulent activity on your card. We'll get your new card to you within three to five working days."
      ],
      "include": [
        "whatsapp"
      ]
    },
    {
      "text": [
        "If you've lost your card or it's been stolen, it's important to call us as soon as you can, so we can cancel your card.\\nFrom the UK 0800 151 XXXX.From abroad +44 1604 230 XXX.You can also use our Next Generation Text or SignVideo service.\\nCheck your statement in case there's been any fraudulent activity on your card. We'll get your new card to you within three to five working days."
      ],
      "include": [
        "abc"
      ]
    },
    {
      "include": [
        "web"
      ],
      "text": [
        "Hi, please go to the architecture tab in edit bot page to start configuring your bot."
      ]
    }
  ]
}

Quick reply

This template has text along with clickable buttons.

output = {
  "generated_msg": [
    {
      "quick_reply": [
        {
          "text": "You can choose the veg pizzas you want from here:",
          "quick_replies": [
            {
              "payload": "a1",
              "title": "Margherita Pizza",
              "content_type": "text"
            },
            {
              "payload": "a1",
              "title": "Golden Corn Pizza",
              "content_type": "text"
            },
            {
              "payload": "a1",
              "title": "Double Cheese Margherita Pizza",
              "content_type": "text"
            }
          ]
        }
      ],
      "include": [
        "web"
      ]
    }
  ]
}

Carousel

Carousel has a collection of cards and each card must have title, image URL and description.

output =
{
  "generated_msg": [
    {
      "generic_template": [
        {
          "elements": [
            {
              "image_url": "tierra-mallorca-JXI2Ap8dTNc-unsplash.jpg",
              "title": "Lincoln Park towers",
              "description": "Logan Square",
              "button": [
                {
                  "payload": "expire",
                  "title": "Book Now",
                  "type": "postback"
                },
                {
                  "payload": "a1",
                  "title": "Know more about the property",
                  "type": "postback"
                }
              ]
            },
            {
              "image_url": "https://imibot-production.s3-eu-west-1.amazonaws.com/integrations/v2/media/shallow-focus-photo-of-white-open-sigange-3345876.jpg",
              "title": "Logan Square",
              "description": "Logan Square",
              "button": []
            }
          ]
        }
      ],
      "include": [
        "web"
      ]
    }
  ]
}

Image

Template to configure images by providing URLs.

output = {
  "generated_msg": [
    {
      "image": [
        {
          "url": "https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_100kB.jpg",
          "type": "image/jpeg"
        }
      ],
      "include": [
        "web"
      ]
    }
  ]
}

Video

Template to configure video by providing the video URL. Consumer can play the video by clicking or tapping on the image.

output = {
  "generated_msg": [
    {
      "video": [
        {
          "url": "https://storage.googleapis.com/coverr-main/mp4%2Fcoverr-brooklyn-bridge-1572178567128.mp4",
          "type": "video/mp4"
        }
      ],
      "include": [
        "web"
      ]
    }
  ]
}

File

Template to configure pdf file by providing the URL to access the file.

output = {
  "generated_msg": [
    {
      "file": [
        {
          "type": "application/pdf",
          "url": "https://download.newlap.com/pccbbs/mobiles_pdf/p1gen2_x1extreme_ug_v2_en.pdf",
          "componentType": "file"
        }
      ],
      "include": [
        "web"
      ]
    }
  ]
}

Audio

Template to configure audio file by providing the audio URL. It also shows the duration of the audio message in the output.

output = {
  "generated_msg": [
    {
      "audio": [
        {
          "url": "http://imibot-production.s3.amazonaws.com/6/502/502/audio/8968.mp3",
          "type": "audio/mp3"
        }
      ],
      "include": [
        "web"
      ]
    }
  ]
}

Apple Messages for Business specific templates

Following are the response templates that are supported only for the Apple Messages for Business channel.

List picker

Configure the list of products or items. Each list section must have title, image URL, image style, description, reply title and reply description. Consumer can choose an option from the list, which is sent as a response by the bot.

output = {
  "generated_msg": [
    {
      "listpicker": [
        {
          "reply_message": {
            "title": "Know more about the violin models",
            "subtitle": "violin models"
          },
          "received_message": {
            "title": "Select violin of your choice",
            "subtitle": "Violins might be frowned upon by some professionals, due the quality of both the violin and bow. As the violins are not handcrafted, there will be minor fine tuning and adjustments to be made",
            "style": "icon",
            "image_url": "https://sample-videos.com/img/Sample-jpg-image-50kb.jpg"
          },
          "sections": [
            {
              "_id": 1648011056132,
              "title": "Violin models",
              "items": [
                {
                  "_id": 1695452083861,
                  "image_url": "https://sample-violinmodel.com/img/Sample-jpg-image-50kb.jpg",
                  "title": "MV300 Violin",
                  "identifier": "a1",
                  "subtitle": "It is not recommended for intermediate violinists, but is perfect for starter violinists."
                }
              ]
            },
            {
              "_id": 1687987556985,
              "title": "Strad Model 101 Violin",
              "items": [
                {
                  "_id": 1684118804509,
                  "image_url": "",
                  "title": "https://sample-violinmodel.com/img/Sample-jpg-image-50kb.jpg",
                  "identifier": "a1",
                  "subtitle": "Cremona’s highest rated beginner violin because of the value it offers – combining playability, tone quality, specification, and price"
                }
              ]
            }
          ],
          "buttons": [],
          "_id": 1711217480840
        }
      ],
      "include": [
        "abc"
      ]
    }
  ]
}

Time picker

Configure different time slots to allow consumers to schedule a meeting or book an appointment at their convenient time. The event section must have title, time zone, and slots.

output = {
  "generated_msg": [
    {
      "timepicker": [
        {
          "reply_message": {
            "title": "Select the date and time to visit the store",
            "subtitle": ""
          },
          "received_message": {
            "title": "Store timing",
            "subtitle": "Book slots to shop at your convenient time now ",
            "style": "icon",
            "image_url": ""
          },
          "event": {
            "title": "Select the time to visit the store",
            "timezone": "Europe/London",
            "slots": [
              {
                "_id": 1671606928209,
                "start_time": "2021-09-28T17:32",
                "duration": 4500,
                "identifier": "slot1"
              },
              {
                "_id": 1644212391705,
                "start_time": "2021-09-28T18:15",
                "duration": 4500,
                "identifier": "slot2"
              }
            ]
          },
          "_id": 1646355134089
        }
      ],
      "include": [
        "abc"
      ]
    },
    {
      "text": [
        "Please configure your message for web"
      ],
      "include": [
        "web"
      ]
    }
  ]
}

Rich link

Configure rich links where URL is provided through an image or a video. This is displayed in a chat bubble. Clicking on the bubble directs the consumer to land on the web page specified in the image or video.

output = {
  "generated_msg": [
    {
      "richlink": [
        {
          "title": "The cabinet - $499",
          "url": "cabin.com",
          "image": {
            "type": "image/jpeg",
            "url": "https://i.imgur.com/IjVUIXz.jpg"
          },
          "componentType": "richlink",
          "_id": 1686088215524
        }
      ],
      "include": [
        "abc"
      ]
    }
  ]
}

Media

Configure the text message with an attachment. This template supports attachments that are in various formats such as jpeg, mp3, mp4, png, pdf and aac.

output = {
  "generated_msg": [
    {
      "text_with_media": [
        {
          "text": "Check the image to view laptop specifications",
          "attachments": [
            {
              "type": "image/jpeg",
              "url": "https://sample-videos.com/img/Sample-jpg-image-50kb.jpg"
            }
          ],
          "componentType": "text_with_media",
          "_id": 1662289617266
        }
      ],
      "include": [
        "abc"
      ]
    }
  ]
}