使用"YourEasyBot"创建机器人。我有一个"睡觉"。线程使用Task. delay(),但我需要检查,如果客户端发送消息,而任务是延迟的,机器人必须回答"请等待。您的订单正在处理中
async Task ProceedOrder()
{
Message msg = new();
msg = await Telegram.SendTextMessageAsync(chat, "Please, wait. Your order is proceedingnnIt won't take more than 3 minutes");
long time = new Random().NextInt64(10, 181);
await Task.Delay(TimeSpan.FromSeconds(Convert.ToDouble(time)));
if(await NewTextMessage(update) != null){
await Telegram.SendTextMessageAsync(chat, "Please, wait. Your order is proceeding");
}
await Telegram.DeleteMessageAsync(chat, msg.MessageId);
msg = await Telegram.SendTextMessageAsync(chat, "Done");
}
我输入一个文本,它回答"请等待"。您的订单正在处理中。只有在任务结束后。推迟完成。但我需要检查NewTextMessage所有的时间任务。拖延是有效的。我该怎么做呢?
期望的对话框是:,请等待。您的订单正在进行中。不会超过3分钟的
- 只是一些文本从客户端而任务。延迟是有效的,请等待。您的订单正在进行中
- 另一个文本,请等待。您的订单正在进行中
你的意思是,你想检查多个任务。延迟任务或newMessage.
我还没有测试它,(它已经晚了),它可能包含打字错误,看到它;我可以给你指出正确的方向。您可以尝试这样做:
async Task ProceedOrder()
{
Message msg = new();
msg = await Telegram.SendTextMessageAsync(chat, "Please, wait. Your order is proceedingnnIt won't take more than 3 minutes");
long time = new Random().NextInt64(10, 181);
// instead of await the task here, store the task in a variable.
var delayTask = Task.Delay(TimeSpan.FromSeconds(Convert.ToDouble(time)));
while(true)
{
// create a new task to receive a new TextMessage
var newMessageTask = NewTextMessage(update);
// if any of the tasks is completed, return from the Task.WhenAny.
// the return value is the task that is completed.
if(await Task.WhenAny(delayTask, newMessageTask) == delayTask)
// if it was the delayTask, that means time is passed by,
// break out of the while loop
break;
// otherwise, the completed task is the newMessageTask
// send the chatmessage
await Telegram.SendTextMessageAsync(chat, "Please, wait. Your order is proceeding");
}
// The delay-time has passed, end it..
await Telegram.DeleteMessageAsync(chat, msg.MessageId);
msg = await Telegram.SendTextMessageAsync(chat, "Done");
}