1. Webchat
  2. Webchat template default context

Webchat

Webchat template default context

Setting the default context for handling webchat messages

What is the default context used for?

  • Handle interactions between the user and other systems in stubber
  • Reacting and responding to the user's incoming messages
  • The default context has to reply to the user so that a stubsession is created between the new stub and the webchat session

Navigate to default context and click on Edit Flow

Replying to a users first message

  • Create a state called pending-message. This will be the state that the stub will be in when replying to the message
  • Add an action called reply. This action will send a message to the webchat client.
  • Add an action called _create. This action runs when the stub is created. We want to override this action so that the new stubs first action is replying. You can trigger any actions provided you send a reply.
  • Add an action called _update_from_webchat. This action will be used to trigger a reply to the webchat when a user sends a message

image

  • Data for the webchat client will be present at stub.data._incoming_webchat_data
  • In the reply action add the Webchat notification to session from the library. change webchat.sessionuuid value to ~~stub.data._incoming_webchat_data.sessionuuid. This will allow the notification to send a message to the correct client.
        "platforms": {
  "webchat": {
    "sessionuuid": "~~stub.data._incoming_webchat_data.sessionuuid",
    "webchat_message": {
      "type": "markdown",
      "value": "~~stubpost.message"
    },
    "stubsession": {
      "set_new_with_timeout_hours": 24
    }
  }
}

      
  • In the _create action add the task Send action from the library. Change the params.action_name to reply. This is to trigger a reply when the new stub is created. Once the reply is sent, a stubsession will be created between the new stub and the webchat client allowing all future messages to be routed into the action _update_from_webchat.

  • Change the params.message to the value A stub has been created. This is so that we can identify what action triggered the reply

        {
  "tasktype": "sendaction",
  "params": {
    "action_name": "reply",
    "message": "A stub has been created",
    "stubref": "~~stub.stubref"
  },
  "name": "sendaction",
  "__key": "sendaction"
}

      

image

Replying to a users future messages

  • To run actions when a user send a message we need to add the task Send action to _update_from_webchat.
  • Change the params.message to the value Conversation stub is now handling all future replies. This is so that we can identify what action triggered the reply
        {
  "tasktype": "sendaction",
  "params": {
    "action_name": "reply",
    "message": "Conversation stub is now handling all future replies",
    "stubref": "~~stub.stubref"
  },
  "name": "sendaction",
  "__key": "sendaction"
}

      
  • Now all future messages will be handled by the conversation stub as seen below

image