private async void Bot_OnMessage(object sender, MessageEventArgs e) { try { if (e.Message.Text != null) { if (!ConnectedClients.TryGetValue(e.Message.From.Id, out TelegramClientInfo clientInfo)) { clientInfo = new TelegramClientInfo(_serverBase) { ConnectedDateTime = DateTime.Now, ClientId = Guid.NewGuid().ToString(), Message = e.Message, SignalGoBotManager = this }; _serverBase.Clients.TryAdd(clientInfo.ClientId, clientInfo); ConnectedClients.TryAdd(e.Message.From.Id, clientInfo); CurrentBotStructureInfo.OnClientConnected(clientInfo, this); } if (Services.Count == 0) { GetListOfServicesButtons(clientInfo); } BotButtonInfo buttonInfo = null; if (UsersBotButtons.TryGetValue(clientInfo, out Dictionary <string, BotButtonInfo> buttons)) { if (buttons.TryGetValue(e.Message.Text, out buttonInfo)) { if (buttonInfo.ServiceName != null) { clientInfo.CurrentServiceName = buttonInfo.ServiceName; } if (buttonInfo.MethodName != null) { clientInfo.CurrentMethodName = buttonInfo.MethodName; } } } if (buttonInfo != null && buttonInfo.Click != null) { buttonInfo.Click(clientInfo); } else if (e.Message.Text == CurrentBotStructureInfo.GetCancelButtonText(clientInfo)) { clientInfo.ParameterInfoes.Clear(); if (!string.IsNullOrEmpty(clientInfo.CurrentMethodName) && !string.IsNullOrEmpty(clientInfo.CurrentServiceName)) { await ShowServiceMethods(clientInfo.CurrentServiceName, clientInfo, e.Message); } else { await ShowServices(clientInfo, e); } } else if (e.Message.Text == CurrentBotStructureInfo.GetSendButtonText(clientInfo) && !string.IsNullOrEmpty(clientInfo.CurrentServiceName) && !string.IsNullOrEmpty(clientInfo.CurrentMethodName)) { if (Services.TryGetValue(clientInfo.CurrentServiceName, out Type service)) { if (CurrentBotStructureInfo.OnBeforeMethodCall(_serverBase, clientInfo, clientInfo.CurrentServiceName, clientInfo.CurrentMethodName, clientInfo.ParameterInfoes)) { Shared.Models.CallMethodResultInfo <OperationContext> result = await CallMethod(clientInfo); MethodInfo method = service.GetFullServiceLevelMethods().FirstOrDefault(x => x.Name.Equals(clientInfo.CurrentMethodName, StringComparison.OrdinalIgnoreCase)); if (OverridedMethodResponses.TryGetValue(service, out Dictionary <string, Delegate> methods) && methods.TryGetValue(clientInfo.CurrentMethodName, out Delegate function)) { BotCustomResponse botCustomResponse = new BotCustomResponse(); BotResponseInfoBase response = (BotResponseInfoBase)function.DynamicInvoke(result.Context, botCustomResponse, result.Result); await ShowResultValue(response.Message, method, clientInfo, e); botCustomResponse.OnAfterComeplete?.Invoke(); } else { string customResponse = CurrentBotStructureInfo.OnCustomResponse(_serverBase, clientInfo, clientInfo.CurrentServiceName, clientInfo.CurrentMethodName, clientInfo.ParameterInfoes, result, out bool responseChanged); if (responseChanged) { await ShowResultValue(customResponse, method, clientInfo, e); } else { await ShowResultValue(result, method, clientInfo, e.Message); } } } } } else if (string.IsNullOrEmpty(clientInfo.CurrentServiceName)) { string serviceName = GetServiceNameByCaption(e.Message.Text); if (Services.ContainsKey(serviceName)) { await ShowServiceMethods(serviceName, clientInfo, e.Message); } else { if (string.IsNullOrEmpty(clientInfo.CurrentMethodName) && !string.IsNullOrEmpty(clientInfo.CurrentServiceName)) { await ShowServiceMethods(clientInfo.CurrentServiceName, clientInfo, e.Message); } else if (string.IsNullOrEmpty(clientInfo.CurrentServiceName)) { await ShowServices(clientInfo, e); } } } else if (string.IsNullOrEmpty(clientInfo.CurrentMethodName)) { if (Services.TryGetValue(clientInfo.CurrentServiceName, out Type service)) { //MethodInfo method = service.GetFullServiceLevelMethods().FirstOrDefault(x => x.Name.Equals(e.Message.Text, StringComparison.OrdinalIgnoreCase)); MethodInfo method = GetMethodByCaption(service, e.Message.Text); if (method != null) { await ShowServiceMethods(method, clientInfo, e.Message); } else { await ShowServiceMethods(clientInfo.CurrentServiceName, clientInfo, e.Message); } } } else if (string.IsNullOrEmpty(clientInfo.CurrentParameterName)) { if (Services.TryGetValue(clientInfo.CurrentServiceName, out Type service)) { MethodInfo method = FindMethod(service, clientInfo.CurrentMethodName); //MethodInfo method = FindMethodByName(service, clientInfo.CurrentMethodName); //ParameterInfo parameter = method.GetParameters().FirstOrDefault(x => x.Name.Equals(e.Message.Text, StringComparison.OrdinalIgnoreCase)); ParameterInfo parameter = FindParameterByName(method, e.Message.Text, true); await GetParameterValueFromClient(method, parameter, clientInfo, e); } } else { if (Services.TryGetValue(clientInfo.CurrentServiceName, out Type service)) { MethodInfo method = FindMethod(service, clientInfo.CurrentMethodName); //MethodInfo method = FindMethodByName(service, clientInfo.CurrentMethodName); // ParameterInfo parameter = method.GetParameters().FirstOrDefault(x => x.Name.Equals(clientInfo.CurrentParameterName, StringComparison.OrdinalIgnoreCase)); ParameterInfo parameter = FindParameterByName(method, clientInfo.CurrentParameterName, false); await SetParameterValueFromClient(method, parameter, clientInfo, e); } } } } catch (System.Exception ex) { Console.WriteLine(ex); } }