Hi Im trying to get this working properly. I'd like the open time to be set between 14:00 UTC and 22:00 UTC. But it doesn't seem to work.
When I call the function url I get a return of "open" when I call it outside of the open times.
Can anyone help me with this? Thanks.
exports.handler = function(context, event, callback) {
const moment = require('moment');
let callerId = event.Caller; // || "+1-000-000-0000"; // default caller ID
let twiml = new Twilio.twiml.VoiceResponse();
if ((moment().hour() >= 14 || moment().hour() < 22) && moment().isoWeekday() <= 5) {
twiml.say("Open");
} else {
twiml.say("Closed");
}
twiml.redirect("http://twimlets.com/voicemail?Email=eeemail#email.com&Message=Please%20leave%20a%20message.&Transcribe=true");
callback(null, twiml);
};
Your code sample you have the hour 17 not 14, anyway, this thing:
(moment().hour() >= 17 || moment().hour() < 22)
will always return true, change it to:
(moment().hour() >= 17 && moment().hour() < 22)
to get the time between 5 PM and 10 PM
Related
I only want to disable the days of the week that I choose, or otherwise enable the days that I need
here is an illustrative image
I found this code in the documentation but it doesn't work for me
() => {
const [startDate, setStartDate] = useState(null);
const isWeekday = (date) => {
const day = getDay(date);
return day !== 0 && day !== 6;
};
return (
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
filterDate={isWeekday}
placeholderText="Select a weekday"
/>
);
};
You can change the day of the week to disable in this example from the docs on the line return day !== 0 && day !== 6; by changing the last number.
For instance, changing day !== 6 to day !== 7 will exclude only Sundays, etc.
If you want to exclude multiple days of the week, add && day !== [day number] to the callback. For instance day !== 0 && day !== 2 && day !== 5; will exclude Sundays, Tuesdays & Fridays.
I'm trying to route calls to different agents based on time of day using Twilio Studio referencing the following function and wondering if it's correct? I'm not a programmer, so this is adapted from Need help creating a Time Gate in Twilio Function
// Time of Day Routing
// Useful for IVR logic, for Example in Studio, to determine which path to route to
// Add moment-timezone 0.5.31 as a dependency under Functions Global Config, Dependencies
const moment = require('moment-timezone');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
function businessHours() {
// My timezone East Coast (other choices: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
const now = moment().tz('America/Denver');
// Weekday Check using moment().isoWeekday()
// Monday = 1, Tuesday = 2 ... Sunday = 7
if(now.isoWeekday() == 1 || 3 || 5 /* Check for Normal Work Week Monday - Friday */) {
//Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
if((now.hour() >= 8 && now.hour() < 9:30) || (now.hour() >= 12 && now.hour() < 17) /* 24h basis */) {
return true
}
}
if(now.isoWeekday() == 2 /* Check for Normal Work Week Monday - Friday */) {
//Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
if((now.hour() >= 8:30 && now.hour() < 11) /* 24h basis */) {
return true
}
}
if(now.isoWeekday() == 4 /* Check for Normal Work Week Monday - Friday */) {
//Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
if((now.hour() >= 8 && now.hour() < 10:30) || (now.hour() >= 15 && now.hour() < 17) /* 24h basis */) {
return true
}
}
// Outside of business hours, return false
return false
};
const isOpen = businessHours();
if (isOpen) {
twiml.say("Business is Open");
} else {
twiml.say("Business is Closed");
}
callback(null, twiml);
};
Twilio developer evangelist here.
Stack Overflow is not the best place to ask "is this correct?". It's much better to come with an actual problem that you have and a description of the things you have tried to fix that problem. It's also hard to answer "is this correct?" if we don't know the outcome you actually want.
However, I can see one issue with the code above and that is dealing with working hours that fall outside of just on the hour tests.
now.hour() will return a whole number that is the current hour. You cannot compare this to 9:30 for example. Instead, we have to look at both the hours and the minutes,
In your first conditional hour check you have:
if((now.hour() >= 8 && now.hour() < 9:30) || (now.hour() >= 12 && now.hour() < 17) /* 24h basis */) {
return true
}
In words, you seem to be going for: If the time is between 8am and 9:30am or the time is between 12pm and 5pm then return true.
To cope with the time 9:30 we have to check that the time is between 8am and 10am and if it is some number of minutes past 9, that number of minutes is not more than 30. Just to cut down the code we are looking at, the issues is this predicate:
(now.hour() >= 8 && now.hour() < 9:30)
We could replace this with:
((now.hour() >= 8 && now.hour() < 9) || (now.hour() == 9 && now.minute() < 30))
This now tests that the hour is more than or equal to 8 and less than 9 OR that the hour is equal to 9 and the minutes are less than 30.
Since "greater than or equal to 8 and less than 9" is effectively the same as "equal to 8" we could shorten this to:
(now.hour() === 8 || (now.hour() == 9 && now.minute() < 30))
But you will want to use the full version when you want to fix further comparisons, like between 8:30 and 11 or between 8 and 10:30.
Hopefully this gives you a good idea of how to approach all your time comparisons.
I have made an app using ionic.
Inside it, I make a http get to get some value from my db.
Then, I loop through the result to compute the difference in terms of time between the created date of my value inside the db and the actual date.
This code works very well on my linux but when I run the code on my iphone on ios, everything works fine except the for loop which does not work... it's very strange. Someone faced it ?
ionViewWillEnter() {
let date = new Date();
this.http.get(this.server + `json.php?&id=${1}`).subscribe(resData => {
console.log(resData);
this.events = resData;
for (let i=0; i < 10; i++){
this.events[i]['unique_id'] = parseInt(this.events[i]['unique_id']);
if ((date.getTime() - new Date(this.events[i].created_at).getTime()) / 1000 < 60) {
this.events[i].ecart_now = 'there is ' + Math.round((date.getTime() - new Date(this.events[i].created_at).getTime()) / 1000) + 's';
}
}
})
}
A high possibility is the invalid date format. Safari and Internet Explorer browsers have issue with date format: new Date("2011-02-07");.
Console log new Date(this.events[i].created_at) and you would see invalid date.
Use below formats:
new Date(2011, 01, 07);
new Date(2011, 01, 07, 11, 05, 00);
I need to get a call forwarding number in place so that outside of uk office hours all incoming calls redirect to our out of hours service.
I've written this which returns a $status = 'closed';
<?php
// set the timezone
date_default_timezone_set ('Europe/London');
function checkDateValue($val, $args) {
if (($val) >= $args['min'] && ($val) <= $args['max'] ) :
return true;
else :
return false;
endif ;
}
// set min and max valuesfor hours, minutes and seconds - format HH:MM:SS
$hours = array(
'min' => '09:00:00',
'max' => '17:30:00'
);
// set min and max values bwtween 0 nd 6. 0 = sunday
$days = array(
'min' => 1,
'max' => 5
);
// store current time
$currentTime = time();
// test if the current time is in opening hours or is a weekday
$status = 'open';
if (checkDateValue(date('H:i:s', $currentTime), $hours) === false || checkDateValue(date('w', $currentTime), $days) === false) {
$status = 'closed';
}
I'm wondering if there is anything in the the php-sdk or in twiml that can handle conditional dialing based on detecting the time of day and day of the week and that also accounts for current callers timezone.
Thanks.
Twilio developer evangelist here.
There's nothing within Twilio's PHP SDK or TwiML that will do this time detection for you, you will need to write your own method (as you have done) to detect the current time and then use that to return different TwiML to perform the in hours or out of hours response.
So, you could add to your current script something like:
use Twilio\TwiML;
$response = new TwiML;
if ($status == 'closed') {
$response->say("Sorry, the office is closed right now");
} else {
$response->dial($officePhone);
}
echo $response;
I'm not sure why you would need to account for the current caller's time zone, your UK hours won't change if someone calls from France. Perhaps you could comment or update your question with a bit more detail. Otherwise, hope this helps.
How can I check within my Rails app what datetime format the user currently uses as his default?
I have this method:
def local_date(date, am_pm = false)
unless am_pm
date&.localtime&.strftime('(%d.%m.%Y, %H:%M)')
else
date&.localtime&.strftime('(%d.%m.%Y, %I:%M %p)')
end
end
I need to set am_pm accordingly to users local machines datetime format WITHOUT relying on the :locale parameter as not everyone who speaks english uses am/pm
This is achievable in Rails only with the help of a bit of client side JavaScript code. The client side code would detect whether the user is using 24 hours time format or 12 hours time format, and then store that information of a cookie.
Your server side code should then read that information from the cookie and set your time format accordingly.
Add this to your app/assets/javascript/application.js file.
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
var dateString = date.toLocaleTimeString();
//apparently toLocaleTimeString() has a bug in Chrome. toString() however returns 12/24 hour formats. If one of two contains AM/PM execute 12 hour coding.
if (dateString.match(/am|pm/i) || date.toString().match(/am|pm/i) )
{
//12 hour clock
//check if we are already rendering in 12 hours format
if(getCookie("time_format") != "twelve")
{
document.cookie = "time_format=twelve";
/***
Now force the browser to reload current page from server.
Since we had set the the cookie, the server will now render
all pages in 12 hours format
****/
location.reload(true).
}
}
else
{
//24 hour clock
document.cookie = "time_format=twenty_four";
}
In your ApplicationController
class SomeController < ApplicationController
around_faction :set_time_format
def set_time_format
if cookie[:time_format]=="twelve"
#Set your desired time format string with 12 hour style
else
#default
#Set your desired time format string with 24 hour style
end
end
end