Пример #1
0
        private List <List <BotButtonInfo> > GetMethodParametersButtons(MethodInfo method, TelegramClientInfo clientInfo)
        {
            List <BotButtonInfo> columns = new List <BotButtonInfo>();

            List <List <BotButtonInfo> > rows = new List <List <BotButtonInfo> >();

            int columnIndex = 0;

            List <ParameterInfo>    parameters    = method.GetParameters().ToList();
            BotDisplayNameAttribute nameAttribute = method.GetCustomAttribute <BotDisplayNameAttribute>();

            for (int i = 0; i < parameters.Count; i++)
            {
                ParameterInfo item          = parameters[i];
                string        parameterName = "";
                if (CurrentBotStructureInfo.InitializeServicesFromAttributes)
                {
                    if (nameAttribute != null)
                    {
                        parameterName = nameAttribute.FindValue(item.Name);
                        //for take parameter attribute
                        if (parameterName == null)
                        {
                            nameAttribute = null;
                        }
                    }
                    //take attribute of parameter
                    if (nameAttribute == null)
                    {
                        nameAttribute = item.GetCustomAttribute <BotDisplayNameAttribute>();
                    }
                    if (nameAttribute == null)
                    {
                        continue;
                    }
                    //if parametername not found
                    if (string.IsNullOrEmpty(parameterName))
                    {
                        parameterName = nameAttribute.Content;
                    }
                }
                else
                {
                    parameterName = item.Name;
                }
                if (columnIndex == 2)
                {
                    columnIndex = 0;
                    rows.Add(columns.ToList());
                    columns.Clear();
                }
                columns.Add(parameterName);
                columnIndex++;
            }
            if (rows.Count == 0)
            {
                rows.Add(columns);
            }
            rows.Add(new List <BotButtonInfo>()
            {
                new BotButtonInfo(CurrentBotStructureInfo.GetSendButtonText(clientInfo))
            });
            rows.Add(new List <BotButtonInfo>()
            {
                new BotButtonInfo(CurrentBotStructureInfo.GetCancelButtonText(clientInfo))
            });
            return(rows);
        }
Пример #2
0
        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);
            }
        }