in dotnet/create-a-chatbot/RemindMeBot/RemindMeBotHandler.cs [40:77]
private async Task HandleRemindAsync(MessagePayload payload, ChatMessageText messageText)
{
var arguments = messageText.Text.Split(' ', StringSplitOptions.TrimEntries);
if (arguments.Length != 2
|| !int.TryParse(arguments[1], out var delayInSeconds))
{
// We're expecting 2 elements: "remind", "X"
// If that's not the case, return help.
await HandleHelpAsync(payload);
return;
}
await _chatClient.Messages.SendMessageAsync(
recipient: MessageRecipient.Member(ProfileIdentifier.Id(payload.UserId)),
content: ChatMessage.Block(
outline: new MessageOutline($"I will remind you in {delayInSeconds} seconds", new ApiIcon("smile")),
sections: new List<MessageSectionElement>()));
Task.Run(async () =>
{
try
{
await Task.Delay(TimeSpan.FromSeconds(delayInSeconds));
await _chatClient.Messages.SendMessageAsync(
recipient: MessageRecipient.Member(ProfileIdentifier.Id(payload.UserId)),
content: ChatMessage.Block(
outline: new MessageOutline($"Hey! {delayInSeconds} seconds are over!", new ApiIcon("smile")),
sections: new List<MessageSectionElement>()));
}
catch (Exception)
{
// Since we're using Task.Run to run code outside of the
// request context, we want to catch any Exception here
// to prevent the server from crashing.
}
});
}