repo: https://github.com/signalwire/signalwire-guides/tree/master/code/execute_code_in_business_hours - language:python - sdk:compatibility - product:voice 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](/guides/signing-up-for-a-space). We will also be utilizing Pythons Flask framework. To learn more about Flask, check [here](https://flask.palletsprojects.com/en/2.0.x/). We also have the full repo for this application on our [GitHub](https://github.com/signalwire/signalwire-guides/tree/master/code/execute_code_in_business_hours). Configuring the code -- What we want to demonstrate in this example is how you can use the [SignalWire Compatibility APIs](/rest/compatibility-api/endpoints/list-accounts) 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 [here](/compatibility-api/cxml/voice/dial)! 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! python @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 +12342556182 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](https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568). 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 its the correct time zone. We also need to create two more datetime objects to represent the businesss opening and closing time (9AM and 8PM respectively here). The year, month, and date dont 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 its 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 to go to the Open route and dial the Were 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 to go to the Closed route and dial the Were 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. python @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 were open!) else: response.redirect(url=/closed) print(Time Now is: + str(timeNow.time())) print(That means were closed!) return response.to_xml() The corresponding XML for the above route is very simple - its only a redirect to the /open route. xml /open Running the application -- You will need the Flask framework and the SignalWire [Python SDK](/compatibility-api/sdks) 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, [create a SignalWire account and Space](https://m.signalwire.com/signups/new?s=1). Please feel free to reach out to us on our Community Slack[here](http://signalwire.community/) or create a Support ticket if you need guidance!