在指定时间显示消息 [英] Show message in specified time
问题描述
我正在制作一个电报 C# 机器人,我想要我的机器人锁定组,我使用时间跨度来完成它和它的工作,但我想要在锁定组向用户显示消息前五分钟,但我的机器人没有自动发送任何消息.我是初学者,在网上找不到任何帮助.
I am making a telegram C# bot and I want my bot lock group and I use timespan for this and its work but I want exactly five minutes before lock group shows a message for users but my bot didn't send any messages automatically. I am so beginner and didn't find any thing online for help.
感谢您的帮助.
TimeSpan Start = new TimeSpan(20,0,0);
TimeSpan End = new TimeSpan(21,0,0);
TimeSpan now = DateTime.Now.TimeOfDay;
TimeSpan risingStart1 = new TimeSpan(19, 55, 0);
if (now == risingStart1)
{
Bot.SendTextMessageAsync(e.Message.Chat.Id , "Group will lock for 5 min.");
}
else if (Start < now && now < End)
{
Bot.DeleteMessageAsync(e.Message.Chat.Id, e.Message.MessageId);
}
PS:锁定组的时间跨度工作.
PS: Timespan work for locking group.
推荐答案
检查这是否有帮助:
public class Repeater
{
private readonly List<DateTime> _checkpoints;
private readonly Action _action;
private readonly Func<Task> _asyncAction;
private readonly Timer _timer;
private Thread _processingThread;
public Repeater(List<DateTime> checkpoints, Action action)
{
_checkpoints = checkpoints;
_timer = new Timer
{
AutoReset = false,
Enabled = false
};
_timer.Elapsed += TimerCheckpoint_Elapsed;
_timer.Elapsed += CalculateNextExecution;
_action = action;
_asyncAction = null;
}
public Repeater(List<DateTime> checkpoints, Func<Task> action)
{
_checkpoints = checkpoints;
_timer = new Timer
{
AutoReset = false,
Enabled = false
};
_timer.Elapsed += TimerCheckpoint_Elapsed;
_timer.Elapsed += CalculateNextExecution;
_asyncAction = action;
_action = null;
}
public void Start()
{
ReinitializeTimer();
}
public void Stop()
{
//_thrProcessaCheckpoints?.Abort();
_timer?.Stop();
_timer?.Dispose();
}
private void TimerCheckpoint_Elapsed(object sender, ElapsedEventArgs e)
{
if (_processingThread != null && _processingThread.IsAlive) return;
_processingThread = new Thread(async () => await ThreadStart())
{
IsBackground = true,
Name = "Timer Processing Thread"
};
try { _processingThread.Start(); } catch { /* ignored */ }
}
private async Task ThreadStart()
{
if (_asyncAction == null)
_action();
else
await _asyncAction();
}
private void CalculateNextExecution(object sender, ElapsedEventArgs e)
{
ReinitializeTimer();
}
private void ReinitializeTimer()
{
_timer.Interval = CalculateNextInterval(_checkpoints);
_timer.Start();
}
private static double CalculateNextInterval(List<DateTime> checkpoints)
{
double min = double.MaxValue;
var dateTime = DateTime.Now;
// Calculate how much time is left to the next checkpoint
foreach (var date in checkpoints)
{
TimeSpan timeForNextCheckpoint;
// If it's in the same time, calculate how much time is left
if (date.TimeOfDay > dateTime.TimeOfDay)
timeForNextCheckpoint = date.TimeOfDay - dateTime.TimeOfDay;
// If not, calculate how much time is left, counting a day
else
timeForNextCheckpoint = TimeSpan.FromHours(24) - (dateTime.TimeOfDay - date.TimeOfDay);
if (Math.Abs(timeForNextCheckpoint.TotalMilliseconds) < min)
{
min = Math.Abs(timeForNextCheckpoint.TotalMilliseconds);
}
}
return min;
}
}
用法:
var myRepeater = new Repeater(new List<DateTime>{ new DateTime(2020,19,11, 19, 55, 00) }, () => MyMethodCall());
myRepeater.Start(); // call on your application start
myRepeater.Stop(); // call on your application stop
您只需要提供一个日期时间列表,其中包含您的任务需要执行的时间,以及一个将在该时间执行的方法.构造函数还有一个重载,可以在您需要时接收异步方法.
You just need to provide a list of datetimes containing the hours that your task needs to be executed, and a method that will be executed at such time. The constructor also have an overload that receives an async method should you need it.
var myAsyncRepeater = new Repeater(_cfg.ConfiguracaoBaixaAdiamentos.Checkpoints,
async () => await MyMethodCallAsync());
这篇关于在指定时间显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!