How to setup Time-based routing for Smart Agent Chat?
- Getting Started
- Bot Building
- Smart Agent Chat
- Conversation Design
-
Developer Guides
Code Step Integration Static Step Integration Shopify Integration SETU Integration Exotel Integration CIBIL integration Freshdesk KMS Integration PayU Integration Zendesk Guide Integration Twilio Integration Razorpay Integration LeadSquared Integration USU(Unymira) Integration Helo(VivaConnect) Integration Salesforce KMS Integration Stripe Integration PayPal Integration CleverTap Integration Fynd Integration HubSpot Integration Magento Integration WooCommerce Integration Microsoft Dynamics 365 Integration
- Deployment
- External Agent Tool Setup
- Analytics & Reporting
- Notifications
- Commerce Plus
- Troubleshooting Guides
- Release Notes
Table of Contents
Understanding Time-based RoutingImplementation Code Step Integration for Time-based routingHow does the above code work for Time-based Routing?Understanding Time-based Routing
Consider, a user has requested to chat with an agent. According to the Time-based Routing algorithm, it would first check if the user's request falls in the time range when the agents are online.
You can add a condition, depending on your agents' working hours. If the user's request falls in working hours, the bot would direct the chat to the agent, else it would respond with "agent is offline".
Implementation
In order to implement this Team-based routing algorithm, you would have to follow certain steps.
- In your Bot, you would have to add a Code step.
- This code step would serve the user's Chat with an Agent request.
- You can add the below-mentioned code in that code step.
- The code would get implemented every time the user request for chatting with an agent.
Code Step Integration for Time-based routing
import json import logging import pytz from datetime import datetime from datetime import time logger = logging.getLogger() logger.setLevel(logging.DEBUG) def main(event, context): body = json.loads(event['body']) # set the start and end time startTime="09:00 AM" initialTime=datetime.strptime(startTime, '%I:%M %p') endTime="06:00 PM" finalTime=datetime.strptime(endTime, '%I:%M %p') # check the actual time present = datetime.now(pytz.timezone('America/New_York')) # Mention the time zone here present_time =present.strftime('%I:%M %p') final_present_time=datetime.strptime(present_time,'%I:%M %p') # condition to route the user if initialTime<= final_present_time <=finalTime: status = "agent_online" else: status = "agent_offline" final_response = { 'status': status } logger.info(f"Final Response Logged is {final_response}") response = {'statusCode': 200, 'body': json.dumps(final_response), 'headers': {'Content-Type': 'application/json'}} return response
How does the above code work for Time-based Routing?
The above code uses two main libraries of Python:
pytz(python timezone)
Datetime
The datetime library here uses a method called strptime, that checks the user’s actual time. By actual time, it means that it captures the system’s time.
When the user asks to chat with an agent, the algorithm would initially check the user's system time and would record it.
Also, a Start time, and an End time is defined in the code.
startTime="09:00 AM"
initialTime=datetime.strptime(startTime, '%I:%M %p')
endTime="06:00 PM"
finalTime=datetime.strptime(endTime, '%I:%M %p')
Here, as you can see, the start time is defined as 09:00 AM, whereas the End time is defined as 06:00 PM, which is their working hours.
Also, here initialTime and finalTime function are recording the time, in the string format.
The next would be pytz.timezone, where the code would define the timezone in the city that is mentioned.
In this case, America/New York has been entered, so this part of the code would ideally record the timezone details in New York, America, irrespective of what time it is.
present = datetime.now(pytz.timezone('America/New_York'))
The next function here would record the present time in New York. It will store this data for further evaluations. The time would be recorded in the string format here.
present_time = present.strftime('%I:%M %p')
Final_present_time = datetime.strptime(present_time,'%I:%M %p')
Here, it also records the Final present time. It would take the time data from the above function and would parse it into the format, which is mentioned in the brackets.
In this case: %I: %M: %p
There is a conditional loop that has been added in the code. It is as follows:
if initialTime<= final_present_time <=finalTime:
status = "agent_online"
else:
status = "agent_offline"
final_response = {
'status': status
}
Here, the logic is quite simple, it says, if the initial time is less than or equal to the final present time, and if that is less than or equal to the final time, which is 06:00 PM, then the status of the agent would be agent_online.
If in any case, the above condition is not met, the status of the agent would be shown as agent_offline.
Let's take an example here, consider you are in London and it is 12:00 AM there, and you have put up a query on the bot. So in this case, considering the time zones, the time at New York would be 07:00 PM.
In the above scenario, your time clearly doesn’t match the time constraint mentioned, which is 09:00 AM to 06:00 PM, which is why you would receive an agent status to be agent_offline.