Chatbot Endpoints#

These endpoints are designed for WebSocket connections to support communication with the chatbot and better engagements to the checkout.

  • /ws/chat/<report_id>
  • /ws/chat-success/<report_id>

Engagement Communication#

/ws/chat/<report_id>#

When the user chooses one of the astrological reports, he starts communicating with the chatbot about the chosen report. The chatbot sends a chunk of messages with 2-second intervals, and then it listens for the user's response.

The dialogue is preprogrammed - the chatbot engages the user step-by-step to make a purchase. Once the user comes to the purchase, the WebSocket connection closes. It signals the frontend app to start the checkout process.

Connect#

The report_id slug in the /ws/chat/<report_id> URL is exactly the same as described in the table in the Stripe payment API.

The WebSocket endpoint checks if the report_id is correct and establishes the connection in case of success. If the report_id is invalid, it sends the {"error": "Invalid Url"} JSON string in response and closes the connection.

Request/Update#

Once the connection is open it waits for the first user input. The frontend app sends the first message to initiate the chat. The mandatory fields in the message JSON string are:

Param type Comment
lang str Now only three language are supported - ru, en, pl
is_new_visitor bool Required only for the first message. If user has never engagement with chatbot befor, he will receive a slightly different greetings. Your frontend app can easily distinguish the type of visitor by saving the flag in browser's/app local storage
price float The price of the report as it will be shown in the dialogue. Required for the first message.
currency string The currency of the report in genitive as it will be shown in dialogue. f.e. "250 рублей". Required for the first message.

The example format for the message JSON string: {"lang": "ru", "is_new_visitor": false}.

If the message has the wrong format (not a JSON string), the endpoint sends an error message and closes the connection.

Response#

Once the user sends a message, the endpoint responds with a series of messages in the following format:

{"message": "Hi!", "status": "sending"}
{"message": "I am your virtual astrologer", "status": "sending"}
{"message": "Hope you are doing great today", "status": "sending"}
{"message": "You wanted to know when you will find a new job, right?", "status": "waiting"}

The status field may have the following values:

  • sending means the chat bot is still sending a chunk of messages and doesn't expect any updates from the client side.
  • waiting - the chatbot has finished its part and is waiting for the client to send next message to respond to.

Warning

Important. Plese keep the same format of the client's messages along the whole conversation.

You can test the conversation on this demo page: /api/chatbot-demo/engagement/<report_id>/

Post-Payment Communication#

/ws/chat-success/<report_id>

Connect#

The report_id slug in the /ws/chat/<report_id> URL is exactly the same as described in the table in the Stripe payment API.

The WebSocket endpoint checks if the report_id is correct and establishes the connection in case of success. If the report_id is invalid, it sends the {"error": "Invalid Url"} JSON string in response and closes the connection.

Request/Update#

Once the connection is open it waits for the client input. The front app sends the first message to initiate the chat.

A message from the client has the following fields:

Param type Comment
success bool true for successful payments.
report_url_params See note below
lang str Now only three language are supported - ru, en, pl

Note: After a successful payment, your app receives the data for the astrological report in the form of key-value URL parameters, e.g., date=2023-10-8&time=12:44&tz=2&ln_d=37&ln_m=37&dir_ln=e&lt_d=55&lt_m=45&dir_lt=n&hash=XXX&report_id=XXX. (see here and here). This string is mandatory only for successful payment. Give this string as part of yur message.

Example of messages that the chatbot expects from the client:

JSON string:

{
    "success": true,
    "lang": "en",
    "report_url_params": "date=2023-9-24&..."
}

or

{
    "success": false,
    "lang": "en"
}

Response#

This endpoins sends a series of messages in responce and closes the connection. Example:

{"message":"Thank you for the payment"}
{"message":"Here is your link with a detailed answer","report_url":"/api/reading/newjob/?datetime=2023-12-03"}
{"message":"Save it for the future"}
{"message":"After that you can leave the chat and look at other questions"}
{"message":"Perhaps I can help with something else"}
{"message":"Come back to me again. I'm your pocket astrologer"}

In case of message with URL in encloses the relative path to the report in a separate field report_url. Your frontend app wll the wrap it to HTML presentation in a chat window.

You can check the chatbot behaviour after success payment in this demo api http://localhost/api/chatbot-demo/success/<report_id>/