Execute Code during Business Hours Only - Python
This short and sweet application will show you how you can use time intervals in order to perform one action during business hours and another action outside of business hours. We will keep it simple and show how you could dial two different numbers depending on the time. However, you can easily replace the code in the 'open hours' and 'closed hours' flask routes with your own current call flow.
What Do You Need to Run this Code?
Just like all SignalWire applications, you will need your API credentials (SignalWire Space URL, Project ID, and API Token) from your SignalWire Space. You can find these by looking at the API tab of your SignalWire Space! Please check out Creating a SignalWire Space. We will also be utilizing Python's Flask framework. To learn more about Flask, check here. We also have the full repo for this application on our GitHub.
Configuring the code
What we want to demonstrate in this example is how you can use the SignalWire Compatibility APIs in conjunction with any call structure in order to perform different actions depending on the time of day and your preferred business hours. To start, we need to create a route for handling calls during business hours and a route for handling calls outside of business hours. These two routes are essentially identical, so they will both be showed together below!
In these routes, we instantiate VoiceResponse()
as response
and then start a dial
with record=true
. This will make sure that the call will start recording once connected. Next, we use dial.number()
to input the phone number that we would like to route to. We will dial +12342556182
when our business is open and +13373820859
when our business is closed. Read more about <Dial>
here! These numbers will play a simple message such as "We are closed" or "We are open". Feel free to test this application by dialing them yourself!
@app.route('/open', methods=['GET', 'POST'])
def inBusinessHours():
response = VoiceResponse()
dial = Dial(record='true')
dial.number('+12342556182')
response.append(dial)
return response.to_xml()
@app.route('/closed', methods=['GET', 'POST'])
def offBusinessHours():
response = VoiceResponse()
dial = Dial(record='true')
dial.number('+13373820859')
response.append(dial)
return response.to_xml()
The corresponding XML for the open route (identical to the closing route save for the number) looks like this:
<?xml version="1.0" encoding="UTF-8"?><Response><Dial record="true"><Number>+12342556182</Number></Dial></Response>
Next, we need to create a route that will respond to incoming calls and check the current time. We instantiate VoiceResponse()
first just as we did in the previous route. We will define the timezone here as US/Eastern
, however, you can change this timezone to whichever one you prefer. You can view the list of Pytz timezones here.
We can use datetime.now()
to get a datetime object at the time of the call, and use tz
as the argument to make sure it's the correct time zone. We also need to create two more datetime objects to represent the business's opening and closing time (9AM and 8PM respectively here). The year, month, and date don't actually matter here - we will only be using the time component of these objects.
When a call comes in, we use the time component of timeNow
to see if it's later than opening
Time and earlier than closing
Time. If the current time is between business hours, we will move on to the next check to see if the day of the week is also between Monday and Friday. We can use the datetime.isoweekday()
functionality to see if the current day of the week is between Monday and Friday. If both of these conditions are satisfied, we will use use <Redirect>
to go to the Open route and dial the 'We're Open' number. You can replace the code within the open route with what you currently do when incoming calls come in.
If either one of the conditions are not satisfied, we will use <Redirect>
to go to the Closed route and dial the 'We're Closed' number. You can replace the code in the closed route with what you would like for customers to hear and do when they dial your number during non business hours.
@app.route('/checkTime', methods=['GET', 'POST'])
def checkTime():
response = VoiceResponse()
tz = timezone('US/Eastern')
timeNow = datetime.now(tz)
opening = datetime(2010, 1, 1, 9, 0, 0) # 9AM
closing = datetime(2010, 1, 1, 20, 0, 0) # 8PM
# check if between hours of 9AM-8PM
if opening.time() < timeNow.time() < closing.time():
# check if M-F
if timeNow.isoweekday() in range(1, 6):
response.redirect(url='/open')
print('Time Now is: ' + str(timeNow.time()))
print("That means we're open!")
else:
response.redirect(url='/closed')
print('Time Now is: ' + str(timeNow.time()))
print("That means we're closed!")
return response.to_xml()
The corresponding XML for the above route is very simple - it's only a redirect to the /open
route.
<?xml version="1.0" encoding="UTF-8"?><Response><Redirect>/open</Redirect></Response>
Running the application
You will need the Flask framework and the SignalWire Python SDK downloaded.
To run the application, execute export FLASK_APP=app.py
then run flask run
.
How to use the application
To use this script, you need to expose it to the web (through a server or SSH tunnel) and use it as a webhook for handling incoming calls under phone number settings. For this script, you would use the server url and the checkTime
route, like this http://myServer.fakeserver.com/checkTime
Sign Up Here
If you would like to test this example out, you can create a SignalWire account and space here.
Please feel free to reach out to us on our Community Slackhere or create a Support ticket if you need guidance!