Error: Invalid Youtube URL.
Error: Invalid Youtube URL.
Error: Invalid Youtube URL.

UChat Official

Introduction

The transcript features a detailed walkthrough by Matthew and Joyce on external requests, a powerful feature that enables chatbots to connect seamlessly with external APIs and services.

This summary distills their insights into a structured, rich-text overview, highlighting setup procedures, best practices, common pitfalls, and potential use cases, all tailored for developers and chatbot enthusiasts aiming to leverage external requests effectively.

What Are External Requests?

External requests are API calls made from a chatbot platform to third-party services, allowing the bot to fetch, send, or process data dynamically. They serve as bridges, enabling chatbots to:

  • Retrieve product information from e-commerce APIs

  • Send messages across channels (e.g., WhatsApp, Messenger)

  • Integrate with payment gateways

  • Access external databases or services

This feature is implemented within the action step of chatbot flows, specifically under advanced actions, providing flexibility and control over external integrations.

Setting Up External Requests

1. Navigating to External Requests

  • Access the action step in your chatbot builder.

  • Select External Requests to create or edit a request.

2. Configuring the API Method and Endpoint

  • Method Selection: Choose between GET, POST, PUT, DELETE, etc., based on the API documentation.

  • Endpoint URL: Enter the API URL, e.g., https://chat.com/api/shop/products.

    • Tip: Ensure correct URL syntax; avoid common mistakes like extra slashes or typos.

  • Example: For fetching products, use the GET method with the endpoint /api/shop/products.

3. Setting Up Headers

Headers often include authorization tokens and content types:

Header Type

Description

Example

Authorization

Bearer token or API key

Bearer YOUR_TOKEN_HERE

Content-Type

Data format, e.g., application/json

application/json

  • Authorization: Use tokens, API keys, or digest authentication as required.

  • Content-Type: Usually application/json for JSON APIs.

Note: When using variables, wrap them in quotes and ensure proper syntax.

4. Managing Authorization

  • Bearer Token: Paste the token directly or store it in a variable.

  • Testing: Use the test request button to verify connectivity.

  • Common Issue: Missing or incorrect tokens lead to 401 Unauthorized responses.

5. Handling Responses and Mapping Data

  • Response JSON: After a successful request, the API returns JSON data.

  • Mapping Data: Use JSON path expressions to extract specific fields.

    • Example: To get product IDs, select the key id.

  • Sample Data: Save the response as a sample to facilitate editing and testing.

6. Looping Through Data with "For Each"

  • When multiple items are returned (e.g., list of products), use the for each loop.

  • Important: Select the correct array (e.g., search_result) to iterate over.

  • Inside the loop, map fields like name, image, price to display or process further.

Error Handling and Routing

1. Managing API Errors

  • Use response status codes to route errors.

  • Example: For 401 Unauthorized, create a route that informs the user to check their API key.

2. Creating Conditional Routes

  • Define routes based on response headers or status codes.

  • Example: If response code is 401, send a message: "Please use a correct API key."

Response Code

Action

Message

401

Route to error handler

"Invalid API key"

200

Proceed with normal flow

-

3. Fallback Values

  • Use fallback values to handle missing or null data.

  • Purpose: Prevent errors when expected data is absent.

  • Example: If status is missing, default to "unknown".

Practical Use Cases

1. Fetching Product Data

  • Use an API like shop/products to retrieve product lists.

  • Map JSON data to display product images, names, and prices.

  • Loop through multiple products for a catalog view.

2. Sending Cross-Channel Messages

  • Send messages across platforms (e.g., WhatsApp, Messenger).

  • Use send content feature with dynamic variables.

  • Example: Send a WhatsApp message with user-specific data.

3. Integrating Payment Gateways

  • Connect with APIs like Razorpay or other payment providers.

  • Generate payment links dynamically.

  • Handle responses to confirm transactions.

4. Connecting with External APIs

  • Use third-party APIs for services like weather, news, or custom databases.

  • Example: Fetch weather data and display it in the chat.

Best Practices and Tips

  • Test Extensively: Always test API calls with sample data before deploying.

  • Use Variables Wisely: Store tokens and dynamic data in variables for easy management.

  • Quote Variables: Wrap variables in quotes when used in JSON or form data.

  • Handle Errors Gracefully: Implement routes for common errors like invalid tokens or server issues.

  • Format Data Properly: Use number, date, or currency formats to enhance user readability.

  • Document Endpoints: Keep track of API endpoints and required parameters for quick reference.

Common Pitfalls and How to Avoid Them

Issue

Cause

Solution

Missing quotes around variables

Syntax error

Always quote variables in JSON or form data

Incorrect endpoint URL

Typo or extra slashes

Double-check URL syntax and test requests

Forgetting to save sample data

Testing with empty data

Save response samples after successful API calls

Not handling error responses

Missing conditional routes

Create routes for status codes like 401, 500

Failing to provide test values

No test data in variables

Input test values before testing requests

Summary Table: External Request Workflow

Step

Action

Key Points

1

Access external request setup

Navigate to action step > advanced > external requests

2

Configure method and endpoint

Choose method, input URL carefully

3

Set headers

Authorization, Content-Type, use variables if needed

4

Test request

Verify connectivity and response data

5

Map response data

Use JSON path, save sample data

6

Loop through data

Use "for each" for multiple items

7

Handle errors

Create routes based on status codes

8

Use fallback values

Prevent errors with missing data

Final Thoughts

External requests unlock vast potential for chatbot customization and integration.

By understanding setup procedures, response handling, error management, and best practices, developers can craft intelligent, responsive bots capable of interacting with a multitude of third-party services. Whether fetching product data, sending cross-channel messages, or processing payments, mastering external requests is a cornerstone skill in modern chatbot development.