我正在使用C#和Twilio API构建文本应用程序。我正在尝试让客户在下午6点之后发短信时自动回复,但我不知道该怎么做,我正在使用DateTime来设置打开和关闭的时间,但是我不知道之后如何继续。预先谢谢你
namespace Mercury.Controllers
{
class ClosingHoursController : SmsController
{
DateTime now = DateTime.Now;
DateTime start = new DateTime(8, 0, 0);
DateTime close = new DateTime(18, 0, 0);
}
}
答案 0 :(得分:0)
您可以定义今天以及一些类似的时间:
var sixAM = DateTime.Today.AddHours(6);
var sixPM = DateTime.Today.AddHours(18);
或者仅检查要检查的DateTime的Hour
属性。
bool IsWorkingHours(DateTime theDate)
{
return (theDate.Hour > 6 && theDate.Hour < 18);
}
答案 1 :(得分:0)
检查消息时间是否在您的营业时间之间。在类中初始化的DateTime.Now会在您初始化时保持设置,因此最好在需要时调用方法。
public void ReceiveMessage(object smsMessage)
{
if (AutoReply(smsMessage))
{
//Send the office closed message
}
else
{
//Continue as normal
}
}
public bool AutoReply(object smsMessage)
{
bool autoReply = true;
DateTime msgTime = smsMessage.MessageTime; //This is when your message came in - i'm assuming it will be a DateTime
TimeSpan openTime = new TimeSpan(08, 0, 0); //This is when your office opens - i have put 08:00
TimeSpan closeTime = new TimeSpan(17, 0, 0); //....and this is when it closes - i have put 17:00
if (msgTime.TimeOfDay > openTime && msgTime.TimeOfDay < closeTime) autoReply = false; //Set to true if we're between opening hours
return autoReply;
}