public async Task ExecuteAsync(object sender, MessageEventArgs e) { if (!string.IsNullOrEmpty(e.Message.Text)) { if (e.Message.Text.Length >= 4 && e.Message.Text.Substring(0, 3).Equals("搜索 ")) { var firstSearch = new SearchOption() { Search = e.Message.Text.Substring(3), ChatId = e.Message.Chat.Id, IsGroup = e.Message.Chat.Id < 0, Skip = 0, Take = 20, Count = -1, ToDelete = new List <long>(), ToDeleteNow = false, ReplyToMessageId = e.Message.MessageId, Chat = e.Message.Chat }; var searchOption = await sonicSearchService.Search(firstSearch); if (searchOption.Messages.Count == 0) { searchOption = await searchService.Search(firstSearch); } await sendService.ExecuteAsync(searchOption, searchOption.Messages); } } }
public async Task ExecuteAsync(object sender, CallbackQueryEventArgs e) { //Console.WriteLine(e.CallbackQuery.Message.Text); //Console.WriteLine(e.CallbackQuery.Id); //Console.WriteLine(e.CallbackQuery.Data);//这才是关键的东西,就是上面在按钮上写的那个sendmessage var ChatId = e.CallbackQuery.Message.Chat.Id; var IsGroup = e.CallbackQuery.Message.Chat.Id < 0; await botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id, "搜索中。。。"); try { var searchOption = JsonConvert.DeserializeObject <SearchOption>(Encoding.UTF8.GetString(await Cache.GetAsync(e.CallbackQuery.Data))); await Cache.RemoveAsync(e.CallbackQuery.Data); searchOption.ToDelete.Add(e.CallbackQuery.Message.MessageId); searchOption.ReplyToMessageId = e.CallbackQuery.Message.MessageId; searchOption.Chat = e.CallbackQuery.Message.Chat; if (searchOption.ToDeleteNow) { foreach (var i in searchOption.ToDelete) { await Send.AddTask(async() => { try { await botClient.DeleteMessageAsync(ChatId, (int)i); } catch (AggregateException) { logger.LogError("删除了不存在的消息"); } }, IsGroup); } return; } var searchOptionNext = await sonicSearchService.Search(searchOption); if (searchOptionNext.Messages.Count == 0) { searchOption = await searchService.Search(searchOption); } await sendService.ExecuteAsync(searchOption, searchOptionNext.Messages); } catch (KeyNotFoundException) { } catch (ArgumentException) { } }